ThreeDOM.js 104 KB

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