ThreeDOM.js 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. var THREE = THREE || { REVISION: '49dev' };
  5. if ( ! self.Int32Array ) {
  6. self.Int32Array = Array;
  7. self.Float32Array = Array;
  8. }
  9. // http://paulirish.com/2011/requestanimationframe-for-smart-animating/
  10. // http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
  11. // requestAnimationFrame polyfill by Erik Möller
  12. // fixes from Paul Irish and Tino Zijdel
  13. (function() {
  14. var lastTime = 0;
  15. var vendors = ['ms', 'moz', 'webkit', 'o'];
  16. for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
  17. window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
  18. window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
  19. || window[vendors[x]+'CancelRequestAnimationFrame'];
  20. }
  21. if (!window.requestAnimationFrame)
  22. window.requestAnimationFrame = function(callback, element) {
  23. var currTime = new Date().getTime();
  24. var timeToCall = Math.max(0, 16 - (currTime - lastTime));
  25. var id = window.setTimeout(function() { callback(currTime + timeToCall); },
  26. timeToCall);
  27. lastTime = currTime + timeToCall;
  28. return id;
  29. };
  30. if (!window.cancelAnimationFrame)
  31. window.cancelAnimationFrame = function(id) {
  32. clearTimeout(id);
  33. };
  34. }());
  35. /**
  36. * @author mr.doob / http://mrdoob.com/
  37. */
  38. THREE.Color = function ( hex ) {
  39. if ( hex !== undefined ) this.setHex( hex );
  40. return this;
  41. };
  42. THREE.Color.prototype = {
  43. constructor: THREE.Color,
  44. r: 1, g: 1, b: 1,
  45. copy: function ( color ) {
  46. this.r = color.r;
  47. this.g = color.g;
  48. this.b = color.b;
  49. return this;
  50. },
  51. copyGammaToLinear: function ( color ) {
  52. this.r = color.r * color.r;
  53. this.g = color.g * color.g;
  54. this.b = color.b * color.b;
  55. return this;
  56. },
  57. copyLinearToGamma: function ( color ) {
  58. this.r = Math.sqrt( color.r );
  59. this.g = Math.sqrt( color.g );
  60. this.b = Math.sqrt( color.b );
  61. return this;
  62. },
  63. convertGammaToLinear: function () {
  64. var r = this.r, g = this.g, b = this.b;
  65. this.r = r * r;
  66. this.g = g * g;
  67. this.b = b * b;
  68. return this;
  69. },
  70. convertLinearToGamma: function () {
  71. this.r = Math.sqrt( this.r );
  72. this.g = Math.sqrt( this.g );
  73. this.b = Math.sqrt( this.b );
  74. return this;
  75. },
  76. setRGB: function ( r, g, b ) {
  77. this.r = r;
  78. this.g = g;
  79. this.b = b;
  80. return this;
  81. },
  82. setHSV: function ( h, s, v ) {
  83. // based on MochiKit implementation by Bob Ippolito
  84. // h,s,v ranges are < 0.0 - 1.0 >
  85. var i, f, p, q, t;
  86. if ( v === 0 ) {
  87. this.r = this.g = this.b = 0;
  88. } else {
  89. i = Math.floor( h * 6 );
  90. f = ( h * 6 ) - i;
  91. p = v * ( 1 - s );
  92. q = v * ( 1 - ( s * f ) );
  93. t = v * ( 1 - ( s * ( 1 - f ) ) );
  94. switch ( i ) {
  95. case 1: this.r = q; this.g = v; this.b = p; break;
  96. case 2: this.r = p; this.g = v; this.b = t; break;
  97. case 3: this.r = p; this.g = q; this.b = v; break;
  98. case 4: this.r = t; this.g = p; this.b = v; break;
  99. case 5: this.r = v; this.g = p; this.b = q; break;
  100. case 6: // fall through
  101. case 0: this.r = v; this.g = t; this.b = p; break;
  102. }
  103. }
  104. return this;
  105. },
  106. setHex: function ( hex ) {
  107. hex = Math.floor( hex );
  108. this.r = ( hex >> 16 & 255 ) / 255;
  109. this.g = ( hex >> 8 & 255 ) / 255;
  110. this.b = ( hex & 255 ) / 255;
  111. return this;
  112. },
  113. lerpSelf: function ( color, alpha ) {
  114. this.r += ( color.r - this.r ) * alpha;
  115. this.g += ( color.g - this.g ) * alpha;
  116. this.b += ( color.b - this.b ) * alpha;
  117. return this;
  118. },
  119. getHex: function () {
  120. return Math.floor( this.r * 255 ) << 16 ^ Math.floor( this.g * 255 ) << 8 ^ Math.floor( this.b * 255 );
  121. },
  122. getContextStyle: function () {
  123. return 'rgb(' + Math.floor( this.r * 255 ) + ',' + Math.floor( this.g * 255 ) + ',' + Math.floor( this.b * 255 ) + ')';
  124. },
  125. clone: function () {
  126. return new THREE.Color().setRGB( this.r, this.g, this.b );
  127. }
  128. };
  129. /**
  130. * @author mr.doob / http://mrdoob.com/
  131. * @author philogb / http://blog.thejit.org/
  132. * @author egraether / http://egraether.com/
  133. * @author zz85 / http://www.lab4games.net/zz85/blog
  134. */
  135. THREE.Vector2 = function ( x, y ) {
  136. this.x = x || 0;
  137. this.y = y || 0;
  138. };
  139. THREE.Vector2.prototype = {
  140. constructor: THREE.Vector2,
  141. set: function ( x, y ) {
  142. this.x = x;
  143. this.y = y;
  144. return this;
  145. },
  146. copy: function ( v ) {
  147. this.x = v.x;
  148. this.y = v.y;
  149. return this;
  150. },
  151. add: function ( a, b ) {
  152. this.x = a.x + b.x;
  153. this.y = a.y + b.y;
  154. return this;
  155. },
  156. addSelf: function ( v ) {
  157. this.x += v.x;
  158. this.y += v.y;
  159. return this;
  160. },
  161. sub: function ( a, b ) {
  162. this.x = a.x - b.x;
  163. this.y = a.y - b.y;
  164. return this;
  165. },
  166. subSelf: function ( v ) {
  167. this.x -= v.x;
  168. this.y -= v.y;
  169. return this;
  170. },
  171. multiplyScalar: function ( s ) {
  172. this.x *= s;
  173. this.y *= s;
  174. return this;
  175. },
  176. divideScalar: function ( s ) {
  177. if ( s ) {
  178. this.x /= s;
  179. this.y /= s;
  180. } else {
  181. this.set( 0, 0 );
  182. }
  183. return this;
  184. },
  185. negate: function() {
  186. return this.multiplyScalar( - 1 );
  187. },
  188. dot: function ( v ) {
  189. return this.x * v.x + this.y * v.y;
  190. },
  191. lengthSq: function () {
  192. return this.x * this.x + this.y * this.y;
  193. },
  194. length: function () {
  195. return Math.sqrt( this.lengthSq() );
  196. },
  197. normalize: function () {
  198. return this.divideScalar( this.length() );
  199. },
  200. distanceTo: function ( v ) {
  201. return Math.sqrt( this.distanceToSquared( v ) );
  202. },
  203. distanceToSquared: function ( v ) {
  204. var dx = this.x - v.x, dy = this.y - v.y;
  205. return dx * dx + dy * dy;
  206. },
  207. setLength: function ( l ) {
  208. return this.normalize().multiplyScalar( l );
  209. },
  210. lerpSelf: function ( v, alpha ) {
  211. this.x += ( v.x - this.x ) * alpha;
  212. this.y += ( v.y - this.y ) * alpha;
  213. return this;
  214. },
  215. equals: function( v ) {
  216. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  217. },
  218. isZero: function () {
  219. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  220. },
  221. clone: function () {
  222. return new THREE.Vector2( this.x, this.y );
  223. }
  224. };
  225. /**
  226. * @author mr.doob / http://mrdoob.com/
  227. * @author kile / http://kile.stravaganza.org/
  228. * @author philogb / http://blog.thejit.org/
  229. * @author mikael emtinger / http://gomo.se/
  230. * @author egraether / http://egraether.com/
  231. */
  232. THREE.Vector3 = function ( x, y, z ) {
  233. this.x = x || 0;
  234. this.y = y || 0;
  235. this.z = z || 0;
  236. };
  237. THREE.Vector3.prototype = {
  238. constructor: THREE.Vector3,
  239. set: function ( x, y, z ) {
  240. this.x = x;
  241. this.y = y;
  242. this.z = z;
  243. return this;
  244. },
  245. setX: function ( x ) {
  246. this.x = x;
  247. return this;
  248. },
  249. setY: function ( y ) {
  250. this.y = y;
  251. return this;
  252. },
  253. setZ: function ( z ) {
  254. this.z = z;
  255. return this;
  256. },
  257. copy: function ( v ) {
  258. this.x = v.x;
  259. this.y = v.y;
  260. this.z = v.z;
  261. return this;
  262. },
  263. add: function ( a, b ) {
  264. this.x = a.x + b.x;
  265. this.y = a.y + b.y;
  266. this.z = a.z + b.z;
  267. return this;
  268. },
  269. addSelf: function ( v ) {
  270. this.x += v.x;
  271. this.y += v.y;
  272. this.z += v.z;
  273. return this;
  274. },
  275. addScalar: function ( s ) {
  276. this.x += s;
  277. this.y += s;
  278. this.z += s;
  279. return this;
  280. },
  281. sub: function ( a, b ) {
  282. this.x = a.x - b.x;
  283. this.y = a.y - b.y;
  284. this.z = a.z - b.z;
  285. return this;
  286. },
  287. subSelf: function ( v ) {
  288. this.x -= v.x;
  289. this.y -= v.y;
  290. this.z -= v.z;
  291. return this;
  292. },
  293. multiply: function ( a, b ) {
  294. this.x = a.x * b.x;
  295. this.y = a.y * b.y;
  296. this.z = a.z * b.z;
  297. return this;
  298. },
  299. multiplySelf: function ( v ) {
  300. this.x *= v.x;
  301. this.y *= v.y;
  302. this.z *= v.z;
  303. return this;
  304. },
  305. multiplyScalar: function ( s ) {
  306. this.x *= s;
  307. this.y *= s;
  308. this.z *= s;
  309. return this;
  310. },
  311. divideSelf: function ( v ) {
  312. this.x /= v.x;
  313. this.y /= v.y;
  314. this.z /= v.z;
  315. return this;
  316. },
  317. divideScalar: function ( s ) {
  318. if ( s ) {
  319. this.x /= s;
  320. this.y /= s;
  321. this.z /= s;
  322. } else {
  323. this.x = 0;
  324. this.y = 0;
  325. this.z = 0;
  326. }
  327. return this;
  328. },
  329. negate: function() {
  330. return this.multiplyScalar( - 1 );
  331. },
  332. dot: function ( v ) {
  333. return this.x * v.x + this.y * v.y + this.z * v.z;
  334. },
  335. lengthSq: function () {
  336. return this.x * this.x + this.y * this.y + this.z * this.z;
  337. },
  338. length: function () {
  339. return Math.sqrt( this.lengthSq() );
  340. },
  341. lengthManhattan: function () {
  342. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  343. },
  344. normalize: function () {
  345. return this.divideScalar( this.length() );
  346. },
  347. setLength: function ( l ) {
  348. return this.normalize().multiplyScalar( l );
  349. },
  350. lerpSelf: function ( v, alpha ) {
  351. this.x += ( v.x - this.x ) * alpha;
  352. this.y += ( v.y - this.y ) * alpha;
  353. this.z += ( v.z - this.z ) * alpha;
  354. return this;
  355. },
  356. cross: function ( a, b ) {
  357. this.x = a.y * b.z - a.z * b.y;
  358. this.y = a.z * b.x - a.x * b.z;
  359. this.z = a.x * b.y - a.y * b.x;
  360. return this;
  361. },
  362. crossSelf: function ( v ) {
  363. var x = this.x, y = this.y, z = this.z;
  364. this.x = y * v.z - z * v.y;
  365. this.y = z * v.x - x * v.z;
  366. this.z = x * v.y - y * v.x;
  367. return this;
  368. },
  369. distanceTo: function ( v ) {
  370. return Math.sqrt( this.distanceToSquared( v ) );
  371. },
  372. distanceToSquared: function ( v ) {
  373. return new THREE.Vector3().sub( this, v ).lengthSq();
  374. },
  375. getPositionFromMatrix: function ( m ) {
  376. this.x = m.elements[12];
  377. this.y = m.elements[13];
  378. this.z = m.elements[14];
  379. return this;
  380. },
  381. getRotationFromMatrix: function ( m, scale ) {
  382. var sx = scale ? scale.x : 1;
  383. var sy = scale ? scale.y : 1;
  384. var sz = scale ? scale.z : 1;
  385. var m11 = m.elements[0] / sx, m12 = m.elements[4] / sy, m13 = m.elements[8] / sz;
  386. var m21 = m.elements[1] / sx, m22 = m.elements[5] / sy, m23 = m.elements[9] / sz;
  387. var m33 = m.elements[10] / sz;
  388. this.y = Math.asin( m13 );
  389. var cosY = Math.cos( this.y );
  390. if ( Math.abs( cosY ) > 0.00001 ) {
  391. this.x = Math.atan2( - m23 / cosY, m33 / cosY );
  392. this.z = Math.atan2( - m12 / cosY, m11 / cosY );
  393. } else {
  394. this.x = 0;
  395. this.z = Math.atan2( m21, m22 );
  396. }
  397. return this;
  398. },
  399. /*
  400. // from http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  401. // order XYZ
  402. getEulerXYZFromQuaternion: function ( q ) {
  403. this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z ) );
  404. this.y = Math.asin( 2 * ( q.x * q.z + q.y * q.w ) );
  405. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z ) );
  406. },
  407. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
  408. // order YZX (assuming heading == y, attitude == z, bank == x)
  409. getEulerYZXFromQuaternion: function ( q ) {
  410. var sqw = q.w * q.w;
  411. var sqx = q.x * q.x;
  412. var sqy = q.y * q.y;
  413. var sqz = q.z * q.z;
  414. var unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
  415. var test = q.x * q.y + q.z * q.w;
  416. if ( test > 0.499 * unit ) { // singularity at north pole
  417. this.y = 2 * Math.atan2( q.x, q.w );
  418. this.z = Math.PI / 2;
  419. this.x = 0;
  420. return;
  421. }
  422. if ( test < -0.499 * unit ) { // singularity at south pole
  423. this.y = -2 * Math.atan2( q.x, q.w );
  424. this.z = -Math.PI / 2;
  425. this.x = 0;
  426. return;
  427. }
  428. this.y = Math.atan2( 2 * q.y * q.w - 2 * q.x * q.z, sqx - sqy - sqz + sqw );
  429. this.z = Math.asin( 2 * test / unit );
  430. this.x = Math.atan2( 2 * q.x * q.w - 2 * q.y * q.z, -sqx + sqy - sqz + sqw );
  431. },
  432. */
  433. getScaleFromMatrix: function ( m ) {
  434. var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  435. var sy = this.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  436. var sz = this.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  437. this.x = sx;
  438. this.y = sy;
  439. this.z = sz;
  440. },
  441. equals: function ( v ) {
  442. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  443. },
  444. isZero: function () {
  445. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  446. },
  447. clone: function () {
  448. return new THREE.Vector3( this.x, this.y, this.z );
  449. }
  450. };
  451. /**
  452. * @author supereggbert / http://www.paulbrunt.co.uk/
  453. * @author philogb / http://blog.thejit.org/
  454. * @author mikael emtinger / http://gomo.se/
  455. * @author egraether / http://egraether.com/
  456. */
  457. THREE.Vector4 = function ( x, y, z, w ) {
  458. this.x = x || 0;
  459. this.y = y || 0;
  460. this.z = z || 0;
  461. this.w = ( w !== undefined ) ? w : 1;
  462. };
  463. THREE.Vector4.prototype = {
  464. constructor: THREE.Vector4,
  465. set: function ( x, y, z, w ) {
  466. this.x = x;
  467. this.y = y;
  468. this.z = z;
  469. this.w = w;
  470. return this;
  471. },
  472. copy: function ( v ) {
  473. this.x = v.x;
  474. this.y = v.y;
  475. this.z = v.z;
  476. this.w = ( v.w !== undefined ) ? v.w : 1;
  477. return this;
  478. },
  479. add: function ( a, b ) {
  480. this.x = a.x + b.x;
  481. this.y = a.y + b.y;
  482. this.z = a.z + b.z;
  483. this.w = a.w + b.w;
  484. return this;
  485. },
  486. addSelf: function ( v ) {
  487. this.x += v.x;
  488. this.y += v.y;
  489. this.z += v.z;
  490. this.w += v.w;
  491. return this;
  492. },
  493. sub: function ( a, b ) {
  494. this.x = a.x - b.x;
  495. this.y = a.y - b.y;
  496. this.z = a.z - b.z;
  497. this.w = a.w - b.w;
  498. return this;
  499. },
  500. subSelf: function ( v ) {
  501. this.x -= v.x;
  502. this.y -= v.y;
  503. this.z -= v.z;
  504. this.w -= v.w;
  505. return this;
  506. },
  507. multiplyScalar: function ( s ) {
  508. this.x *= s;
  509. this.y *= s;
  510. this.z *= s;
  511. this.w *= s;
  512. return this;
  513. },
  514. divideScalar: function ( s ) {
  515. if ( s ) {
  516. this.x /= s;
  517. this.y /= s;
  518. this.z /= s;
  519. this.w /= s;
  520. } else {
  521. this.x = 0;
  522. this.y = 0;
  523. this.z = 0;
  524. this.w = 1;
  525. }
  526. return this;
  527. },
  528. negate: function() {
  529. return this.multiplyScalar( -1 );
  530. },
  531. dot: function ( v ) {
  532. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  533. },
  534. lengthSq: function () {
  535. return this.dot( this );
  536. },
  537. length: function () {
  538. return Math.sqrt( this.lengthSq() );
  539. },
  540. normalize: function () {
  541. return this.divideScalar( this.length() );
  542. },
  543. setLength: function ( l ) {
  544. return this.normalize().multiplyScalar( l );
  545. },
  546. lerpSelf: function ( v, alpha ) {
  547. this.x += ( v.x - this.x ) * alpha;
  548. this.y += ( v.y - this.y ) * alpha;
  549. this.z += ( v.z - this.z ) * alpha;
  550. this.w += ( v.w - this.w ) * alpha;
  551. return this;
  552. },
  553. clone: function () {
  554. return new THREE.Vector4( this.x, this.y, this.z, this.w );
  555. }
  556. };
  557. /**
  558. * @author mrdoob / http://mrdoob.com/
  559. * @author alteredq / http://alteredqualia.com/
  560. */
  561. THREE.Frustum = function ( ) {
  562. this.planes = [
  563. new THREE.Vector4(),
  564. new THREE.Vector4(),
  565. new THREE.Vector4(),
  566. new THREE.Vector4(),
  567. new THREE.Vector4(),
  568. new THREE.Vector4()
  569. ];
  570. };
  571. THREE.Frustum.prototype.setFromMatrix = function ( m ) {
  572. var i, plane,
  573. planes = this.planes;
  574. planes[ 0 ].set( m.elements[3] - m.elements[0], m.elements[7] - m.elements[4], m.elements[11] - m.elements[8], m.elements[15] - m.elements[12] );
  575. planes[ 1 ].set( m.elements[3] + m.elements[0], m.elements[7] + m.elements[4], m.elements[11] + m.elements[8], m.elements[15] + m.elements[12] );
  576. planes[ 2 ].set( m.elements[3] + m.elements[1], m.elements[7] + m.elements[5], m.elements[11] + m.elements[9], m.elements[15] + m.elements[13] );
  577. planes[ 3 ].set( m.elements[3] - m.elements[1], m.elements[7] - m.elements[5], m.elements[11] - m.elements[9], m.elements[15] - m.elements[13] );
  578. planes[ 4 ].set( m.elements[3] - m.elements[2], m.elements[7] - m.elements[6], m.elements[11] - m.elements[10], m.elements[15] - m.elements[14] );
  579. planes[ 5 ].set( m.elements[3] + m.elements[2], m.elements[7] + m.elements[6], m.elements[11] + m.elements[10], m.elements[15] + m.elements[14] );
  580. for ( i = 0; i < 6; i ++ ) {
  581. plane = planes[ i ];
  582. plane.divideScalar( Math.sqrt( plane.x * plane.x + plane.y * plane.y + plane.z * plane.z ) );
  583. }
  584. };
  585. THREE.Frustum.prototype.contains = function ( object ) {
  586. var distance,
  587. planes = this.planes,
  588. matrix = object.matrixWorld,
  589. scale = THREE.Frustum.__v1.set( matrix.getColumnX().length(), matrix.getColumnY().length(), matrix.getColumnZ().length() ),
  590. radius = - object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) );
  591. for ( var i = 0; i < 6; i ++ ) {
  592. distance = planes[ i ].x * matrix.elements[12] + planes[ i ].y * matrix.elements[13] + planes[ i ].z * matrix.elements[14] + planes[ i ].w;
  593. if ( distance <= radius ) return false;
  594. }
  595. return true;
  596. };
  597. THREE.Frustum.__v1 = new THREE.Vector3();
  598. /**
  599. * @author mr.doob / http://mrdoob.com/
  600. */
  601. THREE.Ray = function ( origin, direction ) {
  602. this.origin = origin || new THREE.Vector3();
  603. this.direction = direction || new THREE.Vector3();
  604. var precision = 0.0001;
  605. this.setPrecision = function ( value ) {
  606. precision = value;
  607. };
  608. var a = new THREE.Vector3();
  609. var b = new THREE.Vector3();
  610. var c = new THREE.Vector3();
  611. var d = new THREE.Vector3();
  612. var originCopy = new THREE.Vector3();
  613. var directionCopy = new THREE.Vector3();
  614. var vector = new THREE.Vector3();
  615. var normal = new THREE.Vector3();
  616. var intersectPoint = new THREE.Vector3()
  617. this.intersectObject = function ( object ) {
  618. var intersect, intersects = [];
  619. if ( object instanceof THREE.Particle ) {
  620. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  621. if ( distance > object.scale.x ) {
  622. return [];
  623. }
  624. intersect = {
  625. distance: distance,
  626. point: object.position,
  627. face: null,
  628. object: object
  629. };
  630. intersects.push( intersect );
  631. } else if ( object instanceof THREE.Mesh ) {
  632. // Checking boundingSphere
  633. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  634. var scale = THREE.Frustum.__v1.set( object.matrixWorld.getColumnX().length(), object.matrixWorld.getColumnY().length(), object.matrixWorld.getColumnZ().length() );
  635. if ( distance > object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) ) ) {
  636. return intersects;
  637. }
  638. // Checking faces
  639. var f, fl, face, dot, scalar,
  640. geometry = object.geometry,
  641. vertices = geometry.vertices,
  642. objMatrix;
  643. object.matrixRotationWorld.extractRotation( object.matrixWorld );
  644. for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  645. face = geometry.faces[ f ];
  646. originCopy.copy( this.origin );
  647. directionCopy.copy( this.direction );
  648. objMatrix = object.matrixWorld;
  649. // determine if ray intersects the plane of the face
  650. // note: this works regardless of the direction of the face normal
  651. vector = objMatrix.multiplyVector3( vector.copy( face.centroid ) ).subSelf( originCopy );
  652. normal = object.matrixRotationWorld.multiplyVector3( normal.copy( face.normal ) );
  653. dot = directionCopy.dot( normal );
  654. // bail if ray and plane are parallel
  655. if ( Math.abs( dot ) < precision ) continue;
  656. // calc distance to plane
  657. scalar = normal.dot( vector ) / dot;
  658. // if negative distance, then plane is behind ray
  659. if ( scalar < 0 ) continue;
  660. if ( object.doubleSided || ( object.flipSided ? dot > 0 : dot < 0 ) ) {
  661. intersectPoint.add( originCopy, directionCopy.multiplyScalar( scalar ) );
  662. if ( face instanceof THREE.Face3 ) {
  663. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ].position ) );
  664. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ].position ) );
  665. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ].position ) );
  666. if ( pointInFace3( intersectPoint, a, b, c ) ) {
  667. intersect = {
  668. distance: originCopy.distanceTo( intersectPoint ),
  669. point: intersectPoint.clone(),
  670. face: face,
  671. object: object
  672. };
  673. intersects.push( intersect );
  674. }
  675. } else if ( face instanceof THREE.Face4 ) {
  676. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ].position ) );
  677. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ].position ) );
  678. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ].position ) );
  679. d = objMatrix.multiplyVector3( d.copy( vertices[ face.d ].position ) );
  680. if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
  681. intersect = {
  682. distance: originCopy.distanceTo( intersectPoint ),
  683. point: intersectPoint.clone(),
  684. face: face,
  685. object: object
  686. };
  687. intersects.push( intersect );
  688. }
  689. }
  690. }
  691. }
  692. }
  693. return intersects;
  694. }
  695. this.intersectObjects = function ( objects ) {
  696. var intersects = [];
  697. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  698. Array.prototype.push.apply( intersects, this.intersectObject( objects[ i ] ) );
  699. }
  700. intersects.sort( function ( a, b ) { return a.distance - b.distance; } );
  701. return intersects;
  702. };
  703. var v0 = new THREE.Vector3(), v1 = new THREE.Vector3(), v2 = new THREE.Vector3();
  704. var dot, intersect, distance;
  705. function distanceFromIntersection( origin, direction, position ) {
  706. v0.sub( position, origin );
  707. dot = v0.dot( direction );
  708. intersect = v1.add( origin, v2.copy( direction ).multiplyScalar( dot ) );
  709. distance = position.distanceTo( intersect );
  710. return distance;
  711. }
  712. // http://www.blackpawn.com/texts/pointinpoly/default.html
  713. var dot00, dot01, dot02, dot11, dot12, invDenom, u, v;
  714. function pointInFace3( p, a, b, c ) {
  715. v0.sub( c, a );
  716. v1.sub( b, a );
  717. v2.sub( p, a );
  718. dot00 = v0.dot( v0 );
  719. dot01 = v0.dot( v1 );
  720. dot02 = v0.dot( v2 );
  721. dot11 = v1.dot( v1 );
  722. dot12 = v1.dot( v2 );
  723. invDenom = 1 / ( dot00 * dot11 - dot01 * dot01 );
  724. u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  725. v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  726. return ( u >= 0 ) && ( v >= 0 ) && ( u + v < 1 );
  727. }
  728. };/**
  729. * @author mr.doob / http://mrdoob.com/
  730. */
  731. THREE.Rectangle = function () {
  732. var _left, _top, _right, _bottom,
  733. _width, _height, _isEmpty = true;
  734. function resize() {
  735. _width = _right - _left;
  736. _height = _bottom - _top;
  737. }
  738. this.getX = function () {
  739. return _left;
  740. };
  741. this.getY = function () {
  742. return _top;
  743. };
  744. this.getWidth = function () {
  745. return _width;
  746. };
  747. this.getHeight = function () {
  748. return _height;
  749. };
  750. this.getLeft = function() {
  751. return _left;
  752. };
  753. this.getTop = function() {
  754. return _top;
  755. };
  756. this.getRight = function() {
  757. return _right;
  758. };
  759. this.getBottom = function() {
  760. return _bottom;
  761. };
  762. this.set = function ( left, top, right, bottom ) {
  763. _isEmpty = false;
  764. _left = left; _top = top;
  765. _right = right; _bottom = bottom;
  766. resize();
  767. };
  768. this.addPoint = function ( x, y ) {
  769. if ( _isEmpty ) {
  770. _isEmpty = false;
  771. _left = x; _top = y;
  772. _right = x; _bottom = y;
  773. resize();
  774. } else {
  775. _left = _left < x ? _left : x; // Math.min( _left, x );
  776. _top = _top < y ? _top : y; // Math.min( _top, y );
  777. _right = _right > x ? _right : x; // Math.max( _right, x );
  778. _bottom = _bottom > y ? _bottom : y; // Math.max( _bottom, y );
  779. resize();
  780. }
  781. };
  782. this.add3Points = function ( x1, y1, x2, y2, x3, y3 ) {
  783. if (_isEmpty) {
  784. _isEmpty = false;
  785. _left = x1 < x2 ? ( x1 < x3 ? x1 : x3 ) : ( x2 < x3 ? x2 : x3 );
  786. _top = y1 < y2 ? ( y1 < y3 ? y1 : y3 ) : ( y2 < y3 ? y2 : y3 );
  787. _right = x1 > x2 ? ( x1 > x3 ? x1 : x3 ) : ( x2 > x3 ? x2 : x3 );
  788. _bottom = y1 > y2 ? ( y1 > y3 ? y1 : y3 ) : ( y2 > y3 ? y2 : y3 );
  789. resize();
  790. } else {
  791. _left = x1 < x2 ? ( x1 < x3 ? ( x1 < _left ? x1 : _left ) : ( x3 < _left ? x3 : _left ) ) : ( x2 < x3 ? ( x2 < _left ? x2 : _left ) : ( x3 < _left ? x3 : _left ) );
  792. _top = y1 < y2 ? ( y1 < y3 ? ( y1 < _top ? y1 : _top ) : ( y3 < _top ? y3 : _top ) ) : ( y2 < y3 ? ( y2 < _top ? y2 : _top ) : ( y3 < _top ? y3 : _top ) );
  793. _right = x1 > x2 ? ( x1 > x3 ? ( x1 > _right ? x1 : _right ) : ( x3 > _right ? x3 : _right ) ) : ( x2 > x3 ? ( x2 > _right ? x2 : _right ) : ( x3 > _right ? x3 : _right ) );
  794. _bottom = y1 > y2 ? ( y1 > y3 ? ( y1 > _bottom ? y1 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) ) : ( y2 > y3 ? ( y2 > _bottom ? y2 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) );
  795. resize();
  796. };
  797. };
  798. this.addRectangle = function ( r ) {
  799. if ( _isEmpty ) {
  800. _isEmpty = false;
  801. _left = r.getLeft(); _top = r.getTop();
  802. _right = r.getRight(); _bottom = r.getBottom();
  803. resize();
  804. } else {
  805. _left = _left < r.getLeft() ? _left : r.getLeft(); // Math.min(_left, r.getLeft() );
  806. _top = _top < r.getTop() ? _top : r.getTop(); // Math.min(_top, r.getTop() );
  807. _right = _right > r.getRight() ? _right : r.getRight(); // Math.max(_right, r.getRight() );
  808. _bottom = _bottom > r.getBottom() ? _bottom : r.getBottom(); // Math.max(_bottom, r.getBottom() );
  809. resize();
  810. }
  811. };
  812. this.inflate = function ( v ) {
  813. _left -= v; _top -= v;
  814. _right += v; _bottom += v;
  815. resize();
  816. };
  817. this.minSelf = function ( r ) {
  818. _left = _left > r.getLeft() ? _left : r.getLeft(); // Math.max( _left, r.getLeft() );
  819. _top = _top > r.getTop() ? _top : r.getTop(); // Math.max( _top, r.getTop() );
  820. _right = _right < r.getRight() ? _right : r.getRight(); // Math.min( _right, r.getRight() );
  821. _bottom = _bottom < r.getBottom() ? _bottom : r.getBottom(); // Math.min( _bottom, r.getBottom() );
  822. resize();
  823. };
  824. this.intersects = function ( r ) {
  825. // http://gamemath.com/2011/09/detecting-whether-two-boxes-overlap/
  826. if ( _right < r.getLeft() ) return false;
  827. if ( _left > r.getRight() ) return false;
  828. if ( _bottom < r.getTop() ) return false;
  829. if ( _top > r.getBottom() ) return false;
  830. return true;
  831. };
  832. this.empty = function () {
  833. _isEmpty = true;
  834. _left = 0; _top = 0;
  835. _right = 0; _bottom = 0;
  836. resize();
  837. };
  838. this.isEmpty = function () {
  839. return _isEmpty;
  840. };
  841. };
  842. /**
  843. * @author alteredq / http://alteredqualia.com/
  844. */
  845. THREE.Math = {
  846. // Clamp value to range <a, b>
  847. clamp: function ( x, a, b ) {
  848. return ( x < a ) ? a : ( ( x > b ) ? b : x );
  849. },
  850. // Clamp value to range <a, inf)
  851. clampBottom: function ( x, a ) {
  852. return x < a ? a : x;
  853. },
  854. // Linear mapping from range <a1, a2> to range <b1, b2>
  855. mapLinear: function ( x, a1, a2, b1, b2 ) {
  856. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  857. },
  858. // Random float from <0, 1> with 16 bits of randomness
  859. // (standard Math.random() creates repetitive patterns when applied over larger space)
  860. random16: function () {
  861. return ( 65280 * Math.random() + 255 * Math.random() ) / 65535;
  862. },
  863. // Random integer from <low, high> interval
  864. randInt: function ( low, high ) {
  865. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  866. },
  867. // Random float from <low, high> interval
  868. randFloat: function ( low, high ) {
  869. return low + Math.random() * ( high - low );
  870. },
  871. // Random float from <-range/2, range/2> interval
  872. randFloatSpread: function ( range ) {
  873. return range * ( 0.5 - Math.random() );
  874. },
  875. sign: function ( x ) {
  876. return ( x < 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
  877. }
  878. };
  879. /**
  880. * @author alteredq / http://alteredqualia.com/
  881. */
  882. THREE.Matrix3 = function () {
  883. this.m = [];
  884. };
  885. THREE.Matrix3.prototype = {
  886. constructor: THREE.Matrix3,
  887. getInverse: function ( matrix ) {
  888. // input: THREE.Matrix4
  889. // ( based on http://code.google.com/p/webgl-mjs/ )
  890. var a11 = matrix.elements[10] * matrix.elements[5] - matrix.elements[6] * matrix.elements[9];
  891. var a21 = - matrix.elements[10] * matrix.elements[1] + matrix.elements[2] * matrix.elements[9];
  892. var a31 = matrix.elements[6] * matrix.elements[1] - matrix.elements[2] * matrix.elements[5];
  893. var a12 = - matrix.elements[10] * matrix.elements[4] + matrix.elements[6] * matrix.elements[8];
  894. var a22 = matrix.elements[10] * matrix.elements[0] - matrix.elements[2] * matrix.elements[8];
  895. var a32 = - matrix.elements[6] * matrix.elements[0] + matrix.elements[2] * matrix.elements[4];
  896. var a13 = matrix.elements[9] * matrix.elements[4] - matrix.elements[5] * matrix.elements[8];
  897. var a23 = - matrix.elements[9] * matrix.elements[0] + matrix.elements[1] * matrix.elements[8];
  898. var a33 = matrix.elements[5] * matrix.elements[0] - matrix.elements[1] * matrix.elements[4];
  899. var det = matrix.elements[0] * a11 + matrix.elements[1] * a12 + matrix.elements[2] * a13;
  900. // no inverse
  901. if ( det === 0 ) {
  902. console.warn( "Matrix3.getInverse(): determinant == 0" );
  903. }
  904. var idet = 1.0 / det;
  905. var m = this.m;
  906. m[ 0 ] = idet * a11; m[ 1 ] = idet * a21; m[ 2 ] = idet * a31;
  907. m[ 3 ] = idet * a12; m[ 4 ] = idet * a22; m[ 5 ] = idet * a32;
  908. m[ 6 ] = idet * a13; m[ 7 ] = idet * a23; m[ 8 ] = idet * a33;
  909. return this;
  910. },
  911. /*
  912. transpose: function () {
  913. var tmp, m = this.m;
  914. tmp = m[1]; m[1] = m[3]; m[3] = tmp;
  915. tmp = m[2]; m[2] = m[6]; m[6] = tmp;
  916. tmp = m[5]; m[5] = m[7]; m[7] = tmp;
  917. return this;
  918. },
  919. */
  920. transposeIntoArray: function ( r ) {
  921. var m = this.m;
  922. r[ 0 ] = m[ 0 ];
  923. r[ 1 ] = m[ 3 ];
  924. r[ 2 ] = m[ 6 ];
  925. r[ 3 ] = m[ 1 ];
  926. r[ 4 ] = m[ 4 ];
  927. r[ 5 ] = m[ 7 ];
  928. r[ 6 ] = m[ 2 ];
  929. r[ 7 ] = m[ 5 ];
  930. r[ 8 ] = m[ 8 ];
  931. return this;
  932. }
  933. };
  934. /**
  935. * @author mr.doob / http://mrdoob.com/
  936. * @author supereggbert / http://www.paulbrunt.co.uk/
  937. * @author philogb / http://blog.thejit.org/
  938. * @author jordi_ros / http://plattsoft.com
  939. * @author D1plo1d / http://github.com/D1plo1d
  940. * @author alteredq / http://alteredqualia.com/
  941. * @author mikael emtinger / http://gomo.se/
  942. * @author timknip / http://www.floorplanner.com/
  943. */
  944. THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  945. this.elements = new Float32Array(16);
  946. this.set(
  947. ( n11 !== undefined ) ? n11 : 1, n12 || 0, n13 || 0, n14 || 0,
  948. n21 || 0, ( n22 !== undefined ) ? n22 : 1, n23 || 0, n24 || 0,
  949. n31 || 0, n32 || 0, ( n33 !== undefined ) ? n33 : 1, n34 || 0,
  950. n41 || 0, n42 || 0, n43 || 0, ( n44 !== undefined ) ? n44 : 1
  951. );
  952. };
  953. THREE.Matrix4.prototype = {
  954. constructor: THREE.Matrix4,
  955. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  956. var te = this.elements;
  957. te[0] = n11; te[4] = n12; te[8] = n13; te[12] = n14;
  958. te[1] = n21; te[5] = n22; te[9] = n23; te[13] = n24;
  959. te[2] = n31; te[6] = n32; te[10] = n33; te[14] = n34;
  960. te[3] = n41; te[7] = n42; te[11] = n43; te[15] = n44;
  961. return this;
  962. },
  963. identity: function () {
  964. this.set(
  965. 1, 0, 0, 0,
  966. 0, 1, 0, 0,
  967. 0, 0, 1, 0,
  968. 0, 0, 0, 1
  969. );
  970. return this;
  971. },
  972. copy: function ( m ) {
  973. this.set(
  974. m.elements[0], m.elements[4], m.elements[8], m.elements[12],
  975. m.elements[1], m.elements[5], m.elements[9], m.elements[13],
  976. m.elements[2], m.elements[6], m.elements[10], m.elements[14],
  977. m.elements[3], m.elements[7], m.elements[11], m.elements[15]
  978. );
  979. return this;
  980. },
  981. lookAt: function ( eye, target, up ) {
  982. var te = this.elements;
  983. var x = THREE.Matrix4.__v1;
  984. var y = THREE.Matrix4.__v2;
  985. var z = THREE.Matrix4.__v3;
  986. z.sub( eye, target ).normalize();
  987. if ( z.length() === 0 ) {
  988. z.z = 1;
  989. }
  990. x.cross( up, z ).normalize();
  991. if ( x.length() === 0 ) {
  992. z.x += 0.0001;
  993. x.cross( up, z ).normalize();
  994. }
  995. y.cross( z, x );
  996. te[0] = x.x; te[4] = y.x; te[8] = z.x;
  997. te[1] = x.y; te[5] = y.y; te[9] = z.y;
  998. te[2] = x.z; te[6] = y.z; te[10] = z.z;
  999. return this;
  1000. },
  1001. multiply: function ( a, b ) {
  1002. var ae = a.elements,
  1003. be = b.elements,
  1004. te = this.elements;
  1005. var a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
  1006. var a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
  1007. var a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
  1008. var a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
  1009. var b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12];
  1010. var b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13];
  1011. var b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14];
  1012. var b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15];
  1013. te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  1014. te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  1015. te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  1016. te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  1017. te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  1018. te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  1019. te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  1020. te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  1021. te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  1022. te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  1023. te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  1024. te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  1025. te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  1026. te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  1027. te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  1028. te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  1029. return this;
  1030. },
  1031. multiplySelf: function ( m ) {
  1032. return this.multiply( this, m );
  1033. },
  1034. multiplyToArray: function ( a, b, r ) {
  1035. var te = this.elements;
  1036. this.multiply( a, b );
  1037. r[ 0 ] = te[0]; r[ 1 ] = te[1]; r[ 2 ] = te[2]; r[ 3 ] = te[3];
  1038. r[ 4 ] = te[4]; r[ 5 ] = te[5]; r[ 6 ] = te[6]; r[ 7 ] = te[7];
  1039. r[ 8 ] = te[8]; r[ 9 ] = te[9]; r[ 10 ] = te[10]; r[ 11 ] = te[11];
  1040. r[ 12 ] = te[12]; r[ 13 ] = te[13]; r[ 14 ] = te[14]; r[ 15 ] = te[15];
  1041. return this;
  1042. },
  1043. multiplyScalar: function ( s ) {
  1044. var te = this.elements;
  1045. te[0] *= s; te[4] *= s; te[8] *= s; te[12] *= s;
  1046. te[1] *= s; te[5] *= s; te[9] *= s; te[13] *= s;
  1047. te[2] *= s; te[6] *= s; te[10] *= s; te[14] *= s;
  1048. te[3] *= s; te[7] *= s; te[11] *= s; te[15] *= s;
  1049. return this;
  1050. },
  1051. multiplyVector3: function ( v ) {
  1052. var te = this.elements;
  1053. var vx = v.x, vy = v.y, vz = v.z;
  1054. var d = 1 / ( te[3] * vx + te[7] * vy + te[11] * vz + te[15] );
  1055. v.x = ( te[0] * vx + te[4] * vy + te[8] * vz + te[12] ) * d;
  1056. v.y = ( te[1] * vx + te[5] * vy + te[9] * vz + te[13] ) * d;
  1057. v.z = ( te[2] * vx + te[6] * vy + te[10] * vz + te[14] ) * d;
  1058. return v;
  1059. },
  1060. multiplyVector4: function ( v ) {
  1061. var te = this.elements;
  1062. var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
  1063. v.x = te[0] * vx + te[4] * vy + te[8] * vz + te[12] * vw;
  1064. v.y = te[1] * vx + te[5] * vy + te[9] * vz + te[13] * vw;
  1065. v.z = te[2] * vx + te[6] * vy + te[10] * vz + te[14] * vw;
  1066. v.w = te[3] * vx + te[7] * vy + te[11] * vz + te[15] * vw;
  1067. return v;
  1068. },
  1069. rotateAxis: function ( v ) {
  1070. var te = this.elements;
  1071. var vx = v.x, vy = v.y, vz = v.z;
  1072. v.x = vx * te[0] + vy * te[4] + vz * te[8];
  1073. v.y = vx * te[1] + vy * te[5] + vz * te[9];
  1074. v.z = vx * te[2] + vy * te[6] + vz * te[10];
  1075. v.normalize();
  1076. return v;
  1077. },
  1078. crossVector: function ( a ) {
  1079. var te = this.elements;
  1080. var v = new THREE.Vector4();
  1081. v.x = te[0] * a.x + te[4] * a.y + te[8] * a.z + te[12] * a.w;
  1082. v.y = te[1] * a.x + te[5] * a.y + te[9] * a.z + te[13] * a.w;
  1083. v.z = te[2] * a.x + te[6] * a.y + te[10] * a.z + te[14] * a.w;
  1084. v.w = ( a.w ) ? te[3] * a.x + te[7] * a.y + te[11] * a.z + te[15] * a.w : 1;
  1085. return v;
  1086. },
  1087. determinant: function () {
  1088. var te = this.elements;
  1089. var n11 = te[0], n12 = te[4], n13 = te[8], n14 = te[12];
  1090. var n21 = te[1], n22 = te[5], n23 = te[9], n24 = te[13];
  1091. var n31 = te[2], n32 = te[6], n33 = te[10], n34 = te[14];
  1092. var n41 = te[3], n42 = te[7], n43 = te[11], n44 = te[15];
  1093. //TODO: make this more efficient
  1094. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  1095. return (
  1096. n14 * n23 * n32 * n41-
  1097. n13 * n24 * n32 * n41-
  1098. n14 * n22 * n33 * n41+
  1099. n12 * n24 * n33 * n41+
  1100. n13 * n22 * n34 * n41-
  1101. n12 * n23 * n34 * n41-
  1102. n14 * n23 * n31 * n42+
  1103. n13 * n24 * n31 * n42+
  1104. n14 * n21 * n33 * n42-
  1105. n11 * n24 * n33 * n42-
  1106. n13 * n21 * n34 * n42+
  1107. n11 * n23 * n34 * n42+
  1108. n14 * n22 * n31 * n43-
  1109. n12 * n24 * n31 * n43-
  1110. n14 * n21 * n32 * n43+
  1111. n11 * n24 * n32 * n43+
  1112. n12 * n21 * n34 * n43-
  1113. n11 * n22 * n34 * n43-
  1114. n13 * n22 * n31 * n44+
  1115. n12 * n23 * n31 * n44+
  1116. n13 * n21 * n32 * n44-
  1117. n11 * n23 * n32 * n44-
  1118. n12 * n21 * n33 * n44+
  1119. n11 * n22 * n33 * n44
  1120. );
  1121. },
  1122. transpose: function () {
  1123. var te = this.elements;
  1124. var tmp;
  1125. tmp = te[1]; te[1] = te[4]; te[4] = tmp;
  1126. tmp = te[2]; te[2] = te[8]; te[8] = tmp;
  1127. tmp = te[6]; te[6] = te[9]; te[9] = tmp;
  1128. tmp = te[3]; te[3] = te[12]; te[12] = tmp;
  1129. tmp = te[7]; te[7] = te[13]; te[13] = tmp;
  1130. tmp = te[11]; te[11] = te[14]; te[14] = tmp;
  1131. return this;
  1132. },
  1133. flattenToArray: function ( flat ) {
  1134. var te = this.elements;
  1135. flat[ 0 ] = te[0]; flat[ 1 ] = te[1]; flat[ 2 ] = te[2]; flat[ 3 ] = te[3];
  1136. flat[ 4 ] = te[4]; flat[ 5 ] = te[5]; flat[ 6 ] = te[6]; flat[ 7 ] = te[7];
  1137. flat[ 8 ] = te[8]; flat[ 9 ] = te[9]; flat[ 10 ] = te[10]; flat[ 11 ] = te[11];
  1138. flat[ 12 ] = te[12]; flat[ 13 ] = te[13]; flat[ 14 ] = te[14]; flat[ 15 ] = te[15];
  1139. return flat;
  1140. },
  1141. flattenToArrayOffset: function( flat, offset ) {
  1142. var te = this.elements;
  1143. flat[ offset ] = te[0];
  1144. flat[ offset + 1 ] = te[1];
  1145. flat[ offset + 2 ] = te[2];
  1146. flat[ offset + 3 ] = te[3];
  1147. flat[ offset + 4 ] = te[4];
  1148. flat[ offset + 5 ] = te[5];
  1149. flat[ offset + 6 ] = te[6];
  1150. flat[ offset + 7 ] = te[7];
  1151. flat[ offset + 8 ] = te[8];
  1152. flat[ offset + 9 ] = te[9];
  1153. flat[ offset + 10 ] = te[10];
  1154. flat[ offset + 11 ] = te[11];
  1155. flat[ offset + 12 ] = te[12];
  1156. flat[ offset + 13 ] = te[13];
  1157. flat[ offset + 14 ] = te[14];
  1158. flat[ offset + 15 ] = te[15];
  1159. return flat;
  1160. },
  1161. getPosition: function () {
  1162. var te = this.elements;
  1163. return THREE.Matrix4.__v1.set( te[12], te[13], te[14] );
  1164. },
  1165. setPosition: function ( v ) {
  1166. var te = this.elements;
  1167. te[12] = v.x;
  1168. te[13] = v.y;
  1169. te[14] = v.z;
  1170. return this;
  1171. },
  1172. getColumnX: function () {
  1173. var te = this.elements;
  1174. return THREE.Matrix4.__v1.set( te[0], te[1], te[2] );
  1175. },
  1176. getColumnY: function () {
  1177. var te = this.elements;
  1178. return THREE.Matrix4.__v1.set( te[4], te[5], te[6] );
  1179. },
  1180. getColumnZ: function() {
  1181. var te = this.elements;
  1182. return THREE.Matrix4.__v1.set( te[8], te[9], te[10] );
  1183. },
  1184. getInverse: function ( m ) {
  1185. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  1186. var te = this.elements;
  1187. var n11 = m.elements[0], n12 = m.elements[4], n13 = m.elements[8], n14 = m.elements[12];
  1188. var n21 = m.elements[1], n22 = m.elements[5], n23 = m.elements[9], n24 = m.elements[13];
  1189. var n31 = m.elements[2], n32 = m.elements[6], n33 = m.elements[10], n34 = m.elements[14];
  1190. var n41 = m.elements[3], n42 = m.elements[7], n43 = m.elements[11], n44 = m.elements[15];
  1191. te[0] = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  1192. te[4] = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  1193. te[8] = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  1194. te[12] = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  1195. te[1] = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  1196. te[5] = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  1197. te[9] = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  1198. te[13] = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  1199. te[2] = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  1200. te[6] = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  1201. te[10] = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  1202. te[14] = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  1203. te[3] = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  1204. te[7] = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  1205. te[11] = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  1206. te[15] = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  1207. this.multiplyScalar( 1 / m.determinant() );
  1208. return this;
  1209. },
  1210. setRotationFromEuler: function( v, order ) {
  1211. var te = this.elements;
  1212. var x = v.x, y = v.y, z = v.z;
  1213. var a = Math.cos( x ), b = Math.sin( x );
  1214. var c = Math.cos( y ), d = Math.sin( y );
  1215. var e = Math.cos( z ), f = Math.sin( z );
  1216. switch ( order ) {
  1217. case 'YXZ':
  1218. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  1219. te[0] = ce + df * b;
  1220. te[4] = de * b - cf;
  1221. te[8] = a * d;
  1222. te[1] = a * f;
  1223. te[5] = a * e;
  1224. te[9] = - b;
  1225. te[2] = cf * b - de;
  1226. te[6] = df + ce * b;
  1227. te[10] = a * c;
  1228. break;
  1229. case 'ZXY':
  1230. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  1231. te[0] = ce - df * b;
  1232. te[4] = - a * f;
  1233. te[8] = de + cf * b;
  1234. te[1] = cf + de * b;
  1235. te[5] = a * e;
  1236. te[9] = df - ce * b;
  1237. te[2] = - a * d;
  1238. te[6] = b;
  1239. te[10] = a * c;
  1240. break;
  1241. case 'ZYX':
  1242. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  1243. te[0] = c * e;
  1244. te[4] = be * d - af;
  1245. te[8] = ae * d + bf;
  1246. te[1] = c * f;
  1247. te[5] = bf * d + ae;
  1248. te[9] = af * d - be;
  1249. te[2] = - d;
  1250. te[6] = b * c;
  1251. te[10] = a * c;
  1252. break;
  1253. case 'YZX':
  1254. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  1255. te[0] = c * e;
  1256. te[4] = bd - ac * f;
  1257. te[8] = bc * f + ad;
  1258. te[1] = f;
  1259. te[5] = a * e;
  1260. te[9] = - b * e;
  1261. te[2] = - d * e;
  1262. te[6] = ad * f + bc;
  1263. te[10] = ac - bd * f;
  1264. break;
  1265. case 'XZY':
  1266. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  1267. te[0] = c * e;
  1268. te[4] = - f;
  1269. te[8] = d * e;
  1270. te[1] = ac * f + bd;
  1271. te[5] = a * e;
  1272. te[9] = ad * f - bc;
  1273. te[2] = bc * f - ad;
  1274. te[6] = b * e;
  1275. te[10] = bd * f + ac;
  1276. break;
  1277. default: // 'XYZ'
  1278. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  1279. te[0] = c * e;
  1280. te[4] = - c * f;
  1281. te[8] = d;
  1282. te[1] = af + be * d;
  1283. te[5] = ae - bf * d;
  1284. te[9] = - b * c;
  1285. te[2] = bf - ae * d;
  1286. te[6] = be + af * d;
  1287. te[10] = a * c;
  1288. break;
  1289. }
  1290. return this;
  1291. },
  1292. setRotationFromQuaternion: function( q ) {
  1293. var te = this.elements;
  1294. var x = q.x, y = q.y, z = q.z, w = q.w;
  1295. var x2 = x + x, y2 = y + y, z2 = z + z;
  1296. var xx = x * x2, xy = x * y2, xz = x * z2;
  1297. var yy = y * y2, yz = y * z2, zz = z * z2;
  1298. var wx = w * x2, wy = w * y2, wz = w * z2;
  1299. te[0] = 1 - ( yy + zz );
  1300. te[4] = xy - wz;
  1301. te[8] = xz + wy;
  1302. te[1] = xy + wz;
  1303. te[5] = 1 - ( xx + zz );
  1304. te[9] = yz - wx;
  1305. te[2] = xz - wy;
  1306. te[6] = yz + wx;
  1307. te[10] = 1 - ( xx + yy );
  1308. return this;
  1309. },
  1310. compose: function ( translation, rotation, scale ) {
  1311. var te = this.elements;
  1312. var mRotation = THREE.Matrix4.__m1;
  1313. var mScale = THREE.Matrix4.__m2;
  1314. mRotation.identity();
  1315. mRotation.setRotationFromQuaternion( rotation );
  1316. mScale.makeScale( scale.x, scale.y, scale.z );
  1317. this.multiply( mRotation, mScale );
  1318. te[12] = translation.x;
  1319. te[13] = translation.y;
  1320. te[14] = translation.z;
  1321. return this;
  1322. },
  1323. decompose: function ( translation, rotation, scale ) {
  1324. // grab the axis vectors
  1325. var te = this.elements;
  1326. var x = THREE.Matrix4.__v1;
  1327. var y = THREE.Matrix4.__v2;
  1328. var z = THREE.Matrix4.__v3;
  1329. x.set( te[0], te[1], te[2] );
  1330. y.set( te[4], te[5], te[6] );
  1331. z.set( te[8], te[9], te[10] );
  1332. translation = ( translation instanceof THREE.Vector3 ) ? translation : new THREE.Vector3();
  1333. rotation = ( rotation instanceof THREE.Quaternion ) ? rotation : new THREE.Quaternion();
  1334. scale = ( scale instanceof THREE.Vector3 ) ? scale : new THREE.Vector3();
  1335. scale.x = x.length();
  1336. scale.y = y.length();
  1337. scale.z = z.length();
  1338. translation.x = te[12];
  1339. translation.y = te[13];
  1340. translation.z = te[14];
  1341. // scale the rotation part
  1342. var matrix = THREE.Matrix4.__m1;
  1343. matrix.copy( this );
  1344. matrix.elements[0] /= scale.x;
  1345. matrix.elements[1] /= scale.x;
  1346. matrix.elements[2] /= scale.x;
  1347. matrix.elements[4] /= scale.y;
  1348. matrix.elements[5] /= scale.y;
  1349. matrix.elements[6] /= scale.y;
  1350. matrix.elements[8] /= scale.z;
  1351. matrix.elements[9] /= scale.z;
  1352. matrix.elements[10] /= scale.z;
  1353. rotation.setFromRotationMatrix( matrix );
  1354. return [ translation, rotation, scale ];
  1355. },
  1356. extractPosition: function ( m ) {
  1357. var te = this.elements;
  1358. te[12] = m.elements[12];
  1359. te[13] = m.elements[13];
  1360. te[14] = m.elements[14];
  1361. return this;
  1362. },
  1363. extractRotation: function ( m ) {
  1364. var te = this.elements;
  1365. var vector = THREE.Matrix4.__v1;
  1366. var scaleX = 1 / vector.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  1367. var scaleY = 1 / vector.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  1368. var scaleZ = 1 / vector.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  1369. te[0] = m.elements[0] * scaleX;
  1370. te[1] = m.elements[1] * scaleX;
  1371. te[2] = m.elements[2] * scaleX;
  1372. te[4] = m.elements[4] * scaleY;
  1373. te[5] = m.elements[5] * scaleY;
  1374. te[6] = m.elements[6] * scaleY;
  1375. te[8] = m.elements[8] * scaleZ;
  1376. te[9] = m.elements[9] * scaleZ;
  1377. te[10] = m.elements[10] * scaleZ;
  1378. return this;
  1379. },
  1380. //
  1381. translate: function ( v ) {
  1382. var te = this.elements;
  1383. var x = v.x, y = v.y, z = v.z;
  1384. te[12] = te[0] * x + te[4] * y + te[8] * z + te[12];
  1385. te[13] = te[1] * x + te[5] * y + te[9] * z + te[13];
  1386. te[14] = te[2] * x + te[6] * y + te[10] * z + te[14];
  1387. te[15] = te[3] * x + te[7] * y + te[11] * z + te[15];
  1388. return this;
  1389. },
  1390. rotateX: function ( angle ) {
  1391. var te = this.elements;
  1392. var m12 = te[4];
  1393. var m22 = te[5];
  1394. var m32 = te[6];
  1395. var m42 = te[7];
  1396. var m13 = te[8];
  1397. var m23 = te[9];
  1398. var m33 = te[10];
  1399. var m43 = te[11];
  1400. var c = Math.cos( angle );
  1401. var s = Math.sin( angle );
  1402. te[4] = c * m12 + s * m13;
  1403. te[5] = c * m22 + s * m23;
  1404. te[6] = c * m32 + s * m33;
  1405. te[7] = c * m42 + s * m43;
  1406. te[8] = c * m13 - s * m12;
  1407. te[9] = c * m23 - s * m22;
  1408. te[10] = c * m33 - s * m32;
  1409. te[11] = c * m43 - s * m42;
  1410. return this;
  1411. },
  1412. rotateY: function ( angle ) {
  1413. var te = this.elements;
  1414. var m11 = te[0];
  1415. var m21 = te[1];
  1416. var m31 = te[2];
  1417. var m41 = te[3];
  1418. var m13 = te[8];
  1419. var m23 = te[9];
  1420. var m33 = te[10];
  1421. var m43 = te[11];
  1422. var c = Math.cos( angle );
  1423. var s = Math.sin( angle );
  1424. te[0] = c * m11 - s * m13;
  1425. te[1] = c * m21 - s * m23;
  1426. te[2] = c * m31 - s * m33;
  1427. te[3] = c * m41 - s * m43;
  1428. te[8] = c * m13 + s * m11;
  1429. te[9] = c * m23 + s * m21;
  1430. te[10] = c * m33 + s * m31;
  1431. te[11] = c * m43 + s * m41;
  1432. return this;
  1433. },
  1434. rotateZ: function ( angle ) {
  1435. var te = this.elements;
  1436. var m11 = te[0];
  1437. var m21 = te[1];
  1438. var m31 = te[2];
  1439. var m41 = te[3];
  1440. var m12 = te[4];
  1441. var m22 = te[5];
  1442. var m32 = te[6];
  1443. var m42 = te[7];
  1444. var c = Math.cos( angle );
  1445. var s = Math.sin( angle );
  1446. te[0] = c * m11 + s * m12;
  1447. te[1] = c * m21 + s * m22;
  1448. te[2] = c * m31 + s * m32;
  1449. te[3] = c * m41 + s * m42;
  1450. te[4] = c * m12 - s * m11;
  1451. te[5] = c * m22 - s * m21;
  1452. te[6] = c * m32 - s * m31;
  1453. te[7] = c * m42 - s * m41;
  1454. return this;
  1455. },
  1456. rotateByAxis: function ( axis, angle ) {
  1457. var te = this.elements;
  1458. // optimize by checking axis
  1459. if ( axis.x === 1 && axis.y === 0 && axis.z === 0 ) {
  1460. return this.rotateX( angle );
  1461. } else if ( axis.x === 0 && axis.y === 1 && axis.z === 0 ) {
  1462. return this.rotateY( angle );
  1463. } else if ( axis.x === 0 && axis.y === 0 && axis.z === 1 ) {
  1464. return this.rotateZ( angle );
  1465. }
  1466. var x = axis.x, y = axis.y, z = axis.z;
  1467. var n = Math.sqrt(x * x + y * y + z * z);
  1468. x /= n;
  1469. y /= n;
  1470. z /= n;
  1471. var xx = x * x, yy = y * y, zz = z * z;
  1472. var c = Math.cos( angle );
  1473. var s = Math.sin( angle );
  1474. var oneMinusCosine = 1 - c;
  1475. var xy = x * y * oneMinusCosine;
  1476. var xz = x * z * oneMinusCosine;
  1477. var yz = y * z * oneMinusCosine;
  1478. var xs = x * s;
  1479. var ys = y * s;
  1480. var zs = z * s;
  1481. var r11 = xx + (1 - xx) * c;
  1482. var r21 = xy + zs;
  1483. var r31 = xz - ys;
  1484. var r12 = xy - zs;
  1485. var r22 = yy + (1 - yy) * c;
  1486. var r32 = yz + xs;
  1487. var r13 = xz + ys;
  1488. var r23 = yz - xs;
  1489. var r33 = zz + (1 - zz) * c;
  1490. var m11 = te[0], m21 = te[1], m31 = te[2], m41 = te[3];
  1491. var m12 = te[4], m22 = te[5], m32 = te[6], m42 = te[7];
  1492. var m13 = te[8], m23 = te[9], m33 = te[10], m43 = te[11];
  1493. var m14 = te[12], m24 = te[13], m34 = te[14], m44 = te[15];
  1494. te[0] = r11 * m11 + r21 * m12 + r31 * m13;
  1495. te[1] = r11 * m21 + r21 * m22 + r31 * m23;
  1496. te[2] = r11 * m31 + r21 * m32 + r31 * m33;
  1497. te[3] = r11 * m41 + r21 * m42 + r31 * m43;
  1498. te[4] = r12 * m11 + r22 * m12 + r32 * m13;
  1499. te[5] = r12 * m21 + r22 * m22 + r32 * m23;
  1500. te[6] = r12 * m31 + r22 * m32 + r32 * m33;
  1501. te[7] = r12 * m41 + r22 * m42 + r32 * m43;
  1502. te[8] = r13 * m11 + r23 * m12 + r33 * m13;
  1503. te[9] = r13 * m21 + r23 * m22 + r33 * m23;
  1504. te[10] = r13 * m31 + r23 * m32 + r33 * m33;
  1505. te[11] = r13 * m41 + r23 * m42 + r33 * m43;
  1506. return this;
  1507. },
  1508. scale: function ( v ) {
  1509. var te = this.elements;
  1510. var x = v.x, y = v.y, z = v.z;
  1511. te[0] *= x; te[4] *= y; te[8] *= z;
  1512. te[1] *= x; te[5] *= y; te[9] *= z;
  1513. te[2] *= x; te[6] *= y; te[10] *= z;
  1514. te[3] *= x; te[7] *= y; te[11] *= z;
  1515. return this;
  1516. },
  1517. //
  1518. makeTranslation: function ( x, y, z ) {
  1519. this.set(
  1520. 1, 0, 0, x,
  1521. 0, 1, 0, y,
  1522. 0, 0, 1, z,
  1523. 0, 0, 0, 1
  1524. );
  1525. return this;
  1526. },
  1527. makeRotationX: function ( theta ) {
  1528. var c = Math.cos( theta ), s = Math.sin( theta );
  1529. this.set(
  1530. 1, 0, 0, 0,
  1531. 0, c, -s, 0,
  1532. 0, s, c, 0,
  1533. 0, 0, 0, 1
  1534. );
  1535. return this;
  1536. },
  1537. makeRotationY: function ( theta ) {
  1538. var c = Math.cos( theta ), s = Math.sin( theta );
  1539. this.set(
  1540. c, 0, s, 0,
  1541. 0, 1, 0, 0,
  1542. -s, 0, c, 0,
  1543. 0, 0, 0, 1
  1544. );
  1545. return this;
  1546. },
  1547. makeRotationZ: function ( theta ) {
  1548. var c = Math.cos( theta ), s = Math.sin( theta );
  1549. this.set(
  1550. c, -s, 0, 0,
  1551. s, c, 0, 0,
  1552. 0, 0, 1, 0,
  1553. 0, 0, 0, 1
  1554. );
  1555. return this;
  1556. },
  1557. makeRotationAxis: function ( axis, angle ) {
  1558. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  1559. var c = Math.cos( angle );
  1560. var s = Math.sin( angle );
  1561. var t = 1 - c;
  1562. var x = axis.x, y = axis.y, z = axis.z;
  1563. var tx = t * x, ty = t * y;
  1564. this.set(
  1565. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  1566. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  1567. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  1568. 0, 0, 0, 1
  1569. );
  1570. return this;
  1571. },
  1572. makeScale: function ( x, y, z ) {
  1573. this.set(
  1574. x, 0, 0, 0,
  1575. 0, y, 0, 0,
  1576. 0, 0, z, 0,
  1577. 0, 0, 0, 1
  1578. );
  1579. return this;
  1580. },
  1581. makeFrustum: function ( left, right, bottom, top, near, far ) {
  1582. var te = this.elements;
  1583. var x = 2 * near / ( right - left );
  1584. var y = 2 * near / ( top - bottom );
  1585. var a = ( right + left ) / ( right - left );
  1586. var b = ( top + bottom ) / ( top - bottom );
  1587. var c = - ( far + near ) / ( far - near );
  1588. var d = - 2 * far * near / ( far - near );
  1589. te[0] = x; te[4] = 0; te[8] = a; te[12] = 0;
  1590. te[1] = 0; te[5] = y; te[9] = b; te[13] = 0;
  1591. te[2] = 0; te[6] = 0; te[10] = c; te[14] = d;
  1592. te[3] = 0; te[7] = 0; te[11] = - 1; te[15] = 0;
  1593. return this;
  1594. },
  1595. makePerspective: function ( fov, aspect, near, far ) {
  1596. var ymax = near * Math.tan( fov * Math.PI / 360 );
  1597. var ymin = - ymax;
  1598. var xmin = ymin * aspect;
  1599. var xmax = ymax * aspect;
  1600. return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  1601. },
  1602. makeOrthographic: function ( left, right, top, bottom, near, far ) {
  1603. var te = this.elements;
  1604. var w = right - left;
  1605. var h = top - bottom;
  1606. var p = far - near;
  1607. var x = ( right + left ) / w;
  1608. var y = ( top + bottom ) / h;
  1609. var z = ( far + near ) / p;
  1610. te[0] = 2 / w; te[4] = 0; te[8] = 0; te[12] = -x;
  1611. te[1] = 0; te[5] = 2 / h; te[9] = 0; te[13] = -y;
  1612. te[2] = 0; te[6] = 0; te[10] = -2 / p; te[14] = -z;
  1613. te[3] = 0; te[7] = 0; te[11] = 0; te[15] = 1;
  1614. return this;
  1615. },
  1616. clone: function () {
  1617. var te = this.elements;
  1618. return new THREE.Matrix4(
  1619. te[0], te[4], te[8], te[12],
  1620. te[1], te[5], te[9], te[13],
  1621. te[2], te[6], te[10], te[14],
  1622. te[3], te[7], te[11], te[15]
  1623. );
  1624. }
  1625. };
  1626. THREE.Matrix4.__v1 = new THREE.Vector3();
  1627. THREE.Matrix4.__v2 = new THREE.Vector3();
  1628. THREE.Matrix4.__v3 = new THREE.Vector3();
  1629. THREE.Matrix4.__m1 = new THREE.Matrix4();
  1630. THREE.Matrix4.__m2 = new THREE.Matrix4();
  1631. /**
  1632. * @author mr.doob / http://mrdoob.com/
  1633. * @author mikael emtinger / http://gomo.se/
  1634. * @author alteredq / http://alteredqualia.com/
  1635. */
  1636. THREE.Object3D = function () {
  1637. this.id = THREE.Object3DCount ++;
  1638. this.name = '';
  1639. this.parent = undefined;
  1640. this.children = [];
  1641. this.up = new THREE.Vector3( 0, 1, 0 );
  1642. this.position = new THREE.Vector3();
  1643. this.rotation = new THREE.Vector3();
  1644. this.eulerOrder = 'XYZ';
  1645. this.scale = new THREE.Vector3( 1, 1, 1 );
  1646. this.doubleSided = false;
  1647. this.flipSided = false;
  1648. this.renderDepth = null;
  1649. this.rotationAutoUpdate = true;
  1650. this.matrix = new THREE.Matrix4();
  1651. this.matrixWorld = new THREE.Matrix4();
  1652. this.matrixRotationWorld = new THREE.Matrix4();
  1653. this.matrixAutoUpdate = true;
  1654. this.matrixWorldNeedsUpdate = true;
  1655. this.quaternion = new THREE.Quaternion();
  1656. this.useQuaternion = false;
  1657. this.boundRadius = 0.0;
  1658. this.boundRadiusScale = 1.0;
  1659. this.visible = true;
  1660. this.castShadow = false;
  1661. this.receiveShadow = false;
  1662. this.frustumCulled = true;
  1663. this._vector = new THREE.Vector3();
  1664. };
  1665. THREE.Object3D.prototype = {
  1666. constructor: THREE.Object3D,
  1667. applyMatrix: function ( matrix ) {
  1668. this.matrix.multiply( matrix, this.matrix );
  1669. this.scale.getScaleFromMatrix( this.matrix );
  1670. this.rotation.getRotationFromMatrix( this.matrix, this.scale );
  1671. this.position.getPositionFromMatrix( this.matrix );
  1672. },
  1673. translate: function ( distance, axis ) {
  1674. this.matrix.rotateAxis( axis );
  1675. this.position.addSelf( axis.multiplyScalar( distance ) );
  1676. },
  1677. translateX: function ( distance ) {
  1678. this.translate( distance, this._vector.set( 1, 0, 0 ) );
  1679. },
  1680. translateY: function ( distance ) {
  1681. this.translate( distance, this._vector.set( 0, 1, 0 ) );
  1682. },
  1683. translateZ: function ( distance ) {
  1684. this.translate( distance, this._vector.set( 0, 0, 1 ) );
  1685. },
  1686. lookAt: function ( vector ) {
  1687. // TODO: Add hierarchy support.
  1688. this.matrix.lookAt( vector, this.position, this.up );
  1689. if ( this.rotationAutoUpdate ) {
  1690. this.rotation.getRotationFromMatrix( this.matrix );
  1691. }
  1692. },
  1693. add: function ( object ) {
  1694. if ( object === this ) {
  1695. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  1696. return;
  1697. }
  1698. if ( this.children.indexOf( object ) === - 1 ) {
  1699. if ( object.parent !== undefined ) {
  1700. object.parent.remove( object );
  1701. }
  1702. object.parent = this;
  1703. this.children.push( object );
  1704. // add to scene
  1705. var scene = this;
  1706. while ( scene.parent !== undefined ) {
  1707. scene = scene.parent;
  1708. }
  1709. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  1710. scene.__addObject( object );
  1711. }
  1712. }
  1713. },
  1714. remove: function ( object ) {
  1715. var index = this.children.indexOf( object );
  1716. if ( index !== - 1 ) {
  1717. object.parent = undefined;
  1718. this.children.splice( index, 1 );
  1719. // remove from scene
  1720. var scene = this;
  1721. while ( scene.parent !== undefined ) {
  1722. scene = scene.parent;
  1723. }
  1724. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  1725. scene.__removeObject( object );
  1726. }
  1727. }
  1728. },
  1729. getChildByName: function ( name, recursive ) {
  1730. var c, cl, child;
  1731. for ( c = 0, cl = this.children.length; c < cl; c ++ ) {
  1732. child = this.children[ c ];
  1733. if ( child.name === name ) {
  1734. return child;
  1735. }
  1736. if ( recursive ) {
  1737. child = child.getChildByName( name, recursive );
  1738. if ( child !== undefined ) {
  1739. return child;
  1740. }
  1741. }
  1742. }
  1743. return undefined;
  1744. },
  1745. updateMatrix: function () {
  1746. this.matrix.setPosition( this.position );
  1747. if ( this.useQuaternion ) {
  1748. this.matrix.setRotationFromQuaternion( this.quaternion );
  1749. } else {
  1750. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  1751. }
  1752. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  1753. this.matrix.scale( this.scale );
  1754. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  1755. }
  1756. this.matrixWorldNeedsUpdate = true;
  1757. },
  1758. updateMatrixWorld: function ( force ) {
  1759. this.matrixAutoUpdate && this.updateMatrix();
  1760. // update matrixWorld
  1761. if ( this.matrixWorldNeedsUpdate || force ) {
  1762. if ( this.parent ) {
  1763. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  1764. } else {
  1765. this.matrixWorld.copy( this.matrix );
  1766. }
  1767. this.matrixWorldNeedsUpdate = false;
  1768. force = true;
  1769. }
  1770. // update children
  1771. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  1772. this.children[ i ].updateMatrixWorld( force );
  1773. }
  1774. }
  1775. };
  1776. THREE.Object3DCount = 0;
  1777. /**
  1778. * @author mr.doob / http://mrdoob.com/
  1779. * @author supereggbert / http://www.paulbrunt.co.uk/
  1780. * @author julianwa / https://github.com/julianwa
  1781. */
  1782. THREE.Projector = function() {
  1783. var _object, _objectCount, _objectPool = [],
  1784. _vertex, _vertexCount, _vertexPool = [],
  1785. _face, _face3Count, _face3Pool = [], _face4Count, _face4Pool = [],
  1786. _line, _lineCount, _linePool = [],
  1787. _particle, _particleCount, _particlePool = [],
  1788. _renderData = { objects: [], sprites: [], lights: [], elements: [] },
  1789. _vector3 = new THREE.Vector3(),
  1790. _vector4 = new THREE.Vector4(),
  1791. _projScreenMatrix = new THREE.Matrix4(),
  1792. _projScreenobjectMatrixWorld = new THREE.Matrix4(),
  1793. _frustum = new THREE.Frustum(),
  1794. _clippedVertex1PositionScreen = new THREE.Vector4(),
  1795. _clippedVertex2PositionScreen = new THREE.Vector4(),
  1796. _face3VertexNormals;
  1797. this.projectVector = function ( vector, camera ) {
  1798. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  1799. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  1800. _projScreenMatrix.multiplyVector3( vector );
  1801. return vector;
  1802. };
  1803. this.unprojectVector = function ( vector, camera ) {
  1804. camera.projectionMatrixInverse.getInverse( camera.projectionMatrix );
  1805. _projScreenMatrix.multiply( camera.matrixWorld, camera.projectionMatrixInverse );
  1806. _projScreenMatrix.multiplyVector3( vector );
  1807. return vector;
  1808. };
  1809. this.pickingRay = function ( vector, camera ) {
  1810. var end, ray, t;
  1811. // set two vectors with opposing z values
  1812. vector.z = -1.0;
  1813. end = new THREE.Vector3( vector.x, vector.y, 1.0 );
  1814. this.unprojectVector( vector, camera );
  1815. this.unprojectVector( end, camera );
  1816. // find direction from vector to end
  1817. end.subSelf( vector ).normalize();
  1818. return new THREE.Ray( vector, end );
  1819. };
  1820. this.projectGraph = function ( root, sort ) {
  1821. _objectCount = 0;
  1822. _renderData.objects.length = 0;
  1823. _renderData.sprites.length = 0;
  1824. _renderData.lights.length = 0;
  1825. var projectObject = function ( object ) {
  1826. if ( object.visible === false ) return;
  1827. if ( ( object instanceof THREE.Mesh || object instanceof THREE.Line ) &&
  1828. ( object.frustumCulled === false || _frustum.contains( object ) ) ) {
  1829. _vector3.copy( object.matrixWorld.getPosition() );
  1830. _projScreenMatrix.multiplyVector3( _vector3 );
  1831. _object = getNextObjectInPool();
  1832. _object.object = object;
  1833. _object.z = _vector3.z;
  1834. _renderData.objects.push( _object );
  1835. } else if ( object instanceof THREE.Sprite || object instanceof THREE.Particle ) {
  1836. _vector3.copy( object.matrixWorld.getPosition() );
  1837. _projScreenMatrix.multiplyVector3( _vector3 );
  1838. _object = getNextObjectInPool();
  1839. _object.object = object;
  1840. _object.z = _vector3.z;
  1841. _renderData.sprites.push( _object );
  1842. } else if ( object instanceof THREE.Light ) {
  1843. _renderData.lights.push( object );
  1844. }
  1845. for ( var c = 0, cl = object.children.length; c < cl; c ++ ) {
  1846. projectObject( object.children[ c ] );
  1847. }
  1848. };
  1849. projectObject( root );
  1850. sort && _renderData.objects.sort( painterSort );
  1851. return _renderData;
  1852. };
  1853. this.projectScene = function ( scene, camera, sort ) {
  1854. var near = camera.near, far = camera.far, visible = false,
  1855. o, ol, v, vl, f, fl, n, nl, c, cl, u, ul, object,
  1856. objectMatrixWorld, objectMatrixWorldRotation,
  1857. geometry, geometryMaterials, vertices, vertex, vertexPositionScreen,
  1858. faces, face, faceVertexNormals, normal, faceVertexUvs, uvs,
  1859. v1, v2, v3, v4;
  1860. _face3Count = 0;
  1861. _face4Count = 0;
  1862. _lineCount = 0;
  1863. _particleCount = 0;
  1864. _renderData.elements.length = 0;
  1865. if ( camera.parent === undefined ) {
  1866. console.warn( 'DEPRECATED: Camera hasn\'t been added to a Scene. Adding it...' );
  1867. scene.add( camera );
  1868. }
  1869. scene.updateMatrixWorld();
  1870. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  1871. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  1872. _frustum.setFromMatrix( _projScreenMatrix );
  1873. _renderData = this.projectGraph( scene, false );
  1874. for ( o = 0, ol = _renderData.objects.length; o < ol; o++ ) {
  1875. object = _renderData.objects[ o ].object;
  1876. objectMatrixWorld = object.matrixWorld;
  1877. _vertexCount = 0;
  1878. if ( object instanceof THREE.Mesh ) {
  1879. geometry = object.geometry;
  1880. geometryMaterials = object.geometry.materials;
  1881. vertices = geometry.vertices;
  1882. faces = geometry.faces;
  1883. faceVertexUvs = geometry.faceVertexUvs;
  1884. objectMatrixWorldRotation = object.matrixRotationWorld.extractRotation( objectMatrixWorld );
  1885. for ( v = 0, vl = vertices.length; v < vl; v ++ ) {
  1886. _vertex = getNextVertexInPool();
  1887. _vertex.positionWorld.copy( vertices[ v ].position );
  1888. objectMatrixWorld.multiplyVector3( _vertex.positionWorld );
  1889. _vertex.positionScreen.copy( _vertex.positionWorld );
  1890. _projScreenMatrix.multiplyVector4( _vertex.positionScreen );
  1891. _vertex.positionScreen.x /= _vertex.positionScreen.w;
  1892. _vertex.positionScreen.y /= _vertex.positionScreen.w;
  1893. _vertex.visible = _vertex.positionScreen.z > near && _vertex.positionScreen.z < far;
  1894. }
  1895. for ( f = 0, fl = faces.length; f < fl; f ++ ) {
  1896. face = faces[ f ];
  1897. if ( face instanceof THREE.Face3 ) {
  1898. v1 = _vertexPool[ face.a ];
  1899. v2 = _vertexPool[ face.b ];
  1900. v3 = _vertexPool[ face.c ];
  1901. if ( v1.visible && v2.visible && v3.visible ) {
  1902. visible = ( ( v3.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  1903. ( v3.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) ) < 0;
  1904. if ( object.doubleSided || visible != object.flipSided ) {
  1905. _face = getNextFace3InPool();
  1906. _face.v1.copy( v1 );
  1907. _face.v2.copy( v2 );
  1908. _face.v3.copy( v3 );
  1909. } else {
  1910. continue;
  1911. }
  1912. } else {
  1913. continue;
  1914. }
  1915. } else if ( face instanceof THREE.Face4 ) {
  1916. v1 = _vertexPool[ face.a ];
  1917. v2 = _vertexPool[ face.b ];
  1918. v3 = _vertexPool[ face.c ];
  1919. v4 = _vertexPool[ face.d ];
  1920. if ( v1.visible && v2.visible && v3.visible && v4.visible ) {
  1921. visible = ( v4.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  1922. ( v4.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) < 0 ||
  1923. ( v2.positionScreen.x - v3.positionScreen.x ) * ( v4.positionScreen.y - v3.positionScreen.y ) -
  1924. ( v2.positionScreen.y - v3.positionScreen.y ) * ( v4.positionScreen.x - v3.positionScreen.x ) < 0;
  1925. if ( object.doubleSided || visible != object.flipSided ) {
  1926. _face = getNextFace4InPool();
  1927. _face.v1.copy( v1 );
  1928. _face.v2.copy( v2 );
  1929. _face.v3.copy( v3 );
  1930. _face.v4.copy( v4 );
  1931. } else {
  1932. continue;
  1933. }
  1934. } else {
  1935. continue;
  1936. }
  1937. }
  1938. _face.normalWorld.copy( face.normal );
  1939. if ( !visible && ( object.flipSided || object.doubleSided ) ) _face.normalWorld.negate();
  1940. objectMatrixWorldRotation.multiplyVector3( _face.normalWorld );
  1941. _face.centroidWorld.copy( face.centroid );
  1942. objectMatrixWorld.multiplyVector3( _face.centroidWorld );
  1943. _face.centroidScreen.copy( _face.centroidWorld );
  1944. _projScreenMatrix.multiplyVector3( _face.centroidScreen );
  1945. faceVertexNormals = face.vertexNormals;
  1946. for ( n = 0, nl = faceVertexNormals.length; n < nl; n ++ ) {
  1947. normal = _face.vertexNormalsWorld[ n ];
  1948. normal.copy( faceVertexNormals[ n ] );
  1949. if ( !visible && ( object.flipSided || object.doubleSided ) ) normal.negate();
  1950. objectMatrixWorldRotation.multiplyVector3( normal );
  1951. }
  1952. for ( c = 0, cl = faceVertexUvs.length; c < cl; c ++ ) {
  1953. uvs = faceVertexUvs[ c ][ f ];
  1954. if ( !uvs ) continue;
  1955. for ( u = 0, ul = uvs.length; u < ul; u ++ ) {
  1956. _face.uvs[ c ][ u ] = uvs[ u ];
  1957. }
  1958. }
  1959. _face.material = object.material;
  1960. _face.faceMaterial = face.materialIndex !== null ? geometryMaterials[ face.materialIndex ] : null;
  1961. _face.z = _face.centroidScreen.z;
  1962. _renderData.elements.push( _face );
  1963. }
  1964. } else if ( object instanceof THREE.Line ) {
  1965. _projScreenobjectMatrixWorld.multiply( _projScreenMatrix, objectMatrixWorld );
  1966. vertices = object.geometry.vertices;
  1967. v1 = getNextVertexInPool();
  1968. v1.positionScreen.copy( vertices[ 0 ].position );
  1969. _projScreenobjectMatrixWorld.multiplyVector4( v1.positionScreen );
  1970. // Handle LineStrip and LinePieces
  1971. var step = object.type === THREE.LinePieces ? 2 : 1;
  1972. for ( v = 1, vl = vertices.length; v < vl; v ++ ) {
  1973. v1 = getNextVertexInPool();
  1974. v1.positionScreen.copy( vertices[ v ].position );
  1975. _projScreenobjectMatrixWorld.multiplyVector4( v1.positionScreen );
  1976. if ( ( v + 1 ) % step > 0 ) continue;
  1977. v2 = _vertexPool[ _vertexCount - 2 ];
  1978. _clippedVertex1PositionScreen.copy( v1.positionScreen );
  1979. _clippedVertex2PositionScreen.copy( v2.positionScreen );
  1980. if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) ) {
  1981. // Perform the perspective divide
  1982. _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
  1983. _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
  1984. _line = getNextLineInPool();
  1985. _line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
  1986. _line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
  1987. _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
  1988. _line.material = object.material;
  1989. _renderData.elements.push( _line );
  1990. }
  1991. }
  1992. }
  1993. }
  1994. for ( o = 0, ol = _renderData.sprites.length; o < ol; o++ ) {
  1995. object = _renderData.sprites[ o ].object;
  1996. objectMatrixWorld = object.matrixWorld;
  1997. if ( object instanceof THREE.Particle ) {
  1998. _vector4.set( objectMatrixWorld.elements[12], objectMatrixWorld.elements[13], objectMatrixWorld.elements[14], 1 );
  1999. _projScreenMatrix.multiplyVector4( _vector4 );
  2000. _vector4.z /= _vector4.w;
  2001. if ( _vector4.z > 0 && _vector4.z < 1 ) {
  2002. _particle = getNextParticleInPool();
  2003. _particle.x = _vector4.x / _vector4.w;
  2004. _particle.y = _vector4.y / _vector4.w;
  2005. _particle.z = _vector4.z;
  2006. _particle.rotation = object.rotation.z;
  2007. _particle.scale.x = object.scale.x * Math.abs( _particle.x - ( _vector4.x + camera.projectionMatrix.elements[0] ) / ( _vector4.w + camera.projectionMatrix.elements[12] ) );
  2008. _particle.scale.y = object.scale.y * Math.abs( _particle.y - ( _vector4.y + camera.projectionMatrix.elements[5] ) / ( _vector4.w + camera.projectionMatrix.elements[13] ) );
  2009. _particle.material = object.material;
  2010. _renderData.elements.push( _particle );
  2011. }
  2012. }
  2013. }
  2014. sort && _renderData.elements.sort( painterSort );
  2015. return _renderData;
  2016. };
  2017. // Pools
  2018. function getNextObjectInPool() {
  2019. var object = _objectPool[ _objectCount ] = _objectPool[ _objectCount ] || new THREE.RenderableObject();
  2020. _objectCount ++;
  2021. return object;
  2022. }
  2023. function getNextVertexInPool() {
  2024. var vertex = _vertexPool[ _vertexCount ] = _vertexPool[ _vertexCount ] || new THREE.RenderableVertex();
  2025. _vertexCount ++;
  2026. return vertex;
  2027. }
  2028. function getNextFace3InPool() {
  2029. var face = _face3Pool[ _face3Count ] = _face3Pool[ _face3Count ] || new THREE.RenderableFace3();
  2030. _face3Count ++;
  2031. return face;
  2032. }
  2033. function getNextFace4InPool() {
  2034. var face = _face4Pool[ _face4Count ] = _face4Pool[ _face4Count ] || new THREE.RenderableFace4();
  2035. _face4Count ++;
  2036. return face;
  2037. }
  2038. function getNextLineInPool() {
  2039. var line = _linePool[ _lineCount ] = _linePool[ _lineCount ] || new THREE.RenderableLine();
  2040. _lineCount ++;
  2041. return line;
  2042. }
  2043. function getNextParticleInPool() {
  2044. var particle = _particlePool[ _particleCount ] = _particlePool[ _particleCount ] || new THREE.RenderableParticle();
  2045. _particleCount ++;
  2046. return particle;
  2047. }
  2048. //
  2049. function painterSort( a, b ) {
  2050. return b.z - a.z;
  2051. }
  2052. function clipLine( s1, s2 ) {
  2053. var alpha1 = 0, alpha2 = 1,
  2054. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  2055. // Z = -1 and Z = +1, respectively.
  2056. bc1near = s1.z + s1.w,
  2057. bc2near = s2.z + s2.w,
  2058. bc1far = - s1.z + s1.w,
  2059. bc2far = - s2.z + s2.w;
  2060. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  2061. // Both vertices lie entirely within all clip planes.
  2062. return true;
  2063. } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {
  2064. // Both vertices lie entirely outside one of the clip planes.
  2065. return false;
  2066. } else {
  2067. // The line segment spans at least one clip plane.
  2068. if ( bc1near < 0 ) {
  2069. // v1 lies outside the near plane, v2 inside
  2070. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  2071. } else if ( bc2near < 0 ) {
  2072. // v2 lies outside the near plane, v1 inside
  2073. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  2074. }
  2075. if ( bc1far < 0 ) {
  2076. // v1 lies outside the far plane, v2 inside
  2077. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  2078. } else if ( bc2far < 0 ) {
  2079. // v2 lies outside the far plane, v2 inside
  2080. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  2081. }
  2082. if ( alpha2 < alpha1 ) {
  2083. // The line segment spans two boundaries, but is outside both of them.
  2084. // (This can't happen when we're only clipping against just near/far but good
  2085. // to leave the check here for future usage if other clip planes are added.)
  2086. return false;
  2087. } else {
  2088. // Update the s1 and s2 vertices to match the clipped line segment.
  2089. s1.lerpSelf( s2, alpha1 );
  2090. s2.lerpSelf( s1, 1 - alpha2 );
  2091. return true;
  2092. }
  2093. }
  2094. }
  2095. };
  2096. /**
  2097. * @author mikael emtinger / http://gomo.se/
  2098. * @author alteredq / http://alteredqualia.com/
  2099. */
  2100. THREE.Quaternion = function( x, y, z, w ) {
  2101. this.x = x || 0;
  2102. this.y = y || 0;
  2103. this.z = z || 0;
  2104. this.w = ( w !== undefined ) ? w : 1;
  2105. };
  2106. THREE.Quaternion.prototype = {
  2107. constructor: THREE.Quaternion,
  2108. set: function ( x, y, z, w ) {
  2109. this.x = x;
  2110. this.y = y;
  2111. this.z = z;
  2112. this.w = w;
  2113. return this;
  2114. },
  2115. copy: function ( q ) {
  2116. this.x = q.x;
  2117. this.y = q.y;
  2118. this.z = q.z;
  2119. this.w = q.w;
  2120. return this;
  2121. },
  2122. setFromEuler: function ( vector ) {
  2123. var c = Math.PI / 360, // 0.5 * Math.PI / 360, // 0.5 is an optimization
  2124. x = vector.x * c,
  2125. y = vector.y * c,
  2126. z = vector.z * c,
  2127. c1 = Math.cos( y ),
  2128. s1 = Math.sin( y ),
  2129. c2 = Math.cos( -z ),
  2130. s2 = Math.sin( -z ),
  2131. c3 = Math.cos( x ),
  2132. s3 = Math.sin( x ),
  2133. c1c2 = c1 * c2,
  2134. s1s2 = s1 * s2;
  2135. this.w = c1c2 * c3 - s1s2 * s3;
  2136. this.x = c1c2 * s3 + s1s2 * c3;
  2137. this.y = s1 * c2 * c3 + c1 * s2 * s3;
  2138. this.z = c1 * s2 * c3 - s1 * c2 * s3;
  2139. return this;
  2140. },
  2141. setFromAxisAngle: function ( axis, angle ) {
  2142. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  2143. // axis have to be normalized
  2144. var halfAngle = angle / 2,
  2145. s = Math.sin( halfAngle );
  2146. this.x = axis.x * s;
  2147. this.y = axis.y * s;
  2148. this.z = axis.z * s;
  2149. this.w = Math.cos( halfAngle );
  2150. return this;
  2151. },
  2152. setFromRotationMatrix: function ( m ) {
  2153. // Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  2154. function copySign( a, b ) {
  2155. return b < 0 ? -Math.abs( a ) : Math.abs( a );
  2156. }
  2157. var absQ = Math.pow( m.determinant(), 1.0 / 3.0 );
  2158. this.w = Math.sqrt( Math.max( 0, absQ + m.elements[0] + m.elements[5] + m.elements[10] ) ) / 2;
  2159. this.x = Math.sqrt( Math.max( 0, absQ + m.elements[0] - m.elements[5] - m.elements[10] ) ) / 2;
  2160. this.y = Math.sqrt( Math.max( 0, absQ - m.elements[0] + m.elements[5] - m.elements[10] ) ) / 2;
  2161. this.z = Math.sqrt( Math.max( 0, absQ - m.elements[0] - m.elements[5] + m.elements[10] ) ) / 2;
  2162. this.x = copySign( this.x, ( m.elements[6] - m.elements[9] ) );
  2163. this.y = copySign( this.y, ( m.elements[8] - m.elements[2] ) );
  2164. this.z = copySign( this.z, ( m.elements[1] - m.elements[4] ) );
  2165. this.normalize();
  2166. return this;
  2167. },
  2168. calculateW : function () {
  2169. this.w = - Math.sqrt( Math.abs( 1.0 - this.x * this.x - this.y * this.y - this.z * this.z ) );
  2170. return this;
  2171. },
  2172. inverse: function () {
  2173. this.x *= -1;
  2174. this.y *= -1;
  2175. this.z *= -1;
  2176. return this;
  2177. },
  2178. length: function () {
  2179. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  2180. },
  2181. normalize: function () {
  2182. var l = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  2183. if ( l === 0 ) {
  2184. this.x = 0;
  2185. this.y = 0;
  2186. this.z = 0;
  2187. this.w = 0;
  2188. } else {
  2189. l = 1 / l;
  2190. this.x = this.x * l;
  2191. this.y = this.y * l;
  2192. this.z = this.z * l;
  2193. this.w = this.w * l;
  2194. }
  2195. return this;
  2196. },
  2197. multiply: function ( a, b ) {
  2198. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  2199. this.x = a.x * b.w + a.y * b.z - a.z * b.y + a.w * b.x;
  2200. this.y = -a.x * b.z + a.y * b.w + a.z * b.x + a.w * b.y;
  2201. this.z = a.x * b.y - a.y * b.x + a.z * b.w + a.w * b.z;
  2202. this.w = -a.x * b.x - a.y * b.y - a.z * b.z + a.w * b.w;
  2203. return this;
  2204. },
  2205. multiplySelf: function ( b ) {
  2206. var qax = this.x, qay = this.y, qaz = this.z, qaw = this.w,
  2207. qbx = b.x, qby = b.y, qbz = b.z, qbw = b.w;
  2208. this.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  2209. this.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  2210. this.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  2211. this.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  2212. return this;
  2213. },
  2214. multiplyVector3: function ( vector, dest ) {
  2215. if ( !dest ) { dest = vector; }
  2216. var x = vector.x, y = vector.y, z = vector.z,
  2217. qx = this.x, qy = this.y, qz = this.z, qw = this.w;
  2218. // calculate quat * vector
  2219. var ix = qw * x + qy * z - qz * y,
  2220. iy = qw * y + qz * x - qx * z,
  2221. iz = qw * z + qx * y - qy * x,
  2222. iw = -qx * x - qy * y - qz * z;
  2223. // calculate result * inverse quat
  2224. dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  2225. dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  2226. dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  2227. return dest;
  2228. },
  2229. clone: function () {
  2230. return new THREE.Quaternion( this.x, this.y, this.z, this.w );
  2231. }
  2232. }
  2233. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  2234. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  2235. var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
  2236. if (cosHalfTheta < 0) {
  2237. qm.w = -qb.w; qm.x = -qb.x; qm.y = -qb.y; qm.z = -qb.z;
  2238. cosHalfTheta = -cosHalfTheta;
  2239. } else {
  2240. qm.copy(qb);
  2241. }
  2242. if ( Math.abs( cosHalfTheta ) >= 1.0 ) {
  2243. qm.w = qa.w; qm.x = qa.x; qm.y = qa.y; qm.z = qa.z;
  2244. return qm;
  2245. }
  2246. var halfTheta = Math.acos( cosHalfTheta ),
  2247. sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  2248. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  2249. qm.w = 0.5 * ( qa.w + qb.w );
  2250. qm.x = 0.5 * ( qa.x + qb.x );
  2251. qm.y = 0.5 * ( qa.y + qb.y );
  2252. qm.z = 0.5 * ( qa.z + qb.z );
  2253. return qm;
  2254. }
  2255. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  2256. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  2257. qm.w = ( qa.w * ratioA + qm.w * ratioB );
  2258. qm.x = ( qa.x * ratioA + qm.x * ratioB );
  2259. qm.y = ( qa.y * ratioA + qm.y * ratioB );
  2260. qm.z = ( qa.z * ratioA + qm.z * ratioB );
  2261. return qm;
  2262. }
  2263. /**
  2264. * @author mr.doob / http://mrdoob.com/
  2265. */
  2266. THREE.Vertex = function ( position ) {
  2267. this.position = position || new THREE.Vector3();
  2268. };
  2269. THREE.Vertex.prototype = {
  2270. constructor: THREE.Vertex,
  2271. clone: function () {
  2272. return new THREE.Vertex( this.position.clone() );
  2273. }
  2274. };
  2275. /**
  2276. * @author mr.doob / http://mrdoob.com/
  2277. * @author alteredq / http://alteredqualia.com/
  2278. */
  2279. THREE.Face3 = function ( a, b, c, normal, color, materialIndex ) {
  2280. this.a = a;
  2281. this.b = b;
  2282. this.c = c;
  2283. this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
  2284. this.vertexNormals = normal instanceof Array ? normal : [ ];
  2285. this.color = color instanceof THREE.Color ? color : new THREE.Color();
  2286. this.vertexColors = color instanceof Array ? color : [];
  2287. this.vertexTangents = [];
  2288. this.materialIndex = materialIndex;
  2289. this.centroid = new THREE.Vector3();
  2290. };
  2291. THREE.Face3.prototype = {
  2292. constructor: THREE.Face3,
  2293. clone: function () {
  2294. var face = new THREE.Face3( this.a, this.b, this.c );
  2295. face.normal.copy( this.normal );
  2296. face.color.copy( this.color );
  2297. face.centroid.copy( this.centroid );
  2298. face.materialIndex = this.materialIndex;
  2299. var i, il;
  2300. for ( i = 0, il = this.vertexNormals.length; i < il; i ++ ) face.vertexNormals[ i ] = this.vertexNormals[ i ].clone();
  2301. for ( i = 0, il = this.vertexColors.length; i < il; i ++ ) face.vertexColors[ i ] = this.vertexColors[ i ].clone();
  2302. for ( i = 0, il = this.vertexTangents.length; i < il; i ++ ) face.vertexTangents[ i ] = this.vertexTangents[ i ].clone();
  2303. return face;
  2304. }
  2305. };
  2306. /**
  2307. * @author mr.doob / http://mrdoob.com/
  2308. * @author alteredq / http://alteredqualia.com/
  2309. */
  2310. THREE.Face4 = function ( a, b, c, d, normal, color, materialIndex ) {
  2311. this.a = a;
  2312. this.b = b;
  2313. this.c = c;
  2314. this.d = d;
  2315. this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
  2316. this.vertexNormals = normal instanceof Array ? normal : [ ];
  2317. this.color = color instanceof THREE.Color ? color : new THREE.Color();
  2318. this.vertexColors = color instanceof Array ? color : [];
  2319. this.vertexTangents = [];
  2320. this.materialIndex = materialIndex;
  2321. this.centroid = new THREE.Vector3();
  2322. };
  2323. THREE.Face4.prototype = {
  2324. constructor: THREE.Face4,
  2325. clone: function () {
  2326. var face = new THREE.Face4( this.a, this.b, this.c, this.d );
  2327. face.normal.copy( this.normal );
  2328. face.color.copy( this.color );
  2329. face.centroid.copy( this.centroid );
  2330. face.materialIndex = this.materialIndex;
  2331. var i, il;
  2332. for ( i = 0, il = this.vertexNormals.length; i < il; i ++ ) face.vertexNormals[ i ] = this.vertexNormals[ i ].clone();
  2333. for ( i = 0, il = this.vertexColors.length; i < il; i ++ ) face.vertexColors[ i ] = this.vertexColors[ i ].clone();
  2334. for ( i = 0, il = this.vertexTangents.length; i < il; i ++ ) face.vertexTangents[ i ] = this.vertexTangents[ i ].clone();
  2335. return face;
  2336. }
  2337. };
  2338. /**
  2339. * @author mr.doob / http://mrdoob.com/
  2340. */
  2341. THREE.UV = function ( u, v ) {
  2342. this.u = u || 0;
  2343. this.v = v || 0;
  2344. };
  2345. THREE.UV.prototype = {
  2346. constructor: THREE.UV,
  2347. set: function ( u, v ) {
  2348. this.u = u;
  2349. this.v = v;
  2350. return this;
  2351. },
  2352. copy: function ( uv ) {
  2353. this.u = uv.u;
  2354. this.v = uv.v;
  2355. return this;
  2356. },
  2357. lerpSelf: function ( uv, alpha ) {
  2358. this.u += ( uv.u - this.u ) * alpha;
  2359. this.v += ( uv.v - this.v ) * alpha;
  2360. return this;
  2361. },
  2362. clone: function () {
  2363. return new THREE.UV( this.u, this.v );
  2364. }
  2365. };
  2366. /**
  2367. * @author mr.doob / http://mrdoob.com/
  2368. * @author mikael emtinger / http://gomo.se/
  2369. */
  2370. THREE.Camera = function () {
  2371. THREE.Object3D.call( this );
  2372. this.matrixWorldInverse = new THREE.Matrix4();
  2373. this.projectionMatrix = new THREE.Matrix4();
  2374. this.projectionMatrixInverse = new THREE.Matrix4();
  2375. };
  2376. THREE.Camera.prototype = new THREE.Object3D();
  2377. THREE.Camera.prototype.constructor = THREE.Camera;
  2378. THREE.Camera.prototype.lookAt = function ( vector ) {
  2379. // TODO: Add hierarchy support.
  2380. this.matrix.lookAt( this.position, vector, this.up );
  2381. if ( this.rotationAutoUpdate ) {
  2382. this.rotation.getRotationFromMatrix( this.matrix );
  2383. }
  2384. };
  2385. /**
  2386. * @author alteredq / http://alteredqualia.com/
  2387. */
  2388. THREE.OrthographicCamera = function ( left, right, top, bottom, near, far ) {
  2389. THREE.Camera.call( this );
  2390. this.left = left;
  2391. this.right = right;
  2392. this.top = top;
  2393. this.bottom = bottom;
  2394. this.near = ( near !== undefined ) ? near : 0.1;
  2395. this.far = ( far !== undefined ) ? far : 2000;
  2396. this.updateProjectionMatrix();
  2397. };
  2398. THREE.OrthographicCamera.prototype = new THREE.Camera();
  2399. THREE.OrthographicCamera.prototype.constructor = THREE.OrthographicCamera;
  2400. THREE.OrthographicCamera.prototype.updateProjectionMatrix = function () {
  2401. this.projectionMatrix.makeOrthographic( this.left, this.right, this.top, this.bottom, this.near, this.far );
  2402. };
  2403. /**
  2404. * @author mr.doob / http://mrdoob.com/
  2405. * @author greggman / http://games.greggman.com/
  2406. * @author zz85 / http://www.lab4games.net/zz85/blog
  2407. */
  2408. THREE.PerspectiveCamera = function ( fov, aspect, near, far ) {
  2409. THREE.Camera.call( this );
  2410. this.fov = fov !== undefined ? fov : 50;
  2411. this.aspect = aspect !== undefined ? aspect : 1;
  2412. this.near = near !== undefined ? near : 0.1;
  2413. this.far = far !== undefined ? far : 2000;
  2414. this.updateProjectionMatrix();
  2415. };
  2416. THREE.PerspectiveCamera.prototype = new THREE.Camera();
  2417. THREE.PerspectiveCamera.prototype.constructor = THREE.PerspectiveCamera;
  2418. /**
  2419. * Uses Focal Length (in mm) to estimate and set FOV
  2420. * 35mm (fullframe) camera is used if frame size is not specified;
  2421. * Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html
  2422. */
  2423. THREE.PerspectiveCamera.prototype.setLens = function ( focalLength, frameHeight ) {
  2424. frameHeight = frameHeight !== undefined ? frameHeight : 24;
  2425. this.fov = 2 * Math.atan( frameHeight / ( focalLength * 2 ) ) * ( 180 / Math.PI );
  2426. this.updateProjectionMatrix();
  2427. }
  2428. /**
  2429. * Sets an offset in a larger frustum. This is useful for multi-window or
  2430. * multi-monitor/multi-machine setups.
  2431. *
  2432. * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
  2433. * the monitors are in grid like this
  2434. *
  2435. * +---+---+---+
  2436. * | A | B | C |
  2437. * +---+---+---+
  2438. * | D | E | F |
  2439. * +---+---+---+
  2440. *
  2441. * then for each monitor you would call it like this
  2442. *
  2443. * var w = 1920;
  2444. * var h = 1080;
  2445. * var fullWidth = w * 3;
  2446. * var fullHeight = h * 2;
  2447. *
  2448. * --A--
  2449. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
  2450. * --B--
  2451. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
  2452. * --C--
  2453. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
  2454. * --D--
  2455. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
  2456. * --E--
  2457. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
  2458. * --F--
  2459. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
  2460. *
  2461. * Note there is no reason monitors have to be the same size or in a grid.
  2462. */
  2463. THREE.PerspectiveCamera.prototype.setViewOffset = function ( fullWidth, fullHeight, x, y, width, height ) {
  2464. this.fullWidth = fullWidth;
  2465. this.fullHeight = fullHeight;
  2466. this.x = x;
  2467. this.y = y;
  2468. this.width = width;
  2469. this.height = height;
  2470. this.updateProjectionMatrix();
  2471. };
  2472. THREE.PerspectiveCamera.prototype.updateProjectionMatrix = function () {
  2473. if ( this.fullWidth ) {
  2474. var aspect = this.fullWidth / this.fullHeight;
  2475. var top = Math.tan( this.fov * Math.PI / 360 ) * this.near;
  2476. var bottom = -top;
  2477. var left = aspect * bottom;
  2478. var right = aspect * top;
  2479. var width = Math.abs( right - left );
  2480. var height = Math.abs( top - bottom );
  2481. this.projectionMatrix.makeFrustum(
  2482. left + this.x * width / this.fullWidth,
  2483. left + ( this.x + this.width ) * width / this.fullWidth,
  2484. top - ( this.y + this.height ) * height / this.fullHeight,
  2485. top - this.y * height / this.fullHeight,
  2486. this.near,
  2487. this.far
  2488. );
  2489. } else {
  2490. this.projectionMatrix.makePerspective( this.fov, this.aspect, this.near, this.far );
  2491. }
  2492. };
  2493. /**
  2494. * @author mr.doob / http://mrdoob.com/
  2495. * @author alteredq / http://alteredqualia.com/
  2496. */
  2497. THREE.Light = function ( hex ) {
  2498. THREE.Object3D.call( this );
  2499. this.color = new THREE.Color( hex );
  2500. };
  2501. THREE.Light.prototype = new THREE.Object3D();
  2502. THREE.Light.prototype.constructor = THREE.Light;
  2503. THREE.Light.prototype.supr = THREE.Object3D.prototype;
  2504. /**
  2505. * @author mr.doob / http://mrdoob.com/
  2506. * @author alteredq / http://alteredqualia.com/
  2507. */
  2508. THREE.Material = function ( parameters ) {
  2509. parameters = parameters || {};
  2510. this.id = THREE.MaterialCount ++;
  2511. this.name = '';
  2512. this.opacity = parameters.opacity !== undefined ? parameters.opacity : 1;
  2513. this.transparent = parameters.transparent !== undefined ? parameters.transparent : false;
  2514. this.blending = parameters.blending !== undefined ? parameters.blending : THREE.NormalBlending;
  2515. this.blendSrc = parameters.blendSrc !== undefined ? parameters.blendSrc : THREE.SrcAlphaFactor;
  2516. this.blendDst = parameters.blendDst !== undefined ? parameters.blendDst : THREE.OneMinusSrcAlphaFactor;
  2517. this.blendEquation = parameters.blendEquation !== undefined ? parameters.blendEquation : THREE.AddEquation;
  2518. this.depthTest = parameters.depthTest !== undefined ? parameters.depthTest : true;
  2519. this.depthWrite = parameters.depthWrite !== undefined ? parameters.depthWrite : true;
  2520. this.polygonOffset = parameters.polygonOffset !== undefined ? parameters.polygonOffset : false;
  2521. this.polygonOffsetFactor = parameters.polygonOffsetFactor !== undefined ? parameters.polygonOffsetFactor : 0;
  2522. this.polygonOffsetUnits = parameters.polygonOffsetUnits !== undefined ? parameters.polygonOffsetUnits : 0;
  2523. this.alphaTest = parameters.alphaTest !== undefined ? parameters.alphaTest : 0;
  2524. this.overdraw = parameters.overdraw !== undefined ? parameters.overdraw : false; // Boolean for fixing antialiasing gaps in CanvasRenderer
  2525. this.needsUpdate = true;
  2526. }
  2527. THREE.MaterialCount = 0;
  2528. // shading
  2529. THREE.NoShading = 0;
  2530. THREE.FlatShading = 1;
  2531. THREE.SmoothShading = 2;
  2532. // colors
  2533. THREE.NoColors = 0;
  2534. THREE.FaceColors = 1;
  2535. THREE.VertexColors = 2;
  2536. // blending modes
  2537. THREE.NoBlending = 0;
  2538. THREE.NormalBlending = 1;
  2539. THREE.AdditiveBlending = 2;
  2540. THREE.SubtractiveBlending = 3;
  2541. THREE.MultiplyBlending = 4;
  2542. THREE.AdditiveAlphaBlending = 5;
  2543. THREE.CustomBlending = 6;
  2544. // custom blending equations
  2545. // (numbers start from 100 not to clash with other
  2546. // mappings to OpenGL constants defined in Texture.js)
  2547. THREE.AddEquation = 100;
  2548. THREE.SubtractEquation = 101;
  2549. THREE.ReverseSubtractEquation = 102;
  2550. // custom blending destination factors
  2551. THREE.ZeroFactor = 200;
  2552. THREE.OneFactor = 201;
  2553. THREE.SrcColorFactor = 202;
  2554. THREE.OneMinusSrcColorFactor = 203;
  2555. THREE.SrcAlphaFactor = 204;
  2556. THREE.OneMinusSrcAlphaFactor = 205;
  2557. THREE.DstAlphaFactor = 206;
  2558. THREE.OneMinusDstAlphaFactor = 207;
  2559. // custom blending source factors
  2560. //THREE.ZeroFactor = 200;
  2561. //THREE.OneFactor = 201;
  2562. //THREE.SrcAlphaFactor = 204;
  2563. //THREE.OneMinusSrcAlphaFactor = 205;
  2564. //THREE.DstAlphaFactor = 206;
  2565. //THREE.OneMinusDstAlphaFactor = 207;
  2566. THREE.DstColorFactor = 208;
  2567. THREE.OneMinusDstColorFactor = 209;
  2568. THREE.SrcAlphaSaturateFactor = 210;
  2569. /**
  2570. * @author mr.doob / http://mrdoob.com/
  2571. * @author alteredq / http://alteredqualia.com/
  2572. *
  2573. * parameters = {
  2574. * color: <hex>,
  2575. * opacity: <float>,
  2576. *
  2577. * blending: THREE.NormalBlending,
  2578. * depthTest: <bool>,
  2579. *
  2580. * linewidth: <float>,
  2581. * linecap: "round",
  2582. * linejoin: "round",
  2583. *
  2584. * vertexColors: <bool>
  2585. *
  2586. * fog: <bool>
  2587. * }
  2588. */
  2589. THREE.LineBasicMaterial = function ( parameters ) {
  2590. THREE.Material.call( this, parameters );
  2591. parameters = parameters || {};
  2592. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  2593. this.linewidth = parameters.linewidth !== undefined ? parameters.linewidth : 1;
  2594. this.linecap = parameters.linecap !== undefined ? parameters.linecap : 'round';
  2595. this.linejoin = parameters.linejoin !== undefined ? parameters.linejoin : 'round';
  2596. this.vertexColors = parameters.vertexColors ? parameters.vertexColors : false;
  2597. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  2598. };
  2599. THREE.LineBasicMaterial.prototype = new THREE.Material();
  2600. THREE.LineBasicMaterial.prototype.constructor = THREE.LineBasicMaterial;
  2601. /**
  2602. * @author mr.doob / http://mrdoob.com/
  2603. * @author alteredq / http://alteredqualia.com/
  2604. *
  2605. * parameters = {
  2606. * color: <hex>,
  2607. * opacity: <float>,
  2608. * map: new THREE.Texture( <Image> ),
  2609. *
  2610. * lightMap: new THREE.Texture( <Image> ),
  2611. *
  2612. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  2613. * combine: THREE.Multiply,
  2614. * reflectivity: <float>,
  2615. * refractionRatio: <float>,
  2616. *
  2617. * shading: THREE.SmoothShading,
  2618. * blending: THREE.NormalBlending,
  2619. * depthTest: <bool>,
  2620. *
  2621. * wireframe: <boolean>,
  2622. * wireframeLinewidth: <float>,
  2623. *
  2624. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  2625. *
  2626. * skinning: <bool>,
  2627. * morphTargets: <bool>,
  2628. *
  2629. * fog: <bool>
  2630. * }
  2631. */
  2632. THREE.MeshBasicMaterial = function ( parameters ) {
  2633. THREE.Material.call( this, parameters );
  2634. parameters = parameters || {};
  2635. // color property represents emissive for MeshBasicMaterial
  2636. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  2637. this.map = parameters.map !== undefined ? parameters.map : null;
  2638. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  2639. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  2640. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  2641. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  2642. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  2643. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  2644. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  2645. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  2646. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  2647. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  2648. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  2649. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  2650. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  2651. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  2652. };
  2653. THREE.MeshBasicMaterial.prototype = new THREE.Material();
  2654. THREE.MeshBasicMaterial.prototype.constructor = THREE.MeshBasicMaterial;
  2655. /**
  2656. * @author mr.doob / http://mrdoob.com/
  2657. * @author alteredq / http://alteredqualia.com/
  2658. *
  2659. * parameters = {
  2660. * color: <hex>,
  2661. * opacity: <float>,
  2662. * map: new THREE.Texture( <Image> ),
  2663. *
  2664. * size: <float>,
  2665. *
  2666. * blending: THREE.NormalBlending,
  2667. * depthTest: <bool>,
  2668. *
  2669. * vertexColors: <bool>,
  2670. *
  2671. * fog: <bool>
  2672. * }
  2673. */
  2674. THREE.ParticleBasicMaterial = function ( parameters ) {
  2675. THREE.Material.call( this, parameters );
  2676. parameters = parameters || {};
  2677. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  2678. this.map = parameters.map !== undefined ? parameters.map : null;
  2679. this.size = parameters.size !== undefined ? parameters.size : 1;
  2680. this.sizeAttenuation = parameters.sizeAttenuation !== undefined ? parameters.sizeAttenuation : true;
  2681. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : false;
  2682. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  2683. };
  2684. THREE.ParticleBasicMaterial.prototype = new THREE.Material();
  2685. THREE.ParticleBasicMaterial.prototype.constructor = THREE.ParticleBasicMaterial;
  2686. /**
  2687. * @author mr.doob / http://mrdoob.com/
  2688. */
  2689. THREE.ParticleDOMMaterial = function ( domElement ) {
  2690. THREE.Material.call( this );
  2691. this.domElement = domElement;
  2692. };
  2693. /**
  2694. * @author mr.doob / http://mrdoob.com/
  2695. * @author alteredq / http://alteredqualia.com/
  2696. * @author szimek / https://github.com/szimek/
  2697. */
  2698. THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type ) {
  2699. this.id = THREE.TextureCount ++;
  2700. this.image = image;
  2701. this.mapping = mapping !== undefined ? mapping : new THREE.UVMapping();
  2702. this.wrapS = wrapS !== undefined ? wrapS : THREE.ClampToEdgeWrapping;
  2703. this.wrapT = wrapT !== undefined ? wrapT : THREE.ClampToEdgeWrapping;
  2704. this.magFilter = magFilter !== undefined ? magFilter : THREE.LinearFilter;
  2705. this.minFilter = minFilter !== undefined ? minFilter : THREE.LinearMipMapLinearFilter;
  2706. this.format = format !== undefined ? format : THREE.RGBAFormat;
  2707. this.type = type !== undefined ? type : THREE.UnsignedByteType;
  2708. this.offset = new THREE.Vector2( 0, 0 );
  2709. this.repeat = new THREE.Vector2( 1, 1 );
  2710. this.generateMipmaps = true;
  2711. this.premultiplyAlpha = false;
  2712. this.needsUpdate = false;
  2713. this.onUpdate = null;
  2714. };
  2715. THREE.Texture.prototype = {
  2716. constructor: THREE.Texture,
  2717. clone: function () {
  2718. var clonedTexture = new THREE.Texture( this.image, this.mapping, this.wrapS, this.wrapT, this.magFilter, this.minFilter, this.format, this.type );
  2719. clonedTexture.offset.copy( this.offset );
  2720. clonedTexture.repeat.copy( this.repeat );
  2721. return clonedTexture;
  2722. }
  2723. };
  2724. THREE.TextureCount = 0;
  2725. THREE.MultiplyOperation = 0;
  2726. THREE.MixOperation = 1;
  2727. // Mapping modes
  2728. THREE.UVMapping = function () {};
  2729. THREE.CubeReflectionMapping = function () {};
  2730. THREE.CubeRefractionMapping = function () {};
  2731. THREE.SphericalReflectionMapping = function () {};
  2732. THREE.SphericalRefractionMapping = function () {};
  2733. // Wrapping modes
  2734. THREE.RepeatWrapping = 0;
  2735. THREE.ClampToEdgeWrapping = 1;
  2736. THREE.MirroredRepeatWrapping = 2;
  2737. // Filters
  2738. THREE.NearestFilter = 3;
  2739. THREE.NearestMipMapNearestFilter = 4;
  2740. THREE.NearestMipMapLinearFilter = 5;
  2741. THREE.LinearFilter = 6;
  2742. THREE.LinearMipMapNearestFilter = 7;
  2743. THREE.LinearMipMapLinearFilter = 8;
  2744. // Types
  2745. THREE.ByteType = 9;
  2746. THREE.UnsignedByteType = 10;
  2747. THREE.ShortType = 11;
  2748. THREE.UnsignedShortType = 12;
  2749. THREE.IntType = 13;
  2750. THREE.UnsignedIntType = 14;
  2751. THREE.FloatType = 15;
  2752. // Formats
  2753. THREE.AlphaFormat = 16;
  2754. THREE.RGBFormat = 17;
  2755. THREE.RGBAFormat = 18;
  2756. THREE.LuminanceFormat = 19;
  2757. THREE.LuminanceAlphaFormat = 20;
  2758. /**
  2759. * @author alteredq / http://alteredqualia.com/
  2760. */
  2761. THREE.DataTexture = function ( data, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter ) {
  2762. THREE.Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type );
  2763. this.image = { data: data, width: width, height: height };
  2764. };
  2765. THREE.DataTexture.prototype = new THREE.Texture();
  2766. THREE.DataTexture.prototype.constructor = THREE.DataTexture;
  2767. THREE.DataTexture.prototype.clone = function () {
  2768. 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 );
  2769. clonedTexture.offset.copy( this.offset );
  2770. clonedTexture.repeat.copy( this.repeat );
  2771. return clonedTexture;
  2772. };
  2773. /**
  2774. * @author mr.doob / http://mrdoob.com/
  2775. */
  2776. THREE.Particle = function ( material ) {
  2777. THREE.Object3D.call( this );
  2778. this.material = material;
  2779. };
  2780. THREE.Particle.prototype = new THREE.Object3D();
  2781. THREE.Particle.prototype.constructor = THREE.Particle;
  2782. /**
  2783. * @author mr.doob / http://mrdoob.com/
  2784. * @author alteredq / http://alteredqualia.com/
  2785. * @author mikael emtinger / http://gomo.se/
  2786. */
  2787. THREE.Mesh = function ( geometry, material ) {
  2788. THREE.Object3D.call( this );
  2789. this.geometry = geometry;
  2790. this.material = ( material !== undefined ) ? material : new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: true } );
  2791. if ( this.geometry ) {
  2792. // calc bound radius
  2793. if( ! this.geometry.boundingSphere ) {
  2794. this.geometry.computeBoundingSphere();
  2795. }
  2796. this.boundRadius = geometry.boundingSphere.radius;
  2797. // setup morph targets
  2798. if( this.geometry.morphTargets.length ) {
  2799. this.morphTargetBase = -1;
  2800. this.morphTargetForcedOrder = [];
  2801. this.morphTargetInfluences = [];
  2802. this.morphTargetDictionary = {};
  2803. for( var m = 0; m < this.geometry.morphTargets.length; m ++ ) {
  2804. this.morphTargetInfluences.push( 0 );
  2805. this.morphTargetDictionary[ this.geometry.morphTargets[ m ].name ] = m;
  2806. }
  2807. }
  2808. }
  2809. }
  2810. THREE.Mesh.prototype = new THREE.Object3D();
  2811. THREE.Mesh.prototype.constructor = THREE.Mesh;
  2812. THREE.Mesh.prototype.supr = THREE.Object3D.prototype;
  2813. /*
  2814. * Get Morph Target Index by Name
  2815. */
  2816. THREE.Mesh.prototype.getMorphTargetIndexByName = function( name ) {
  2817. if ( this.morphTargetDictionary[ name ] !== undefined ) {
  2818. return this.morphTargetDictionary[ name ];
  2819. }
  2820. console.log( "THREE.Mesh.getMorphTargetIndexByName: morph target " + name + " does not exist. Returning 0." );
  2821. return 0;
  2822. }
  2823. /**
  2824. * @author mr.doob / http://mrdoob.com/
  2825. */
  2826. THREE.Line = function ( geometry, material, type ) {
  2827. THREE.Object3D.call( this );
  2828. this.geometry = geometry;
  2829. this.material = ( material !== undefined ) ? material : new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff } );
  2830. this.type = ( type !== undefined ) ? type : THREE.LineStrip;
  2831. if ( this.geometry ) {
  2832. if ( ! this.geometry.boundingSphere ) {
  2833. this.geometry.computeBoundingSphere();
  2834. }
  2835. }
  2836. };
  2837. THREE.LineStrip = 0;
  2838. THREE.LinePieces = 1;
  2839. THREE.Line.prototype = new THREE.Object3D();
  2840. THREE.Line.prototype.constructor = THREE.Line;
  2841. /**
  2842. * @author mikael emtinger / http://gomo.se/
  2843. * @author alteredq / http://alteredqualia.com/
  2844. */
  2845. THREE.Bone = function( belongsToSkin ) {
  2846. THREE.Object3D.call( this );
  2847. this.skin = belongsToSkin;
  2848. this.skinMatrix = new THREE.Matrix4();
  2849. };
  2850. THREE.Bone.prototype = new THREE.Object3D();
  2851. THREE.Bone.prototype.constructor = THREE.Bone;
  2852. THREE.Bone.prototype.supr = THREE.Object3D.prototype;
  2853. THREE.Bone.prototype.update = function( parentSkinMatrix, forceUpdate ) {
  2854. // update local
  2855. if ( this.matrixAutoUpdate ) {
  2856. forceUpdate |= this.updateMatrix();
  2857. }
  2858. // update skin matrix
  2859. if ( forceUpdate || this.matrixWorldNeedsUpdate ) {
  2860. if( parentSkinMatrix ) {
  2861. this.skinMatrix.multiply( parentSkinMatrix, this.matrix );
  2862. } else {
  2863. this.skinMatrix.copy( this.matrix );
  2864. }
  2865. this.matrixWorldNeedsUpdate = false;
  2866. forceUpdate = true;
  2867. }
  2868. // update children
  2869. var child, i, l = this.children.length;
  2870. for ( i = 0; i < l; i ++ ) {
  2871. this.children[ i ].update( this.skinMatrix, forceUpdate );
  2872. }
  2873. };
  2874. /**
  2875. * @author mikael emtinger / http://gomo.se/
  2876. */
  2877. THREE.Sprite = function ( parameters ) {
  2878. THREE.Object3D.call( this );
  2879. this.color = ( parameters.color !== undefined ) ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  2880. this.map = ( parameters.map !== undefined ) ? parameters.map : new THREE.Texture();
  2881. this.blending = ( parameters.blending !== undefined ) ? parameters.blending : THREE.NormalBlending;
  2882. this.blendSrc = parameters.blendSrc !== undefined ? parameters.blendSrc : THREE.SrcAlphaFactor;
  2883. this.blendDst = parameters.blendDst !== undefined ? parameters.blendDst : THREE.OneMinusSrcAlphaFactor;
  2884. this.blendEquation = parameters.blendEquation !== undefined ? parameters.blendEquation : THREE.AddEquation;
  2885. this.useScreenCoordinates = ( parameters.useScreenCoordinates !== undefined ) ? parameters.useScreenCoordinates : true;
  2886. this.mergeWith3D = ( parameters.mergeWith3D !== undefined ) ? parameters.mergeWith3D : !this.useScreenCoordinates;
  2887. this.affectedByDistance = ( parameters.affectedByDistance !== undefined ) ? parameters.affectedByDistance : !this.useScreenCoordinates;
  2888. this.scaleByViewport = ( parameters.scaleByViewport !== undefined ) ? parameters.scaleByViewport : !this.affectedByDistance;
  2889. this.alignment = ( parameters.alignment instanceof THREE.Vector2 ) ? parameters.alignment : THREE.SpriteAlignment.center;
  2890. this.rotation3d = this.rotation;
  2891. this.rotation = 0;
  2892. this.opacity = 1;
  2893. this.uvOffset = new THREE.Vector2( 0, 0 );
  2894. this.uvScale = new THREE.Vector2( 1, 1 );
  2895. };
  2896. THREE.Sprite.prototype = new THREE.Object3D();
  2897. THREE.Sprite.prototype.constructor = THREE.Sprite;
  2898. /*
  2899. * Custom update matrix
  2900. */
  2901. THREE.Sprite.prototype.updateMatrix = function () {
  2902. this.matrix.setPosition( this.position );
  2903. this.rotation3d.set( 0, 0, this.rotation );
  2904. this.matrix.setRotationFromEuler( this.rotation3d );
  2905. if ( this.scale.x !== 1 || this.scale.y !== 1 ) {
  2906. this.matrix.scale( this.scale );
  2907. this.boundRadiusScale = Math.max( this.scale.x, this.scale.y );
  2908. }
  2909. this.matrixWorldNeedsUpdate = true;
  2910. };
  2911. /*
  2912. * Alignment
  2913. */
  2914. THREE.SpriteAlignment = {};
  2915. THREE.SpriteAlignment.topLeft = new THREE.Vector2( 1, -1 );
  2916. THREE.SpriteAlignment.topCenter = new THREE.Vector2( 0, -1 );
  2917. THREE.SpriteAlignment.topRight = new THREE.Vector2( -1, -1 );
  2918. THREE.SpriteAlignment.centerLeft = new THREE.Vector2( 1, 0 );
  2919. THREE.SpriteAlignment.center = new THREE.Vector2( 0, 0 );
  2920. THREE.SpriteAlignment.centerRight = new THREE.Vector2( -1, 0 );
  2921. THREE.SpriteAlignment.bottomLeft = new THREE.Vector2( 1, 1 );
  2922. THREE.SpriteAlignment.bottomCenter = new THREE.Vector2( 0, 1 );
  2923. THREE.SpriteAlignment.bottomRight = new THREE.Vector2( -1, 1 );
  2924. /**
  2925. * @author mr.doob / http://mrdoob.com/
  2926. */
  2927. THREE.Scene = function () {
  2928. THREE.Object3D.call( this );
  2929. this.fog = null;
  2930. this.overrideMaterial = null;
  2931. this.matrixAutoUpdate = false;
  2932. this.__objects = [];
  2933. this.__lights = [];
  2934. this.__objectsAdded = [];
  2935. this.__objectsRemoved = [];
  2936. };
  2937. THREE.Scene.prototype = new THREE.Object3D();
  2938. THREE.Scene.prototype.constructor = THREE.Scene;
  2939. THREE.Scene.prototype.__addObject = function ( object ) {
  2940. if ( object instanceof THREE.Light ) {
  2941. if ( this.__lights.indexOf( object ) === - 1 ) {
  2942. this.__lights.push( object );
  2943. }
  2944. } else if ( !( object instanceof THREE.Camera || object instanceof THREE.Bone ) ) {
  2945. if ( this.__objects.indexOf( object ) === - 1 ) {
  2946. this.__objects.push( object );
  2947. this.__objectsAdded.push( object );
  2948. // check if previously removed
  2949. var i = this.__objectsRemoved.indexOf( object );
  2950. if ( i !== -1 ) {
  2951. this.__objectsRemoved.splice( i, 1 );
  2952. }
  2953. }
  2954. }
  2955. for ( var c = 0; c < object.children.length; c ++ ) {
  2956. this.__addObject( object.children[ c ] );
  2957. }
  2958. };
  2959. THREE.Scene.prototype.__removeObject = function ( object ) {
  2960. if ( object instanceof THREE.Light ) {
  2961. var i = this.__lights.indexOf( object );
  2962. if ( i !== -1 ) {
  2963. this.__lights.splice( i, 1 );
  2964. }
  2965. } else if ( !( object instanceof THREE.Camera ) ) {
  2966. var i = this.__objects.indexOf( object );
  2967. if( i !== -1 ) {
  2968. this.__objects.splice( i, 1 );
  2969. this.__objectsRemoved.push( object );
  2970. // check if previously added
  2971. var ai = this.__objectsAdded.indexOf( object );
  2972. if ( ai !== -1 ) {
  2973. this.__objectsAdded.splice( ai, 1 );
  2974. }
  2975. }
  2976. }
  2977. for ( var c = 0; c < object.children.length; c ++ ) {
  2978. this.__removeObject( object.children[ c ] );
  2979. }
  2980. };
  2981. /**
  2982. * @author mr.doob / http://mrdoob.com/
  2983. */
  2984. THREE.DOMRenderer = function () {
  2985. var _renderData, _elements,
  2986. _width, _height, _widthHalf, _heightHalf, _transformProp,
  2987. _projector = new THREE.Projector();
  2988. var getSupportedProp = function ( proparray ) {
  2989. var root = document.documentElement
  2990. for ( var i = 0; i < proparray.length; i ++ ) {
  2991. if ( typeof root.style[ proparray[ i ] ] === "string" ) {
  2992. return proparray[i];
  2993. }
  2994. }
  2995. return null;
  2996. };
  2997. _transformProp = getSupportedProp( [ 'transform', 'MozTransform', 'WebkitTransform', 'msTransform', 'OTransform' ] );
  2998. this.domElement = document.createElement( 'div' );
  2999. this.setSize = function ( width, height ) {
  3000. _width = width;
  3001. _height = height;
  3002. _widthHalf = _width / 2;
  3003. _heightHalf = _height / 2;
  3004. };
  3005. this.render = function ( scene, camera ) {
  3006. var e, el, m, ml, element, material, dom, v1x, v1y;
  3007. _renderData = _projector.projectScene( scene, camera );
  3008. _elements = _renderData.elements;
  3009. for ( e = 0, el = _elements.length; e < el; e ++ ) {
  3010. element = _elements[ e ];
  3011. if ( element instanceof THREE.RenderableParticle && element.material instanceof THREE.ParticleDOMMaterial ) {
  3012. dom = element.material.domElement;
  3013. v1x = element.x * _widthHalf + _widthHalf - ( dom.offsetWidth >> 1 );
  3014. v1y = element.y * _heightHalf + _heightHalf - ( dom.offsetHeight >> 1 );
  3015. dom.style.left = v1x + 'px';
  3016. dom.style.top = v1y + 'px';
  3017. dom.style.zIndex = Math.abs( Math.floor( ( 1 - element.z ) * camera.far / camera.near ) )
  3018. if ( _transformProp ) {
  3019. var scaleX = element.scale.x * _widthHalf;
  3020. var scaleY = element.scale.y * _heightHalf;
  3021. var scaleVal = "scale(" + scaleX + "," + scaleY + ")";
  3022. dom.style[ _transformProp ] = scaleVal;
  3023. }
  3024. }
  3025. }
  3026. };
  3027. };
  3028. /**
  3029. * @author mr.doob / http://mrdoob.com/
  3030. */
  3031. THREE.RenderableParticle = function () {
  3032. this.x = null;
  3033. this.y = null;
  3034. this.z = null;
  3035. this.rotation = null;
  3036. this.scale = new THREE.Vector2();
  3037. this.material = null;
  3038. };
  3039. /**
  3040. * @author mr.doob / http://mrdoob.com/
  3041. */
  3042. THREE.RenderableVertex = function () {
  3043. this.positionWorld = new THREE.Vector3();
  3044. this.positionScreen = new THREE.Vector4();
  3045. this.visible = true;
  3046. };
  3047. THREE.RenderableVertex.prototype.copy = function ( vertex ) {
  3048. this.positionWorld.copy( vertex.positionWorld );
  3049. this.positionScreen.copy( vertex.positionScreen );
  3050. }
  3051. /**
  3052. * @author mr.doob / http://mrdoob.com/
  3053. */
  3054. THREE.RenderableFace3 = function () {
  3055. this.v1 = new THREE.RenderableVertex();
  3056. this.v2 = new THREE.RenderableVertex();
  3057. this.v3 = new THREE.RenderableVertex();
  3058. this.centroidWorld = new THREE.Vector3();
  3059. this.centroidScreen = new THREE.Vector3();
  3060. this.normalWorld = new THREE.Vector3();
  3061. this.vertexNormalsWorld = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  3062. this.material = null;
  3063. this.faceMaterial = null;
  3064. this.uvs = [[]];
  3065. this.z = null;
  3066. };
  3067. /**
  3068. * @author mr.doob / http://mrdoob.com/
  3069. */
  3070. THREE.RenderableFace4 = function () {
  3071. this.v1 = new THREE.RenderableVertex();
  3072. this.v2 = new THREE.RenderableVertex();
  3073. this.v3 = new THREE.RenderableVertex();
  3074. this.v4 = new THREE.RenderableVertex();
  3075. this.centroidWorld = new THREE.Vector3();
  3076. this.centroidScreen = new THREE.Vector3();
  3077. this.normalWorld = new THREE.Vector3();
  3078. this.vertexNormalsWorld = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  3079. this.material = null;
  3080. this.faceMaterial = null;
  3081. this.uvs = [[]];
  3082. this.z = null;
  3083. };
  3084. /**
  3085. * @author mr.doob / http://mrdoob.com/
  3086. */
  3087. THREE.RenderableObject = function () {
  3088. this.object = null;
  3089. this.z = null;
  3090. };
  3091. /**
  3092. * @author mr.doob / http://mrdoob.com/
  3093. */
  3094. THREE.RenderableParticle = function () {
  3095. this.x = null;
  3096. this.y = null;
  3097. this.z = null;
  3098. this.rotation = null;
  3099. this.scale = new THREE.Vector2();
  3100. this.material = null;
  3101. };
  3102. /**
  3103. * @author mr.doob / http://mrdoob.com/
  3104. */
  3105. THREE.RenderableLine = function () {
  3106. this.z = null;
  3107. this.v1 = new THREE.RenderableVertex();
  3108. this.v2 = new THREE.RenderableVertex();
  3109. this.material = null;
  3110. };