ThreeDOM.js 103 KB

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