ThreeDOM.js 104 KB

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