ThreeDOM.js 104 KB

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