WebGLRenderer.js 180 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522
  1. /**
  2. * @author supereggbert / http://www.paulbrunt.co.uk/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author szimek / https://github.com/szimek/
  6. */
  7. THREE.WebGLRenderer = function ( parameters ) {
  8. console.log( 'THREE.WebGLRenderer', THREE.REVISION );
  9. parameters = parameters || {};
  10. var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
  11. _precision = parameters.precision !== undefined ? parameters.precision : 'highp',
  12. _alpha = parameters.alpha !== undefined ? parameters.alpha : true,
  13. _premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
  14. _antialias = parameters.antialias !== undefined ? parameters.antialias : false,
  15. _stencil = parameters.stencil !== undefined ? parameters.stencil : true,
  16. _preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
  17. _clearColor = parameters.clearColor !== undefined ? new THREE.Color( parameters.clearColor ) : new THREE.Color( 0x000000 ),
  18. _clearAlpha = parameters.clearAlpha !== undefined ? parameters.clearAlpha : 0;
  19. // public properties
  20. this.domElement = _canvas;
  21. this.context = null;
  22. this.devicePixelRatio = parameters.devicePixelRatio !== undefined
  23. ? parameters.devicePixelRatio
  24. : window.devicePixelRatio !== undefined
  25. ? window.devicePixelRatio
  26. : 1;
  27. // clearing
  28. this.autoClear = true;
  29. this.autoClearColor = true;
  30. this.autoClearDepth = true;
  31. this.autoClearStencil = true;
  32. // scene graph
  33. this.sortObjects = true;
  34. this.autoUpdateObjects = true;
  35. this.autoUpdateScene = true;
  36. // physically based shading
  37. this.gammaInput = false;
  38. this.gammaOutput = false;
  39. this.physicallyBasedShading = false;
  40. // shadow map
  41. this.shadowMapEnabled = false;
  42. this.shadowMapAutoUpdate = true;
  43. this.shadowMapType = THREE.PCFShadowMap;
  44. this.shadowMapCullFace = THREE.CullFaceFront;
  45. this.shadowMapDebug = false;
  46. this.shadowMapCascade = false;
  47. // morphs
  48. this.maxMorphTargets = 8;
  49. this.maxMorphNormals = 4;
  50. // flags
  51. this.autoScaleCubemaps = true;
  52. // custom render plugins
  53. this.renderPluginsPre = [];
  54. this.renderPluginsPost = [];
  55. // info
  56. this.info = {
  57. memory: {
  58. programs: 0,
  59. geometries: 0,
  60. textures: 0
  61. },
  62. render: {
  63. calls: 0,
  64. vertices: 0,
  65. faces: 0,
  66. points: 0
  67. }
  68. };
  69. // internal properties
  70. var _this = this,
  71. _programs = [],
  72. _programs_counter = 0,
  73. // internal state cache
  74. _currentProgram = null,
  75. _currentFramebuffer = null,
  76. _currentMaterialId = -1,
  77. _currentGeometryGroupHash = null,
  78. _currentCamera = null,
  79. _geometryGroupCounter = 0,
  80. _usedTextureUnits = 0,
  81. // GL state cache
  82. _oldDoubleSided = -1,
  83. _oldFlipSided = -1,
  84. _oldBlending = -1,
  85. _oldBlendEquation = -1,
  86. _oldBlendSrc = -1,
  87. _oldBlendDst = -1,
  88. _oldDepthTest = -1,
  89. _oldDepthWrite = -1,
  90. _oldPolygonOffset = null,
  91. _oldPolygonOffsetFactor = null,
  92. _oldPolygonOffsetUnits = null,
  93. _oldLineWidth = null,
  94. _viewportX = 0,
  95. _viewportY = 0,
  96. _viewportWidth = 0,
  97. _viewportHeight = 0,
  98. _currentWidth = 0,
  99. _currentHeight = 0,
  100. _enabledAttributes = {},
  101. // frustum
  102. _frustum = new THREE.Frustum(),
  103. // camera matrices cache
  104. _projScreenMatrix = new THREE.Matrix4(),
  105. _projScreenMatrixPS = new THREE.Matrix4(),
  106. _vector3 = new THREE.Vector3(),
  107. // light arrays cache
  108. _direction = new THREE.Vector3(),
  109. _lightsNeedUpdate = true,
  110. _lights = {
  111. ambient: [ 0, 0, 0 ],
  112. directional: { length: 0, colors: new Array(), positions: new Array() },
  113. point: { length: 0, colors: new Array(), positions: new Array(), distances: new Array() },
  114. spot: { length: 0, colors: new Array(), positions: new Array(), distances: new Array(), directions: new Array(), anglesCos: new Array(), exponents: new Array() },
  115. hemi: { length: 0, skyColors: new Array(), groundColors: new Array(), positions: new Array() }
  116. };
  117. // initialize
  118. var _gl;
  119. var _glExtensionTextureFloat;
  120. var _glExtensionStandardDerivatives;
  121. var _glExtensionTextureFilterAnisotropic;
  122. var _glExtensionCompressedTextureS3TC;
  123. initGL();
  124. setDefaultGLState();
  125. this.context = _gl;
  126. // GPU capabilities
  127. var _maxTextures = _gl.getParameter( _gl.MAX_TEXTURE_IMAGE_UNITS );
  128. var _maxVertexTextures = _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
  129. var _maxTextureSize = _gl.getParameter( _gl.MAX_TEXTURE_SIZE );
  130. var _maxCubemapSize = _gl.getParameter( _gl.MAX_CUBE_MAP_TEXTURE_SIZE );
  131. var _maxAnisotropy = _glExtensionTextureFilterAnisotropic ? _gl.getParameter( _glExtensionTextureFilterAnisotropic.MAX_TEXTURE_MAX_ANISOTROPY_EXT ) : 0;
  132. var _supportsVertexTextures = ( _maxVertexTextures > 0 );
  133. var _supportsBoneTextures = _supportsVertexTextures && _glExtensionTextureFloat;
  134. var _compressedTextureFormats = _glExtensionCompressedTextureS3TC ? _gl.getParameter( _gl.COMPRESSED_TEXTURE_FORMATS ) : [];
  135. //
  136. var _vertexShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.HIGH_FLOAT );
  137. var _vertexShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.MEDIUM_FLOAT );
  138. var _vertexShaderPrecisionLowpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.LOW_FLOAT );
  139. var _fragmentShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.HIGH_FLOAT );
  140. var _fragmentShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.MEDIUM_FLOAT );
  141. var _fragmentShaderPrecisionLowpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.LOW_FLOAT );
  142. var _vertexShaderPrecisionHighpInt = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.HIGH_INT );
  143. var _vertexShaderPrecisionMediumpInt = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.MEDIUM_INT );
  144. var _vertexShaderPrecisionLowpInt = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.LOW_INT );
  145. var _fragmentShaderPrecisionHighpInt = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.HIGH_INT );
  146. var _fragmentShaderPrecisionMediumpInt = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.MEDIUM_INT );
  147. var _fragmentShaderPrecisionLowpInt = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.LOW_INT );
  148. // clamp precision to maximum available
  149. var highpAvailable = _vertexShaderPrecisionHighpFloat.precision > 0 && _fragmentShaderPrecisionHighpFloat.precision > 0;
  150. var mediumpAvailable = _vertexShaderPrecisionMediumpFloat.precision > 0 && _fragmentShaderPrecisionMediumpFloat.precision > 0;
  151. if ( _precision === "highp" && ! highpAvailable ) {
  152. if ( mediumpAvailable ) {
  153. _precision = "mediump";
  154. console.warn( "WebGLRenderer: highp not supported, using mediump" );
  155. } else {
  156. _precision = "lowp";
  157. console.warn( "WebGLRenderer: highp and mediump not supported, using lowp" );
  158. }
  159. }
  160. if ( _precision === "mediump" && ! mediumpAvailable ) {
  161. _precision = "lowp";
  162. console.warn( "WebGLRenderer: mediump not supported, using lowp" );
  163. }
  164. // API
  165. this.getContext = function () {
  166. return _gl;
  167. };
  168. this.supportsVertexTextures = function () {
  169. return _supportsVertexTextures;
  170. };
  171. this.supportsFloatTextures = function () {
  172. return _glExtensionTextureFloat;
  173. };
  174. this.supportsStandardDerivatives = function () {
  175. return _glExtensionStandardDerivatives;
  176. };
  177. this.supportsCompressedTextureS3TC = function () {
  178. return _glExtensionCompressedTextureS3TC;
  179. };
  180. this.getMaxAnisotropy = function () {
  181. return _maxAnisotropy;
  182. };
  183. this.getPrecision = function () {
  184. return _precision;
  185. };
  186. this.setSize = function ( width, height ) {
  187. _canvas.width = width * this.devicePixelRatio;
  188. _canvas.height = height * this.devicePixelRatio;
  189. _canvas.style.width = width + 'px';
  190. _canvas.style.height = height + 'px';
  191. this.setViewport( 0, 0, _canvas.width, _canvas.height );
  192. };
  193. this.setViewport = function ( x, y, width, height ) {
  194. _viewportX = x !== undefined ? x : 0;
  195. _viewportY = y !== undefined ? y : 0;
  196. _viewportWidth = width !== undefined ? width : _canvas.width;
  197. _viewportHeight = height !== undefined ? height : _canvas.height;
  198. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  199. };
  200. this.setScissor = function ( x, y, width, height ) {
  201. _gl.scissor( x, y, width, height );
  202. };
  203. this.enableScissorTest = function ( enable ) {
  204. enable ? _gl.enable( _gl.SCISSOR_TEST ) : _gl.disable( _gl.SCISSOR_TEST );
  205. };
  206. // Clearing
  207. this.setClearColorHex = function ( hex, alpha ) {
  208. _clearColor.setHex( hex );
  209. _clearAlpha = alpha;
  210. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  211. };
  212. this.setClearColor = function ( color, alpha ) {
  213. _clearColor.copy( color );
  214. _clearAlpha = alpha;
  215. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  216. };
  217. this.getClearColor = function () {
  218. return _clearColor;
  219. };
  220. this.getClearAlpha = function () {
  221. return _clearAlpha;
  222. };
  223. this.clear = function ( color, depth, stencil ) {
  224. var bits = 0;
  225. if ( color === undefined || color ) bits |= _gl.COLOR_BUFFER_BIT;
  226. if ( depth === undefined || depth ) bits |= _gl.DEPTH_BUFFER_BIT;
  227. if ( stencil === undefined || stencil ) bits |= _gl.STENCIL_BUFFER_BIT;
  228. _gl.clear( bits );
  229. };
  230. this.clearTarget = function ( renderTarget, color, depth, stencil ) {
  231. this.setRenderTarget( renderTarget );
  232. this.clear( color, depth, stencil );
  233. };
  234. // Plugins
  235. this.addPostPlugin = function ( plugin ) {
  236. plugin.init( this );
  237. this.renderPluginsPost.push( plugin );
  238. };
  239. this.addPrePlugin = function ( plugin ) {
  240. plugin.init( this );
  241. this.renderPluginsPre.push( plugin );
  242. };
  243. // Rendering
  244. this.updateShadowMap = function ( scene, camera ) {
  245. _currentProgram = null;
  246. _oldBlending = -1;
  247. _oldDepthTest = -1;
  248. _oldDepthWrite = -1;
  249. _currentGeometryGroupHash = -1;
  250. _currentMaterialId = -1;
  251. _lightsNeedUpdate = true;
  252. _oldDoubleSided = -1;
  253. _oldFlipSided = -1;
  254. this.shadowMapPlugin.update( scene, camera );
  255. };
  256. // Internal functions
  257. // Buffer allocation
  258. function createParticleBuffers ( geometry ) {
  259. geometry.__webglVertexBuffer = _gl.createBuffer();
  260. geometry.__webglColorBuffer = _gl.createBuffer();
  261. _this.info.memory.geometries ++;
  262. };
  263. function createLineBuffers ( geometry ) {
  264. geometry.__webglVertexBuffer = _gl.createBuffer();
  265. geometry.__webglColorBuffer = _gl.createBuffer();
  266. geometry.__webglLineDistanceBuffer = _gl.createBuffer();
  267. _this.info.memory.geometries ++;
  268. };
  269. function createRibbonBuffers ( geometry ) {
  270. geometry.__webglVertexBuffer = _gl.createBuffer();
  271. geometry.__webglColorBuffer = _gl.createBuffer();
  272. geometry.__webglNormalBuffer = _gl.createBuffer();
  273. _this.info.memory.geometries ++;
  274. };
  275. function createMeshBuffers ( geometryGroup ) {
  276. geometryGroup.__webglVertexBuffer = _gl.createBuffer();
  277. geometryGroup.__webglNormalBuffer = _gl.createBuffer();
  278. geometryGroup.__webglTangentBuffer = _gl.createBuffer();
  279. geometryGroup.__webglColorBuffer = _gl.createBuffer();
  280. geometryGroup.__webglUVBuffer = _gl.createBuffer();
  281. geometryGroup.__webglUV2Buffer = _gl.createBuffer();
  282. geometryGroup.__webglSkinIndicesBuffer = _gl.createBuffer();
  283. geometryGroup.__webglSkinWeightsBuffer = _gl.createBuffer();
  284. geometryGroup.__webglFaceBuffer = _gl.createBuffer();
  285. geometryGroup.__webglLineBuffer = _gl.createBuffer();
  286. var m, ml;
  287. if ( geometryGroup.numMorphTargets ) {
  288. geometryGroup.__webglMorphTargetsBuffers = [];
  289. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  290. geometryGroup.__webglMorphTargetsBuffers.push( _gl.createBuffer() );
  291. }
  292. }
  293. if ( geometryGroup.numMorphNormals ) {
  294. geometryGroup.__webglMorphNormalsBuffers = [];
  295. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  296. geometryGroup.__webglMorphNormalsBuffers.push( _gl.createBuffer() );
  297. }
  298. }
  299. _this.info.memory.geometries ++;
  300. };
  301. // Events
  302. var onGeometryDispose = function ( event ) {
  303. var geometry = event.target;
  304. geometry.removeEventListener( 'dispose', onGeometryDispose );
  305. deallocateGeometry( geometry );
  306. _this.info.memory.geometries --;
  307. };
  308. var onTextureDispose = function ( event ) {
  309. var texture = event.target;
  310. texture.removeEventListener( 'dispose', onTextureDispose );
  311. deallocateTexture( texture );
  312. _this.info.memory.textures --;
  313. };
  314. var onRenderTargetDispose = function ( event ) {
  315. var renderTarget = event.target;
  316. renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );
  317. deallocateRenderTarget( renderTarget );
  318. _this.info.memory.textures --;
  319. };
  320. var onMaterialDispose = function ( event ) {
  321. var material = event.target;
  322. material.removeEventListener( 'dispose', onMaterialDispose );
  323. deallocateMaterial( material );
  324. };
  325. // Buffer deallocation
  326. var deallocateGeometry = function ( geometry ) {
  327. geometry.__webglInit = undefined;
  328. if ( geometry.__webglVertexBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglVertexBuffer );
  329. if ( geometry.__webglNormalBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglNormalBuffer );
  330. if ( geometry.__webglTangentBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglTangentBuffer );
  331. if ( geometry.__webglColorBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglColorBuffer );
  332. if ( geometry.__webglUVBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglUVBuffer );
  333. if ( geometry.__webglUV2Buffer !== undefined ) _gl.deleteBuffer( geometry.__webglUV2Buffer );
  334. if ( geometry.__webglSkinIndicesBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglSkinIndicesBuffer );
  335. if ( geometry.__webglSkinWeightsBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglSkinWeightsBuffer );
  336. if ( geometry.__webglFaceBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglFaceBuffer );
  337. if ( geometry.__webglLineBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglLineBuffer );
  338. if ( geometry.__webglLineDistanceBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglLineDistanceBuffer );
  339. // geometry groups
  340. if ( geometry.geometryGroups !== undefined ) {
  341. for ( var g in geometry.geometryGroups ) {
  342. var geometryGroup = geometry.geometryGroups[ g ];
  343. if ( geometryGroup.numMorphTargets !== undefined ) {
  344. for ( var m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  345. _gl.deleteBuffer( geometryGroup.__webglMorphTargetsBuffers[ m ] );
  346. }
  347. }
  348. if ( geometryGroup.numMorphNormals !== undefined ) {
  349. for ( var m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  350. _gl.deleteBuffer( geometryGroup.__webglMorphNormalsBuffers[ m ] );
  351. }
  352. }
  353. deleteCustomAttributesBuffers( geometryGroup );
  354. }
  355. }
  356. deleteCustomAttributesBuffers( geometry );
  357. };
  358. var deallocateTexture = function ( texture ) {
  359. if ( texture.image && texture.image.__webglTextureCube ) {
  360. // cube texture
  361. _gl.deleteTexture( texture.image.__webglTextureCube );
  362. } else {
  363. // 2D texture
  364. if ( ! texture.__webglInit ) return;
  365. texture.__webglInit = false;
  366. _gl.deleteTexture( texture.__webglTexture );
  367. }
  368. };
  369. var deallocateRenderTarget = function ( renderTarget ) {
  370. if ( !renderTarget || ! renderTarget.__webglTexture ) return;
  371. _gl.deleteTexture( renderTarget.__webglTexture );
  372. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  373. for ( var i = 0; i < 6; i ++ ) {
  374. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer[ i ] );
  375. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer[ i ] );
  376. }
  377. } else {
  378. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer );
  379. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer );
  380. }
  381. };
  382. var deallocateMaterial = function ( material ) {
  383. var program = material.program;
  384. if ( program === undefined ) return;
  385. material.program = undefined;
  386. // only deallocate GL program if this was the last use of shared program
  387. // assumed there is only single copy of any program in the _programs list
  388. // (that's how it's constructed)
  389. var i, il, programInfo;
  390. var deleteProgram = false;
  391. for ( i = 0, il = _programs.length; i < il; i ++ ) {
  392. programInfo = _programs[ i ];
  393. if ( programInfo.program === program ) {
  394. programInfo.usedTimes --;
  395. if ( programInfo.usedTimes === 0 ) {
  396. deleteProgram = true;
  397. }
  398. break;
  399. }
  400. }
  401. if ( deleteProgram === true ) {
  402. // avoid using array.splice, this is costlier than creating new array from scratch
  403. var newPrograms = [];
  404. for ( i = 0, il = _programs.length; i < il; i ++ ) {
  405. programInfo = _programs[ i ];
  406. if ( programInfo.program !== program ) {
  407. newPrograms.push( programInfo );
  408. }
  409. }
  410. _programs = newPrograms;
  411. _gl.deleteProgram( program );
  412. _this.info.memory.programs --;
  413. }
  414. };
  415. //
  416. /*
  417. function deleteParticleBuffers ( geometry ) {
  418. _gl.deleteBuffer( geometry.__webglVertexBuffer );
  419. _gl.deleteBuffer( geometry.__webglColorBuffer );
  420. deleteCustomAttributesBuffers( geometry );
  421. _this.info.memory.geometries --;
  422. };
  423. function deleteLineBuffers ( geometry ) {
  424. _gl.deleteBuffer( geometry.__webglVertexBuffer );
  425. _gl.deleteBuffer( geometry.__webglColorBuffer );
  426. _gl.deleteBuffer( geometry.__webglLineDistanceBuffer );
  427. deleteCustomAttributesBuffers( geometry );
  428. _this.info.memory.geometries --;
  429. };
  430. function deleteRibbonBuffers ( geometry ) {
  431. _gl.deleteBuffer( geometry.__webglVertexBuffer );
  432. _gl.deleteBuffer( geometry.__webglColorBuffer );
  433. _gl.deleteBuffer( geometry.__webglNormalBuffer );
  434. deleteCustomAttributesBuffers( geometry );
  435. _this.info.memory.geometries --;
  436. };
  437. function deleteMeshBuffers ( geometryGroup ) {
  438. _gl.deleteBuffer( geometryGroup.__webglVertexBuffer );
  439. _gl.deleteBuffer( geometryGroup.__webglNormalBuffer );
  440. _gl.deleteBuffer( geometryGroup.__webglTangentBuffer );
  441. _gl.deleteBuffer( geometryGroup.__webglColorBuffer );
  442. _gl.deleteBuffer( geometryGroup.__webglUVBuffer );
  443. _gl.deleteBuffer( geometryGroup.__webglUV2Buffer );
  444. _gl.deleteBuffer( geometryGroup.__webglSkinIndicesBuffer );
  445. _gl.deleteBuffer( geometryGroup.__webglSkinWeightsBuffer );
  446. _gl.deleteBuffer( geometryGroup.__webglFaceBuffer );
  447. _gl.deleteBuffer( geometryGroup.__webglLineBuffer );
  448. var m, ml;
  449. if ( geometryGroup.numMorphTargets ) {
  450. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  451. _gl.deleteBuffer( geometryGroup.__webglMorphTargetsBuffers[ m ] );
  452. }
  453. }
  454. if ( geometryGroup.numMorphNormals ) {
  455. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  456. _gl.deleteBuffer( geometryGroup.__webglMorphNormalsBuffers[ m ] );
  457. }
  458. }
  459. deleteCustomAttributesBuffers( geometryGroup );
  460. _this.info.memory.geometries --;
  461. };
  462. */
  463. function deleteCustomAttributesBuffers( geometry ) {
  464. if ( geometry.__webglCustomAttributesList ) {
  465. for ( var id in geometry.__webglCustomAttributesList ) {
  466. _gl.deleteBuffer( geometry.__webglCustomAttributesList[ id ].buffer );
  467. }
  468. }
  469. };
  470. // Buffer initialization
  471. function initCustomAttributes ( geometry, object ) {
  472. var nvertices = geometry.vertices.length;
  473. var material = object.material;
  474. if ( material.attributes ) {
  475. if ( geometry.__webglCustomAttributesList === undefined ) {
  476. geometry.__webglCustomAttributesList = [];
  477. }
  478. for ( var a in material.attributes ) {
  479. var attribute = material.attributes[ a ];
  480. if ( !attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  481. attribute.__webglInitialized = true;
  482. var size = 1; // "f" and "i"
  483. if ( attribute.type === "v2" ) size = 2;
  484. else if ( attribute.type === "v3" ) size = 3;
  485. else if ( attribute.type === "v4" ) size = 4;
  486. else if ( attribute.type === "c" ) size = 3;
  487. attribute.size = size;
  488. attribute.array = new Float32Array( nvertices * size );
  489. attribute.buffer = _gl.createBuffer();
  490. attribute.buffer.belongsToAttribute = a;
  491. attribute.needsUpdate = true;
  492. }
  493. geometry.__webglCustomAttributesList.push( attribute );
  494. }
  495. }
  496. };
  497. function initParticleBuffers ( geometry, object ) {
  498. var nvertices = geometry.vertices.length;
  499. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  500. geometry.__colorArray = new Float32Array( nvertices * 3 );
  501. geometry.__sortArray = [];
  502. geometry.__webglParticleCount = nvertices;
  503. initCustomAttributes ( geometry, object );
  504. };
  505. function initLineBuffers ( geometry, object ) {
  506. var nvertices = geometry.vertices.length;
  507. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  508. geometry.__colorArray = new Float32Array( nvertices * 3 );
  509. geometry.__lineDistanceArray = new Float32Array( nvertices * 1 );
  510. geometry.__webglLineCount = nvertices;
  511. initCustomAttributes ( geometry, object );
  512. };
  513. function initRibbonBuffers ( geometry, object ) {
  514. var nvertices = geometry.vertices.length;
  515. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  516. geometry.__colorArray = new Float32Array( nvertices * 3 );
  517. geometry.__normalArray = new Float32Array( nvertices * 3 );
  518. geometry.__webglVertexCount = nvertices;
  519. initCustomAttributes ( geometry, object );
  520. };
  521. function initMeshBuffers ( geometryGroup, object ) {
  522. var geometry = object.geometry,
  523. faces3 = geometryGroup.faces3,
  524. faces4 = geometryGroup.faces4,
  525. nvertices = faces3.length * 3 + faces4.length * 4,
  526. ntris = faces3.length * 1 + faces4.length * 2,
  527. nlines = faces3.length * 3 + faces4.length * 4,
  528. material = getBufferMaterial( object, geometryGroup ),
  529. uvType = bufferGuessUVType( material ),
  530. normalType = bufferGuessNormalType( material ),
  531. vertexColorType = bufferGuessVertexColorType( material );
  532. //console.log( "uvType", uvType, "normalType", normalType, "vertexColorType", vertexColorType, object, geometryGroup, material );
  533. geometryGroup.__vertexArray = new Float32Array( nvertices * 3 );
  534. if ( normalType ) {
  535. geometryGroup.__normalArray = new Float32Array( nvertices * 3 );
  536. }
  537. if ( geometry.hasTangents ) {
  538. geometryGroup.__tangentArray = new Float32Array( nvertices * 4 );
  539. }
  540. if ( vertexColorType ) {
  541. geometryGroup.__colorArray = new Float32Array( nvertices * 3 );
  542. }
  543. if ( uvType ) {
  544. if ( geometry.faceUvs.length > 0 || geometry.faceVertexUvs.length > 0 ) {
  545. geometryGroup.__uvArray = new Float32Array( nvertices * 2 );
  546. }
  547. if ( geometry.faceUvs.length > 1 || geometry.faceVertexUvs.length > 1 ) {
  548. geometryGroup.__uv2Array = new Float32Array( nvertices * 2 );
  549. }
  550. }
  551. if ( object.geometry.skinWeights.length && object.geometry.skinIndices.length ) {
  552. geometryGroup.__skinIndexArray = new Float32Array( nvertices * 4 );
  553. geometryGroup.__skinWeightArray = new Float32Array( nvertices * 4 );
  554. }
  555. geometryGroup.__faceArray = new Uint16Array( ntris * 3 );
  556. geometryGroup.__lineArray = new Uint16Array( nlines * 2 );
  557. var m, ml;
  558. if ( geometryGroup.numMorphTargets ) {
  559. geometryGroup.__morphTargetsArrays = [];
  560. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  561. geometryGroup.__morphTargetsArrays.push( new Float32Array( nvertices * 3 ) );
  562. }
  563. }
  564. if ( geometryGroup.numMorphNormals ) {
  565. geometryGroup.__morphNormalsArrays = [];
  566. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  567. geometryGroup.__morphNormalsArrays.push( new Float32Array( nvertices * 3 ) );
  568. }
  569. }
  570. geometryGroup.__webglFaceCount = ntris * 3;
  571. geometryGroup.__webglLineCount = nlines * 2;
  572. // custom attributes
  573. if ( material.attributes ) {
  574. if ( geometryGroup.__webglCustomAttributesList === undefined ) {
  575. geometryGroup.__webglCustomAttributesList = [];
  576. }
  577. for ( var a in material.attributes ) {
  578. // Do a shallow copy of the attribute object so different geometryGroup chunks use different
  579. // attribute buffers which are correctly indexed in the setMeshBuffers function
  580. var originalAttribute = material.attributes[ a ];
  581. var attribute = {};
  582. for ( var property in originalAttribute ) {
  583. attribute[ property ] = originalAttribute[ property ];
  584. }
  585. if ( !attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  586. attribute.__webglInitialized = true;
  587. var size = 1; // "f" and "i"
  588. if( attribute.type === "v2" ) size = 2;
  589. else if( attribute.type === "v3" ) size = 3;
  590. else if( attribute.type === "v4" ) size = 4;
  591. else if( attribute.type === "c" ) size = 3;
  592. attribute.size = size;
  593. attribute.array = new Float32Array( nvertices * size );
  594. attribute.buffer = _gl.createBuffer();
  595. attribute.buffer.belongsToAttribute = a;
  596. originalAttribute.needsUpdate = true;
  597. attribute.__original = originalAttribute;
  598. }
  599. geometryGroup.__webglCustomAttributesList.push( attribute );
  600. }
  601. }
  602. geometryGroup.__inittedArrays = true;
  603. };
  604. function getBufferMaterial( object, geometryGroup ) {
  605. return object.material instanceof THREE.MeshFaceMaterial
  606. ? object.material.materials[ geometryGroup.materialIndex ]
  607. : object.material;
  608. };
  609. function materialNeedsSmoothNormals ( material ) {
  610. return material && material.shading !== undefined && material.shading === THREE.SmoothShading;
  611. };
  612. function bufferGuessNormalType ( material ) {
  613. // only MeshBasicMaterial and MeshDepthMaterial don't need normals
  614. if ( ( material instanceof THREE.MeshBasicMaterial && !material.envMap ) || material instanceof THREE.MeshDepthMaterial ) {
  615. return false;
  616. }
  617. if ( materialNeedsSmoothNormals( material ) ) {
  618. return THREE.SmoothShading;
  619. } else {
  620. return THREE.FlatShading;
  621. }
  622. };
  623. function bufferGuessVertexColorType ( material ) {
  624. if ( material.vertexColors ) {
  625. return material.vertexColors;
  626. }
  627. return false;
  628. };
  629. function bufferGuessUVType ( material ) {
  630. // material must use some texture to require uvs
  631. if ( material.map || material.lightMap || material.bumpMap || material.normalMap || material.specularMap || material instanceof THREE.ShaderMaterial ) {
  632. return true;
  633. }
  634. return false;
  635. };
  636. //
  637. function initDirectBuffers( geometry ) {
  638. var a, attribute, type;
  639. for ( a in geometry.attributes ) {
  640. if ( a === "index" ) {
  641. type = _gl.ELEMENT_ARRAY_BUFFER;
  642. } else {
  643. type = _gl.ARRAY_BUFFER;
  644. }
  645. attribute = geometry.attributes[ a ];
  646. attribute.buffer = _gl.createBuffer();
  647. _gl.bindBuffer( type, attribute.buffer );
  648. _gl.bufferData( type, attribute.array, _gl.STATIC_DRAW );
  649. }
  650. };
  651. // Buffer setting
  652. function setParticleBuffers ( geometry, hint, object ) {
  653. var v, c, vertex, offset, index, color,
  654. vertices = geometry.vertices,
  655. vl = vertices.length,
  656. colors = geometry.colors,
  657. cl = colors.length,
  658. vertexArray = geometry.__vertexArray,
  659. colorArray = geometry.__colorArray,
  660. sortArray = geometry.__sortArray,
  661. dirtyVertices = geometry.verticesNeedUpdate,
  662. dirtyElements = geometry.elementsNeedUpdate,
  663. dirtyColors = geometry.colorsNeedUpdate,
  664. customAttributes = geometry.__webglCustomAttributesList,
  665. i, il,
  666. a, ca, cal, value,
  667. customAttribute;
  668. if ( object.sortParticles ) {
  669. _projScreenMatrixPS.copy( _projScreenMatrix );
  670. _projScreenMatrixPS.multiply( object.matrixWorld );
  671. for ( v = 0; v < vl; v ++ ) {
  672. vertex = vertices[ v ];
  673. _vector3.copy( vertex );
  674. _vector3.applyProjection( _projScreenMatrixPS );
  675. sortArray[ v ] = [ _vector3.z, v ];
  676. }
  677. sortArray.sort( numericalSort );
  678. for ( v = 0; v < vl; v ++ ) {
  679. vertex = vertices[ sortArray[v][1] ];
  680. offset = v * 3;
  681. vertexArray[ offset ] = vertex.x;
  682. vertexArray[ offset + 1 ] = vertex.y;
  683. vertexArray[ offset + 2 ] = vertex.z;
  684. }
  685. for ( c = 0; c < cl; c ++ ) {
  686. offset = c * 3;
  687. color = colors[ sortArray[c][1] ];
  688. colorArray[ offset ] = color.r;
  689. colorArray[ offset + 1 ] = color.g;
  690. colorArray[ offset + 2 ] = color.b;
  691. }
  692. if ( customAttributes ) {
  693. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  694. customAttribute = customAttributes[ i ];
  695. if ( ! ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) ) continue;
  696. offset = 0;
  697. cal = customAttribute.value.length;
  698. if ( customAttribute.size === 1 ) {
  699. for ( ca = 0; ca < cal; ca ++ ) {
  700. index = sortArray[ ca ][ 1 ];
  701. customAttribute.array[ ca ] = customAttribute.value[ index ];
  702. }
  703. } else if ( customAttribute.size === 2 ) {
  704. for ( ca = 0; ca < cal; ca ++ ) {
  705. index = sortArray[ ca ][ 1 ];
  706. value = customAttribute.value[ index ];
  707. customAttribute.array[ offset ] = value.x;
  708. customAttribute.array[ offset + 1 ] = value.y;
  709. offset += 2;
  710. }
  711. } else if ( customAttribute.size === 3 ) {
  712. if ( customAttribute.type === "c" ) {
  713. for ( ca = 0; ca < cal; ca ++ ) {
  714. index = sortArray[ ca ][ 1 ];
  715. value = customAttribute.value[ index ];
  716. customAttribute.array[ offset ] = value.r;
  717. customAttribute.array[ offset + 1 ] = value.g;
  718. customAttribute.array[ offset + 2 ] = value.b;
  719. offset += 3;
  720. }
  721. } else {
  722. for ( ca = 0; ca < cal; ca ++ ) {
  723. index = sortArray[ ca ][ 1 ];
  724. value = customAttribute.value[ index ];
  725. customAttribute.array[ offset ] = value.x;
  726. customAttribute.array[ offset + 1 ] = value.y;
  727. customAttribute.array[ offset + 2 ] = value.z;
  728. offset += 3;
  729. }
  730. }
  731. } else if ( customAttribute.size === 4 ) {
  732. for ( ca = 0; ca < cal; ca ++ ) {
  733. index = sortArray[ ca ][ 1 ];
  734. value = customAttribute.value[ index ];
  735. customAttribute.array[ offset ] = value.x;
  736. customAttribute.array[ offset + 1 ] = value.y;
  737. customAttribute.array[ offset + 2 ] = value.z;
  738. customAttribute.array[ offset + 3 ] = value.w;
  739. offset += 4;
  740. }
  741. }
  742. }
  743. }
  744. } else {
  745. if ( dirtyVertices ) {
  746. for ( v = 0; v < vl; v ++ ) {
  747. vertex = vertices[ v ];
  748. offset = v * 3;
  749. vertexArray[ offset ] = vertex.x;
  750. vertexArray[ offset + 1 ] = vertex.y;
  751. vertexArray[ offset + 2 ] = vertex.z;
  752. }
  753. }
  754. if ( dirtyColors ) {
  755. for ( c = 0; c < cl; c ++ ) {
  756. color = colors[ c ];
  757. offset = c * 3;
  758. colorArray[ offset ] = color.r;
  759. colorArray[ offset + 1 ] = color.g;
  760. colorArray[ offset + 2 ] = color.b;
  761. }
  762. }
  763. if ( customAttributes ) {
  764. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  765. customAttribute = customAttributes[ i ];
  766. if ( customAttribute.needsUpdate &&
  767. ( customAttribute.boundTo === undefined ||
  768. customAttribute.boundTo === "vertices") ) {
  769. cal = customAttribute.value.length;
  770. offset = 0;
  771. if ( customAttribute.size === 1 ) {
  772. for ( ca = 0; ca < cal; ca ++ ) {
  773. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  774. }
  775. } else if ( customAttribute.size === 2 ) {
  776. for ( ca = 0; ca < cal; ca ++ ) {
  777. value = customAttribute.value[ ca ];
  778. customAttribute.array[ offset ] = value.x;
  779. customAttribute.array[ offset + 1 ] = value.y;
  780. offset += 2;
  781. }
  782. } else if ( customAttribute.size === 3 ) {
  783. if ( customAttribute.type === "c" ) {
  784. for ( ca = 0; ca < cal; ca ++ ) {
  785. value = customAttribute.value[ ca ];
  786. customAttribute.array[ offset ] = value.r;
  787. customAttribute.array[ offset + 1 ] = value.g;
  788. customAttribute.array[ offset + 2 ] = value.b;
  789. offset += 3;
  790. }
  791. } else {
  792. for ( ca = 0; ca < cal; ca ++ ) {
  793. value = customAttribute.value[ ca ];
  794. customAttribute.array[ offset ] = value.x;
  795. customAttribute.array[ offset + 1 ] = value.y;
  796. customAttribute.array[ offset + 2 ] = value.z;
  797. offset += 3;
  798. }
  799. }
  800. } else if ( customAttribute.size === 4 ) {
  801. for ( ca = 0; ca < cal; ca ++ ) {
  802. value = customAttribute.value[ ca ];
  803. customAttribute.array[ offset ] = value.x;
  804. customAttribute.array[ offset + 1 ] = value.y;
  805. customAttribute.array[ offset + 2 ] = value.z;
  806. customAttribute.array[ offset + 3 ] = value.w;
  807. offset += 4;
  808. }
  809. }
  810. }
  811. }
  812. }
  813. }
  814. if ( dirtyVertices || object.sortParticles ) {
  815. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  816. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  817. }
  818. if ( dirtyColors || object.sortParticles ) {
  819. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  820. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  821. }
  822. if ( customAttributes ) {
  823. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  824. customAttribute = customAttributes[ i ];
  825. if ( customAttribute.needsUpdate || object.sortParticles ) {
  826. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  827. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  828. }
  829. }
  830. }
  831. };
  832. function setLineBuffers ( geometry, hint ) {
  833. var v, c, d, vertex, offset, color,
  834. vertices = geometry.vertices,
  835. colors = geometry.colors,
  836. lineDistances = geometry.lineDistances,
  837. vl = vertices.length,
  838. cl = colors.length,
  839. dl = lineDistances.length,
  840. vertexArray = geometry.__vertexArray,
  841. colorArray = geometry.__colorArray,
  842. lineDistanceArray = geometry.__lineDistanceArray,
  843. dirtyVertices = geometry.verticesNeedUpdate,
  844. dirtyColors = geometry.colorsNeedUpdate,
  845. dirtyLineDistances = geometry.lineDistancesNeedUpdate,
  846. customAttributes = geometry.__webglCustomAttributesList,
  847. i, il,
  848. a, ca, cal, value,
  849. customAttribute;
  850. if ( dirtyVertices ) {
  851. for ( v = 0; v < vl; v ++ ) {
  852. vertex = vertices[ v ];
  853. offset = v * 3;
  854. vertexArray[ offset ] = vertex.x;
  855. vertexArray[ offset + 1 ] = vertex.y;
  856. vertexArray[ offset + 2 ] = vertex.z;
  857. }
  858. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  859. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  860. }
  861. if ( dirtyColors ) {
  862. for ( c = 0; c < cl; c ++ ) {
  863. color = colors[ c ];
  864. offset = c * 3;
  865. colorArray[ offset ] = color.r;
  866. colorArray[ offset + 1 ] = color.g;
  867. colorArray[ offset + 2 ] = color.b;
  868. }
  869. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  870. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  871. }
  872. if ( dirtyLineDistances ) {
  873. for ( d = 0; d < dl; d ++ ) {
  874. lineDistanceArray[ d ] = lineDistances[ d ];
  875. }
  876. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglLineDistanceBuffer );
  877. _gl.bufferData( _gl.ARRAY_BUFFER, lineDistanceArray, hint );
  878. }
  879. if ( customAttributes ) {
  880. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  881. customAttribute = customAttributes[ i ];
  882. if ( customAttribute.needsUpdate &&
  883. ( customAttribute.boundTo === undefined ||
  884. customAttribute.boundTo === "vertices" ) ) {
  885. offset = 0;
  886. cal = customAttribute.value.length;
  887. if ( customAttribute.size === 1 ) {
  888. for ( ca = 0; ca < cal; ca ++ ) {
  889. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  890. }
  891. } else if ( customAttribute.size === 2 ) {
  892. for ( ca = 0; ca < cal; ca ++ ) {
  893. value = customAttribute.value[ ca ];
  894. customAttribute.array[ offset ] = value.x;
  895. customAttribute.array[ offset + 1 ] = value.y;
  896. offset += 2;
  897. }
  898. } else if ( customAttribute.size === 3 ) {
  899. if ( customAttribute.type === "c" ) {
  900. for ( ca = 0; ca < cal; ca ++ ) {
  901. value = customAttribute.value[ ca ];
  902. customAttribute.array[ offset ] = value.r;
  903. customAttribute.array[ offset + 1 ] = value.g;
  904. customAttribute.array[ offset + 2 ] = value.b;
  905. offset += 3;
  906. }
  907. } else {
  908. for ( ca = 0; ca < cal; ca ++ ) {
  909. value = customAttribute.value[ ca ];
  910. customAttribute.array[ offset ] = value.x;
  911. customAttribute.array[ offset + 1 ] = value.y;
  912. customAttribute.array[ offset + 2 ] = value.z;
  913. offset += 3;
  914. }
  915. }
  916. } else if ( customAttribute.size === 4 ) {
  917. for ( ca = 0; ca < cal; ca ++ ) {
  918. value = customAttribute.value[ ca ];
  919. customAttribute.array[ offset ] = value.x;
  920. customAttribute.array[ offset + 1 ] = value.y;
  921. customAttribute.array[ offset + 2 ] = value.z;
  922. customAttribute.array[ offset + 3 ] = value.w;
  923. offset += 4;
  924. }
  925. }
  926. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  927. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  928. }
  929. }
  930. }
  931. };
  932. function setRibbonBuffers ( geometry, hint ) {
  933. var v, c, n, vertex, offset, color, normal,
  934. i, il, ca, cal, customAttribute, value,
  935. vertices = geometry.vertices,
  936. colors = geometry.colors,
  937. normals = geometry.normals,
  938. vl = vertices.length,
  939. cl = colors.length,
  940. nl = normals.length,
  941. vertexArray = geometry.__vertexArray,
  942. colorArray = geometry.__colorArray,
  943. normalArray = geometry.__normalArray,
  944. dirtyVertices = geometry.verticesNeedUpdate,
  945. dirtyColors = geometry.colorsNeedUpdate,
  946. dirtyNormals = geometry.normalsNeedUpdate,
  947. customAttributes = geometry.__webglCustomAttributesList;
  948. if ( dirtyVertices ) {
  949. for ( v = 0; v < vl; v ++ ) {
  950. vertex = vertices[ v ];
  951. offset = v * 3;
  952. vertexArray[ offset ] = vertex.x;
  953. vertexArray[ offset + 1 ] = vertex.y;
  954. vertexArray[ offset + 2 ] = vertex.z;
  955. }
  956. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  957. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  958. }
  959. if ( dirtyColors ) {
  960. for ( c = 0; c < cl; c ++ ) {
  961. color = colors[ c ];
  962. offset = c * 3;
  963. colorArray[ offset ] = color.r;
  964. colorArray[ offset + 1 ] = color.g;
  965. colorArray[ offset + 2 ] = color.b;
  966. }
  967. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  968. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  969. }
  970. if ( dirtyNormals ) {
  971. for ( n = 0; n < nl; n ++ ) {
  972. normal = normals[ n ];
  973. offset = n * 3;
  974. normalArray[ offset ] = normal.x;
  975. normalArray[ offset + 1 ] = normal.y;
  976. normalArray[ offset + 2 ] = normal.z;
  977. }
  978. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglNormalBuffer );
  979. _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint );
  980. }
  981. if ( customAttributes ) {
  982. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  983. customAttribute = customAttributes[ i ];
  984. if ( customAttribute.needsUpdate &&
  985. ( customAttribute.boundTo === undefined ||
  986. customAttribute.boundTo === "vertices" ) ) {
  987. offset = 0;
  988. cal = customAttribute.value.length;
  989. if ( customAttribute.size === 1 ) {
  990. for ( ca = 0; ca < cal; ca ++ ) {
  991. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  992. }
  993. } else if ( customAttribute.size === 2 ) {
  994. for ( ca = 0; ca < cal; ca ++ ) {
  995. value = customAttribute.value[ ca ];
  996. customAttribute.array[ offset ] = value.x;
  997. customAttribute.array[ offset + 1 ] = value.y;
  998. offset += 2;
  999. }
  1000. } else if ( customAttribute.size === 3 ) {
  1001. if ( customAttribute.type === "c" ) {
  1002. for ( ca = 0; ca < cal; ca ++ ) {
  1003. value = customAttribute.value[ ca ];
  1004. customAttribute.array[ offset ] = value.r;
  1005. customAttribute.array[ offset + 1 ] = value.g;
  1006. customAttribute.array[ offset + 2 ] = value.b;
  1007. offset += 3;
  1008. }
  1009. } else {
  1010. for ( ca = 0; ca < cal; ca ++ ) {
  1011. value = customAttribute.value[ ca ];
  1012. customAttribute.array[ offset ] = value.x;
  1013. customAttribute.array[ offset + 1 ] = value.y;
  1014. customAttribute.array[ offset + 2 ] = value.z;
  1015. offset += 3;
  1016. }
  1017. }
  1018. } else if ( customAttribute.size === 4 ) {
  1019. for ( ca = 0; ca < cal; ca ++ ) {
  1020. value = customAttribute.value[ ca ];
  1021. customAttribute.array[ offset ] = value.x;
  1022. customAttribute.array[ offset + 1 ] = value.y;
  1023. customAttribute.array[ offset + 2 ] = value.z;
  1024. customAttribute.array[ offset + 3 ] = value.w;
  1025. offset += 4;
  1026. }
  1027. }
  1028. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  1029. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  1030. }
  1031. }
  1032. }
  1033. };
  1034. function setMeshBuffers( geometryGroup, object, hint, dispose, material ) {
  1035. if ( ! geometryGroup.__inittedArrays ) {
  1036. return;
  1037. }
  1038. var normalType = bufferGuessNormalType( material ),
  1039. vertexColorType = bufferGuessVertexColorType( material ),
  1040. uvType = bufferGuessUVType( material ),
  1041. needsSmoothNormals = ( normalType === THREE.SmoothShading );
  1042. var f, fl, fi, face,
  1043. vertexNormals, faceNormal, normal,
  1044. vertexColors, faceColor,
  1045. vertexTangents,
  1046. uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4, n1, n2, n3, n4,
  1047. c1, c2, c3, c4,
  1048. sw1, sw2, sw3, sw4,
  1049. si1, si2, si3, si4,
  1050. sa1, sa2, sa3, sa4,
  1051. sb1, sb2, sb3, sb4,
  1052. m, ml, i, il,
  1053. vn, uvi, uv2i,
  1054. vk, vkl, vka,
  1055. nka, chf, faceVertexNormals,
  1056. a,
  1057. vertexIndex = 0,
  1058. offset = 0,
  1059. offset_uv = 0,
  1060. offset_uv2 = 0,
  1061. offset_face = 0,
  1062. offset_normal = 0,
  1063. offset_tangent = 0,
  1064. offset_line = 0,
  1065. offset_color = 0,
  1066. offset_skin = 0,
  1067. offset_morphTarget = 0,
  1068. offset_custom = 0,
  1069. offset_customSrc = 0,
  1070. value,
  1071. vertexArray = geometryGroup.__vertexArray,
  1072. uvArray = geometryGroup.__uvArray,
  1073. uv2Array = geometryGroup.__uv2Array,
  1074. normalArray = geometryGroup.__normalArray,
  1075. tangentArray = geometryGroup.__tangentArray,
  1076. colorArray = geometryGroup.__colorArray,
  1077. skinIndexArray = geometryGroup.__skinIndexArray,
  1078. skinWeightArray = geometryGroup.__skinWeightArray,
  1079. morphTargetsArrays = geometryGroup.__morphTargetsArrays,
  1080. morphNormalsArrays = geometryGroup.__morphNormalsArrays,
  1081. customAttributes = geometryGroup.__webglCustomAttributesList,
  1082. customAttribute,
  1083. faceArray = geometryGroup.__faceArray,
  1084. lineArray = geometryGroup.__lineArray,
  1085. geometry = object.geometry, // this is shared for all chunks
  1086. dirtyVertices = geometry.verticesNeedUpdate,
  1087. dirtyElements = geometry.elementsNeedUpdate,
  1088. dirtyUvs = geometry.uvsNeedUpdate,
  1089. dirtyNormals = geometry.normalsNeedUpdate,
  1090. dirtyTangents = geometry.tangentsNeedUpdate,
  1091. dirtyColors = geometry.colorsNeedUpdate,
  1092. dirtyMorphTargets = geometry.morphTargetsNeedUpdate,
  1093. vertices = geometry.vertices,
  1094. chunk_faces3 = geometryGroup.faces3,
  1095. chunk_faces4 = geometryGroup.faces4,
  1096. obj_faces = geometry.faces,
  1097. obj_uvs = geometry.faceVertexUvs[ 0 ],
  1098. obj_uvs2 = geometry.faceVertexUvs[ 1 ],
  1099. obj_colors = geometry.colors,
  1100. obj_skinIndices = geometry.skinIndices,
  1101. obj_skinWeights = geometry.skinWeights,
  1102. morphTargets = geometry.morphTargets,
  1103. morphNormals = geometry.morphNormals;
  1104. if ( dirtyVertices ) {
  1105. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1106. face = obj_faces[ chunk_faces3[ f ] ];
  1107. v1 = vertices[ face.a ];
  1108. v2 = vertices[ face.b ];
  1109. v3 = vertices[ face.c ];
  1110. vertexArray[ offset ] = v1.x;
  1111. vertexArray[ offset + 1 ] = v1.y;
  1112. vertexArray[ offset + 2 ] = v1.z;
  1113. vertexArray[ offset + 3 ] = v2.x;
  1114. vertexArray[ offset + 4 ] = v2.y;
  1115. vertexArray[ offset + 5 ] = v2.z;
  1116. vertexArray[ offset + 6 ] = v3.x;
  1117. vertexArray[ offset + 7 ] = v3.y;
  1118. vertexArray[ offset + 8 ] = v3.z;
  1119. offset += 9;
  1120. }
  1121. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1122. face = obj_faces[ chunk_faces4[ f ] ];
  1123. v1 = vertices[ face.a ];
  1124. v2 = vertices[ face.b ];
  1125. v3 = vertices[ face.c ];
  1126. v4 = vertices[ face.d ];
  1127. vertexArray[ offset ] = v1.x;
  1128. vertexArray[ offset + 1 ] = v1.y;
  1129. vertexArray[ offset + 2 ] = v1.z;
  1130. vertexArray[ offset + 3 ] = v2.x;
  1131. vertexArray[ offset + 4 ] = v2.y;
  1132. vertexArray[ offset + 5 ] = v2.z;
  1133. vertexArray[ offset + 6 ] = v3.x;
  1134. vertexArray[ offset + 7 ] = v3.y;
  1135. vertexArray[ offset + 8 ] = v3.z;
  1136. vertexArray[ offset + 9 ] = v4.x;
  1137. vertexArray[ offset + 10 ] = v4.y;
  1138. vertexArray[ offset + 11 ] = v4.z;
  1139. offset += 12;
  1140. }
  1141. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  1142. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  1143. }
  1144. if ( dirtyMorphTargets ) {
  1145. for ( vk = 0, vkl = morphTargets.length; vk < vkl; vk ++ ) {
  1146. offset_morphTarget = 0;
  1147. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1148. chf = chunk_faces3[ f ];
  1149. face = obj_faces[ chf ];
  1150. // morph positions
  1151. v1 = morphTargets[ vk ].vertices[ face.a ];
  1152. v2 = morphTargets[ vk ].vertices[ face.b ];
  1153. v3 = morphTargets[ vk ].vertices[ face.c ];
  1154. vka = morphTargetsArrays[ vk ];
  1155. vka[ offset_morphTarget ] = v1.x;
  1156. vka[ offset_morphTarget + 1 ] = v1.y;
  1157. vka[ offset_morphTarget + 2 ] = v1.z;
  1158. vka[ offset_morphTarget + 3 ] = v2.x;
  1159. vka[ offset_morphTarget + 4 ] = v2.y;
  1160. vka[ offset_morphTarget + 5 ] = v2.z;
  1161. vka[ offset_morphTarget + 6 ] = v3.x;
  1162. vka[ offset_morphTarget + 7 ] = v3.y;
  1163. vka[ offset_morphTarget + 8 ] = v3.z;
  1164. // morph normals
  1165. if ( material.morphNormals ) {
  1166. if ( needsSmoothNormals ) {
  1167. faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
  1168. n1 = faceVertexNormals.a;
  1169. n2 = faceVertexNormals.b;
  1170. n3 = faceVertexNormals.c;
  1171. } else {
  1172. n1 = morphNormals[ vk ].faceNormals[ chf ];
  1173. n2 = n1;
  1174. n3 = n1;
  1175. }
  1176. nka = morphNormalsArrays[ vk ];
  1177. nka[ offset_morphTarget ] = n1.x;
  1178. nka[ offset_morphTarget + 1 ] = n1.y;
  1179. nka[ offset_morphTarget + 2 ] = n1.z;
  1180. nka[ offset_morphTarget + 3 ] = n2.x;
  1181. nka[ offset_morphTarget + 4 ] = n2.y;
  1182. nka[ offset_morphTarget + 5 ] = n2.z;
  1183. nka[ offset_morphTarget + 6 ] = n3.x;
  1184. nka[ offset_morphTarget + 7 ] = n3.y;
  1185. nka[ offset_morphTarget + 8 ] = n3.z;
  1186. }
  1187. //
  1188. offset_morphTarget += 9;
  1189. }
  1190. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1191. chf = chunk_faces4[ f ];
  1192. face = obj_faces[ chf ];
  1193. // morph positions
  1194. v1 = morphTargets[ vk ].vertices[ face.a ];
  1195. v2 = morphTargets[ vk ].vertices[ face.b ];
  1196. v3 = morphTargets[ vk ].vertices[ face.c ];
  1197. v4 = morphTargets[ vk ].vertices[ face.d ];
  1198. vka = morphTargetsArrays[ vk ];
  1199. vka[ offset_morphTarget ] = v1.x;
  1200. vka[ offset_morphTarget + 1 ] = v1.y;
  1201. vka[ offset_morphTarget + 2 ] = v1.z;
  1202. vka[ offset_morphTarget + 3 ] = v2.x;
  1203. vka[ offset_morphTarget + 4 ] = v2.y;
  1204. vka[ offset_morphTarget + 5 ] = v2.z;
  1205. vka[ offset_morphTarget + 6 ] = v3.x;
  1206. vka[ offset_morphTarget + 7 ] = v3.y;
  1207. vka[ offset_morphTarget + 8 ] = v3.z;
  1208. vka[ offset_morphTarget + 9 ] = v4.x;
  1209. vka[ offset_morphTarget + 10 ] = v4.y;
  1210. vka[ offset_morphTarget + 11 ] = v4.z;
  1211. // morph normals
  1212. if ( material.morphNormals ) {
  1213. if ( needsSmoothNormals ) {
  1214. faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
  1215. n1 = faceVertexNormals.a;
  1216. n2 = faceVertexNormals.b;
  1217. n3 = faceVertexNormals.c;
  1218. n4 = faceVertexNormals.d;
  1219. } else {
  1220. n1 = morphNormals[ vk ].faceNormals[ chf ];
  1221. n2 = n1;
  1222. n3 = n1;
  1223. n4 = n1;
  1224. }
  1225. nka = morphNormalsArrays[ vk ];
  1226. nka[ offset_morphTarget ] = n1.x;
  1227. nka[ offset_morphTarget + 1 ] = n1.y;
  1228. nka[ offset_morphTarget + 2 ] = n1.z;
  1229. nka[ offset_morphTarget + 3 ] = n2.x;
  1230. nka[ offset_morphTarget + 4 ] = n2.y;
  1231. nka[ offset_morphTarget + 5 ] = n2.z;
  1232. nka[ offset_morphTarget + 6 ] = n3.x;
  1233. nka[ offset_morphTarget + 7 ] = n3.y;
  1234. nka[ offset_morphTarget + 8 ] = n3.z;
  1235. nka[ offset_morphTarget + 9 ] = n4.x;
  1236. nka[ offset_morphTarget + 10 ] = n4.y;
  1237. nka[ offset_morphTarget + 11 ] = n4.z;
  1238. }
  1239. //
  1240. offset_morphTarget += 12;
  1241. }
  1242. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ vk ] );
  1243. _gl.bufferData( _gl.ARRAY_BUFFER, morphTargetsArrays[ vk ], hint );
  1244. if ( material.morphNormals ) {
  1245. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ vk ] );
  1246. _gl.bufferData( _gl.ARRAY_BUFFER, morphNormalsArrays[ vk ], hint );
  1247. }
  1248. }
  1249. }
  1250. if ( obj_skinWeights.length ) {
  1251. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1252. face = obj_faces[ chunk_faces3[ f ] ];
  1253. // weights
  1254. sw1 = obj_skinWeights[ face.a ];
  1255. sw2 = obj_skinWeights[ face.b ];
  1256. sw3 = obj_skinWeights[ face.c ];
  1257. skinWeightArray[ offset_skin ] = sw1.x;
  1258. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  1259. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  1260. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  1261. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  1262. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  1263. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  1264. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  1265. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  1266. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  1267. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  1268. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  1269. // indices
  1270. si1 = obj_skinIndices[ face.a ];
  1271. si2 = obj_skinIndices[ face.b ];
  1272. si3 = obj_skinIndices[ face.c ];
  1273. skinIndexArray[ offset_skin ] = si1.x;
  1274. skinIndexArray[ offset_skin + 1 ] = si1.y;
  1275. skinIndexArray[ offset_skin + 2 ] = si1.z;
  1276. skinIndexArray[ offset_skin + 3 ] = si1.w;
  1277. skinIndexArray[ offset_skin + 4 ] = si2.x;
  1278. skinIndexArray[ offset_skin + 5 ] = si2.y;
  1279. skinIndexArray[ offset_skin + 6 ] = si2.z;
  1280. skinIndexArray[ offset_skin + 7 ] = si2.w;
  1281. skinIndexArray[ offset_skin + 8 ] = si3.x;
  1282. skinIndexArray[ offset_skin + 9 ] = si3.y;
  1283. skinIndexArray[ offset_skin + 10 ] = si3.z;
  1284. skinIndexArray[ offset_skin + 11 ] = si3.w;
  1285. offset_skin += 12;
  1286. }
  1287. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1288. face = obj_faces[ chunk_faces4[ f ] ];
  1289. // weights
  1290. sw1 = obj_skinWeights[ face.a ];
  1291. sw2 = obj_skinWeights[ face.b ];
  1292. sw3 = obj_skinWeights[ face.c ];
  1293. sw4 = obj_skinWeights[ face.d ];
  1294. skinWeightArray[ offset_skin ] = sw1.x;
  1295. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  1296. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  1297. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  1298. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  1299. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  1300. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  1301. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  1302. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  1303. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  1304. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  1305. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  1306. skinWeightArray[ offset_skin + 12 ] = sw4.x;
  1307. skinWeightArray[ offset_skin + 13 ] = sw4.y;
  1308. skinWeightArray[ offset_skin + 14 ] = sw4.z;
  1309. skinWeightArray[ offset_skin + 15 ] = sw4.w;
  1310. // indices
  1311. si1 = obj_skinIndices[ face.a ];
  1312. si2 = obj_skinIndices[ face.b ];
  1313. si3 = obj_skinIndices[ face.c ];
  1314. si4 = obj_skinIndices[ face.d ];
  1315. skinIndexArray[ offset_skin ] = si1.x;
  1316. skinIndexArray[ offset_skin + 1 ] = si1.y;
  1317. skinIndexArray[ offset_skin + 2 ] = si1.z;
  1318. skinIndexArray[ offset_skin + 3 ] = si1.w;
  1319. skinIndexArray[ offset_skin + 4 ] = si2.x;
  1320. skinIndexArray[ offset_skin + 5 ] = si2.y;
  1321. skinIndexArray[ offset_skin + 6 ] = si2.z;
  1322. skinIndexArray[ offset_skin + 7 ] = si2.w;
  1323. skinIndexArray[ offset_skin + 8 ] = si3.x;
  1324. skinIndexArray[ offset_skin + 9 ] = si3.y;
  1325. skinIndexArray[ offset_skin + 10 ] = si3.z;
  1326. skinIndexArray[ offset_skin + 11 ] = si3.w;
  1327. skinIndexArray[ offset_skin + 12 ] = si4.x;
  1328. skinIndexArray[ offset_skin + 13 ] = si4.y;
  1329. skinIndexArray[ offset_skin + 14 ] = si4.z;
  1330. skinIndexArray[ offset_skin + 15 ] = si4.w;
  1331. offset_skin += 16;
  1332. }
  1333. if ( offset_skin > 0 ) {
  1334. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  1335. _gl.bufferData( _gl.ARRAY_BUFFER, skinIndexArray, hint );
  1336. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  1337. _gl.bufferData( _gl.ARRAY_BUFFER, skinWeightArray, hint );
  1338. }
  1339. }
  1340. if ( dirtyColors && vertexColorType ) {
  1341. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1342. face = obj_faces[ chunk_faces3[ f ] ];
  1343. vertexColors = face.vertexColors;
  1344. faceColor = face.color;
  1345. if ( vertexColors.length === 3 && vertexColorType === THREE.VertexColors ) {
  1346. c1 = vertexColors[ 0 ];
  1347. c2 = vertexColors[ 1 ];
  1348. c3 = vertexColors[ 2 ];
  1349. } else {
  1350. c1 = faceColor;
  1351. c2 = faceColor;
  1352. c3 = faceColor;
  1353. }
  1354. colorArray[ offset_color ] = c1.r;
  1355. colorArray[ offset_color + 1 ] = c1.g;
  1356. colorArray[ offset_color + 2 ] = c1.b;
  1357. colorArray[ offset_color + 3 ] = c2.r;
  1358. colorArray[ offset_color + 4 ] = c2.g;
  1359. colorArray[ offset_color + 5 ] = c2.b;
  1360. colorArray[ offset_color + 6 ] = c3.r;
  1361. colorArray[ offset_color + 7 ] = c3.g;
  1362. colorArray[ offset_color + 8 ] = c3.b;
  1363. offset_color += 9;
  1364. }
  1365. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1366. face = obj_faces[ chunk_faces4[ f ] ];
  1367. vertexColors = face.vertexColors;
  1368. faceColor = face.color;
  1369. if ( vertexColors.length === 4 && vertexColorType === THREE.VertexColors ) {
  1370. c1 = vertexColors[ 0 ];
  1371. c2 = vertexColors[ 1 ];
  1372. c3 = vertexColors[ 2 ];
  1373. c4 = vertexColors[ 3 ];
  1374. } else {
  1375. c1 = faceColor;
  1376. c2 = faceColor;
  1377. c3 = faceColor;
  1378. c4 = faceColor;
  1379. }
  1380. colorArray[ offset_color ] = c1.r;
  1381. colorArray[ offset_color + 1 ] = c1.g;
  1382. colorArray[ offset_color + 2 ] = c1.b;
  1383. colorArray[ offset_color + 3 ] = c2.r;
  1384. colorArray[ offset_color + 4 ] = c2.g;
  1385. colorArray[ offset_color + 5 ] = c2.b;
  1386. colorArray[ offset_color + 6 ] = c3.r;
  1387. colorArray[ offset_color + 7 ] = c3.g;
  1388. colorArray[ offset_color + 8 ] = c3.b;
  1389. colorArray[ offset_color + 9 ] = c4.r;
  1390. colorArray[ offset_color + 10 ] = c4.g;
  1391. colorArray[ offset_color + 11 ] = c4.b;
  1392. offset_color += 12;
  1393. }
  1394. if ( offset_color > 0 ) {
  1395. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  1396. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  1397. }
  1398. }
  1399. if ( dirtyTangents && geometry.hasTangents ) {
  1400. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1401. face = obj_faces[ chunk_faces3[ f ] ];
  1402. vertexTangents = face.vertexTangents;
  1403. t1 = vertexTangents[ 0 ];
  1404. t2 = vertexTangents[ 1 ];
  1405. t3 = vertexTangents[ 2 ];
  1406. tangentArray[ offset_tangent ] = t1.x;
  1407. tangentArray[ offset_tangent + 1 ] = t1.y;
  1408. tangentArray[ offset_tangent + 2 ] = t1.z;
  1409. tangentArray[ offset_tangent + 3 ] = t1.w;
  1410. tangentArray[ offset_tangent + 4 ] = t2.x;
  1411. tangentArray[ offset_tangent + 5 ] = t2.y;
  1412. tangentArray[ offset_tangent + 6 ] = t2.z;
  1413. tangentArray[ offset_tangent + 7 ] = t2.w;
  1414. tangentArray[ offset_tangent + 8 ] = t3.x;
  1415. tangentArray[ offset_tangent + 9 ] = t3.y;
  1416. tangentArray[ offset_tangent + 10 ] = t3.z;
  1417. tangentArray[ offset_tangent + 11 ] = t3.w;
  1418. offset_tangent += 12;
  1419. }
  1420. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1421. face = obj_faces[ chunk_faces4[ f ] ];
  1422. vertexTangents = face.vertexTangents;
  1423. t1 = vertexTangents[ 0 ];
  1424. t2 = vertexTangents[ 1 ];
  1425. t3 = vertexTangents[ 2 ];
  1426. t4 = vertexTangents[ 3 ];
  1427. tangentArray[ offset_tangent ] = t1.x;
  1428. tangentArray[ offset_tangent + 1 ] = t1.y;
  1429. tangentArray[ offset_tangent + 2 ] = t1.z;
  1430. tangentArray[ offset_tangent + 3 ] = t1.w;
  1431. tangentArray[ offset_tangent + 4 ] = t2.x;
  1432. tangentArray[ offset_tangent + 5 ] = t2.y;
  1433. tangentArray[ offset_tangent + 6 ] = t2.z;
  1434. tangentArray[ offset_tangent + 7 ] = t2.w;
  1435. tangentArray[ offset_tangent + 8 ] = t3.x;
  1436. tangentArray[ offset_tangent + 9 ] = t3.y;
  1437. tangentArray[ offset_tangent + 10 ] = t3.z;
  1438. tangentArray[ offset_tangent + 11 ] = t3.w;
  1439. tangentArray[ offset_tangent + 12 ] = t4.x;
  1440. tangentArray[ offset_tangent + 13 ] = t4.y;
  1441. tangentArray[ offset_tangent + 14 ] = t4.z;
  1442. tangentArray[ offset_tangent + 15 ] = t4.w;
  1443. offset_tangent += 16;
  1444. }
  1445. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  1446. _gl.bufferData( _gl.ARRAY_BUFFER, tangentArray, hint );
  1447. }
  1448. if ( dirtyNormals && normalType ) {
  1449. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1450. face = obj_faces[ chunk_faces3[ f ] ];
  1451. vertexNormals = face.vertexNormals;
  1452. faceNormal = face.normal;
  1453. if ( vertexNormals.length === 3 && needsSmoothNormals ) {
  1454. for ( i = 0; i < 3; i ++ ) {
  1455. vn = vertexNormals[ i ];
  1456. normalArray[ offset_normal ] = vn.x;
  1457. normalArray[ offset_normal + 1 ] = vn.y;
  1458. normalArray[ offset_normal + 2 ] = vn.z;
  1459. offset_normal += 3;
  1460. }
  1461. } else {
  1462. for ( i = 0; i < 3; i ++ ) {
  1463. normalArray[ offset_normal ] = faceNormal.x;
  1464. normalArray[ offset_normal + 1 ] = faceNormal.y;
  1465. normalArray[ offset_normal + 2 ] = faceNormal.z;
  1466. offset_normal += 3;
  1467. }
  1468. }
  1469. }
  1470. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1471. face = obj_faces[ chunk_faces4[ f ] ];
  1472. vertexNormals = face.vertexNormals;
  1473. faceNormal = face.normal;
  1474. if ( vertexNormals.length === 4 && needsSmoothNormals ) {
  1475. for ( i = 0; i < 4; i ++ ) {
  1476. vn = vertexNormals[ i ];
  1477. normalArray[ offset_normal ] = vn.x;
  1478. normalArray[ offset_normal + 1 ] = vn.y;
  1479. normalArray[ offset_normal + 2 ] = vn.z;
  1480. offset_normal += 3;
  1481. }
  1482. } else {
  1483. for ( i = 0; i < 4; i ++ ) {
  1484. normalArray[ offset_normal ] = faceNormal.x;
  1485. normalArray[ offset_normal + 1 ] = faceNormal.y;
  1486. normalArray[ offset_normal + 2 ] = faceNormal.z;
  1487. offset_normal += 3;
  1488. }
  1489. }
  1490. }
  1491. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  1492. _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint );
  1493. }
  1494. if ( dirtyUvs && obj_uvs && uvType ) {
  1495. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1496. fi = chunk_faces3[ f ];
  1497. uv = obj_uvs[ fi ];
  1498. if ( uv === undefined ) continue;
  1499. for ( i = 0; i < 3; i ++ ) {
  1500. uvi = uv[ i ];
  1501. uvArray[ offset_uv ] = uvi.x;
  1502. uvArray[ offset_uv + 1 ] = uvi.y;
  1503. offset_uv += 2;
  1504. }
  1505. }
  1506. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1507. fi = chunk_faces4[ f ];
  1508. uv = obj_uvs[ fi ];
  1509. if ( uv === undefined ) continue;
  1510. for ( i = 0; i < 4; i ++ ) {
  1511. uvi = uv[ i ];
  1512. uvArray[ offset_uv ] = uvi.x;
  1513. uvArray[ offset_uv + 1 ] = uvi.y;
  1514. offset_uv += 2;
  1515. }
  1516. }
  1517. if ( offset_uv > 0 ) {
  1518. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  1519. _gl.bufferData( _gl.ARRAY_BUFFER, uvArray, hint );
  1520. }
  1521. }
  1522. if ( dirtyUvs && obj_uvs2 && uvType ) {
  1523. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1524. fi = chunk_faces3[ f ];
  1525. uv2 = obj_uvs2[ fi ];
  1526. if ( uv2 === undefined ) continue;
  1527. for ( i = 0; i < 3; i ++ ) {
  1528. uv2i = uv2[ i ];
  1529. uv2Array[ offset_uv2 ] = uv2i.x;
  1530. uv2Array[ offset_uv2 + 1 ] = uv2i.y;
  1531. offset_uv2 += 2;
  1532. }
  1533. }
  1534. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1535. fi = chunk_faces4[ f ];
  1536. uv2 = obj_uvs2[ fi ];
  1537. if ( uv2 === undefined ) continue;
  1538. for ( i = 0; i < 4; i ++ ) {
  1539. uv2i = uv2[ i ];
  1540. uv2Array[ offset_uv2 ] = uv2i.x;
  1541. uv2Array[ offset_uv2 + 1 ] = uv2i.y;
  1542. offset_uv2 += 2;
  1543. }
  1544. }
  1545. if ( offset_uv2 > 0 ) {
  1546. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  1547. _gl.bufferData( _gl.ARRAY_BUFFER, uv2Array, hint );
  1548. }
  1549. }
  1550. if ( dirtyElements ) {
  1551. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1552. faceArray[ offset_face ] = vertexIndex;
  1553. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  1554. faceArray[ offset_face + 2 ] = vertexIndex + 2;
  1555. offset_face += 3;
  1556. lineArray[ offset_line ] = vertexIndex;
  1557. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  1558. lineArray[ offset_line + 2 ] = vertexIndex;
  1559. lineArray[ offset_line + 3 ] = vertexIndex + 2;
  1560. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  1561. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  1562. offset_line += 6;
  1563. vertexIndex += 3;
  1564. }
  1565. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1566. faceArray[ offset_face ] = vertexIndex;
  1567. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  1568. faceArray[ offset_face + 2 ] = vertexIndex + 3;
  1569. faceArray[ offset_face + 3 ] = vertexIndex + 1;
  1570. faceArray[ offset_face + 4 ] = vertexIndex + 2;
  1571. faceArray[ offset_face + 5 ] = vertexIndex + 3;
  1572. offset_face += 6;
  1573. lineArray[ offset_line ] = vertexIndex;
  1574. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  1575. lineArray[ offset_line + 2 ] = vertexIndex;
  1576. lineArray[ offset_line + 3 ] = vertexIndex + 3;
  1577. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  1578. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  1579. lineArray[ offset_line + 6 ] = vertexIndex + 2;
  1580. lineArray[ offset_line + 7 ] = vertexIndex + 3;
  1581. offset_line += 8;
  1582. vertexIndex += 4;
  1583. }
  1584. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  1585. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faceArray, hint );
  1586. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  1587. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint );
  1588. }
  1589. if ( customAttributes ) {
  1590. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  1591. customAttribute = customAttributes[ i ];
  1592. if ( ! customAttribute.__original.needsUpdate ) continue;
  1593. offset_custom = 0;
  1594. offset_customSrc = 0;
  1595. if ( customAttribute.size === 1 ) {
  1596. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  1597. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1598. face = obj_faces[ chunk_faces3[ f ] ];
  1599. customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ];
  1600. customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
  1601. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
  1602. offset_custom += 3;
  1603. }
  1604. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1605. face = obj_faces[ chunk_faces4[ f ] ];
  1606. customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ];
  1607. customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
  1608. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
  1609. customAttribute.array[ offset_custom + 3 ] = customAttribute.value[ face.d ];
  1610. offset_custom += 4;
  1611. }
  1612. } else if ( customAttribute.boundTo === "faces" ) {
  1613. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1614. value = customAttribute.value[ chunk_faces3[ f ] ];
  1615. customAttribute.array[ offset_custom ] = value;
  1616. customAttribute.array[ offset_custom + 1 ] = value;
  1617. customAttribute.array[ offset_custom + 2 ] = value;
  1618. offset_custom += 3;
  1619. }
  1620. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1621. value = customAttribute.value[ chunk_faces4[ f ] ];
  1622. customAttribute.array[ offset_custom ] = value;
  1623. customAttribute.array[ offset_custom + 1 ] = value;
  1624. customAttribute.array[ offset_custom + 2 ] = value;
  1625. customAttribute.array[ offset_custom + 3 ] = value;
  1626. offset_custom += 4;
  1627. }
  1628. }
  1629. } else if ( customAttribute.size === 2 ) {
  1630. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  1631. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1632. face = obj_faces[ chunk_faces3[ f ] ];
  1633. v1 = customAttribute.value[ face.a ];
  1634. v2 = customAttribute.value[ face.b ];
  1635. v3 = customAttribute.value[ face.c ];
  1636. customAttribute.array[ offset_custom ] = v1.x;
  1637. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1638. customAttribute.array[ offset_custom + 2 ] = v2.x;
  1639. customAttribute.array[ offset_custom + 3 ] = v2.y;
  1640. customAttribute.array[ offset_custom + 4 ] = v3.x;
  1641. customAttribute.array[ offset_custom + 5 ] = v3.y;
  1642. offset_custom += 6;
  1643. }
  1644. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1645. face = obj_faces[ chunk_faces4[ f ] ];
  1646. v1 = customAttribute.value[ face.a ];
  1647. v2 = customAttribute.value[ face.b ];
  1648. v3 = customAttribute.value[ face.c ];
  1649. v4 = customAttribute.value[ face.d ];
  1650. customAttribute.array[ offset_custom ] = v1.x;
  1651. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1652. customAttribute.array[ offset_custom + 2 ] = v2.x;
  1653. customAttribute.array[ offset_custom + 3 ] = v2.y;
  1654. customAttribute.array[ offset_custom + 4 ] = v3.x;
  1655. customAttribute.array[ offset_custom + 5 ] = v3.y;
  1656. customAttribute.array[ offset_custom + 6 ] = v4.x;
  1657. customAttribute.array[ offset_custom + 7 ] = v4.y;
  1658. offset_custom += 8;
  1659. }
  1660. } else if ( customAttribute.boundTo === "faces" ) {
  1661. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1662. value = customAttribute.value[ chunk_faces3[ f ] ];
  1663. v1 = value;
  1664. v2 = value;
  1665. v3 = value;
  1666. customAttribute.array[ offset_custom ] = v1.x;
  1667. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1668. customAttribute.array[ offset_custom + 2 ] = v2.x;
  1669. customAttribute.array[ offset_custom + 3 ] = v2.y;
  1670. customAttribute.array[ offset_custom + 4 ] = v3.x;
  1671. customAttribute.array[ offset_custom + 5 ] = v3.y;
  1672. offset_custom += 6;
  1673. }
  1674. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1675. value = customAttribute.value[ chunk_faces4[ f ] ];
  1676. v1 = value;
  1677. v2 = value;
  1678. v3 = value;
  1679. v4 = value;
  1680. customAttribute.array[ offset_custom ] = v1.x;
  1681. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1682. customAttribute.array[ offset_custom + 2 ] = v2.x;
  1683. customAttribute.array[ offset_custom + 3 ] = v2.y;
  1684. customAttribute.array[ offset_custom + 4 ] = v3.x;
  1685. customAttribute.array[ offset_custom + 5 ] = v3.y;
  1686. customAttribute.array[ offset_custom + 6 ] = v4.x;
  1687. customAttribute.array[ offset_custom + 7 ] = v4.y;
  1688. offset_custom += 8;
  1689. }
  1690. }
  1691. } else if ( customAttribute.size === 3 ) {
  1692. var pp;
  1693. if ( customAttribute.type === "c" ) {
  1694. pp = [ "r", "g", "b" ];
  1695. } else {
  1696. pp = [ "x", "y", "z" ];
  1697. }
  1698. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  1699. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1700. face = obj_faces[ chunk_faces3[ f ] ];
  1701. v1 = customAttribute.value[ face.a ];
  1702. v2 = customAttribute.value[ face.b ];
  1703. v3 = customAttribute.value[ face.c ];
  1704. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  1705. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  1706. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  1707. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  1708. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  1709. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  1710. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  1711. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  1712. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  1713. offset_custom += 9;
  1714. }
  1715. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1716. face = obj_faces[ chunk_faces4[ f ] ];
  1717. v1 = customAttribute.value[ face.a ];
  1718. v2 = customAttribute.value[ face.b ];
  1719. v3 = customAttribute.value[ face.c ];
  1720. v4 = customAttribute.value[ face.d ];
  1721. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  1722. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  1723. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  1724. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  1725. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  1726. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  1727. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  1728. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  1729. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  1730. customAttribute.array[ offset_custom + 9 ] = v4[ pp[ 0 ] ];
  1731. customAttribute.array[ offset_custom + 10 ] = v4[ pp[ 1 ] ];
  1732. customAttribute.array[ offset_custom + 11 ] = v4[ pp[ 2 ] ];
  1733. offset_custom += 12;
  1734. }
  1735. } else if ( customAttribute.boundTo === "faces" ) {
  1736. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1737. value = customAttribute.value[ chunk_faces3[ f ] ];
  1738. v1 = value;
  1739. v2 = value;
  1740. v3 = value;
  1741. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  1742. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  1743. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  1744. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  1745. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  1746. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  1747. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  1748. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  1749. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  1750. offset_custom += 9;
  1751. }
  1752. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1753. value = customAttribute.value[ chunk_faces4[ f ] ];
  1754. v1 = value;
  1755. v2 = value;
  1756. v3 = value;
  1757. v4 = value;
  1758. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  1759. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  1760. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  1761. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  1762. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  1763. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  1764. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  1765. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  1766. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  1767. customAttribute.array[ offset_custom + 9 ] = v4[ pp[ 0 ] ];
  1768. customAttribute.array[ offset_custom + 10 ] = v4[ pp[ 1 ] ];
  1769. customAttribute.array[ offset_custom + 11 ] = v4[ pp[ 2 ] ];
  1770. offset_custom += 12;
  1771. }
  1772. } else if ( customAttribute.boundTo === "faceVertices" ) {
  1773. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1774. value = customAttribute.value[ chunk_faces3[ f ] ];
  1775. v1 = value[ 0 ];
  1776. v2 = value[ 1 ];
  1777. v3 = value[ 2 ];
  1778. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  1779. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  1780. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  1781. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  1782. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  1783. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  1784. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  1785. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  1786. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  1787. offset_custom += 9;
  1788. }
  1789. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1790. value = customAttribute.value[ chunk_faces4[ f ] ];
  1791. v1 = value[ 0 ];
  1792. v2 = value[ 1 ];
  1793. v3 = value[ 2 ];
  1794. v4 = value[ 3 ];
  1795. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  1796. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  1797. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  1798. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  1799. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  1800. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  1801. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  1802. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  1803. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  1804. customAttribute.array[ offset_custom + 9 ] = v4[ pp[ 0 ] ];
  1805. customAttribute.array[ offset_custom + 10 ] = v4[ pp[ 1 ] ];
  1806. customAttribute.array[ offset_custom + 11 ] = v4[ pp[ 2 ] ];
  1807. offset_custom += 12;
  1808. }
  1809. }
  1810. } else if ( customAttribute.size === 4 ) {
  1811. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  1812. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1813. face = obj_faces[ chunk_faces3[ f ] ];
  1814. v1 = customAttribute.value[ face.a ];
  1815. v2 = customAttribute.value[ face.b ];
  1816. v3 = customAttribute.value[ face.c ];
  1817. customAttribute.array[ offset_custom ] = v1.x;
  1818. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1819. customAttribute.array[ offset_custom + 2 ] = v1.z;
  1820. customAttribute.array[ offset_custom + 3 ] = v1.w;
  1821. customAttribute.array[ offset_custom + 4 ] = v2.x;
  1822. customAttribute.array[ offset_custom + 5 ] = v2.y;
  1823. customAttribute.array[ offset_custom + 6 ] = v2.z;
  1824. customAttribute.array[ offset_custom + 7 ] = v2.w;
  1825. customAttribute.array[ offset_custom + 8 ] = v3.x;
  1826. customAttribute.array[ offset_custom + 9 ] = v3.y;
  1827. customAttribute.array[ offset_custom + 10 ] = v3.z;
  1828. customAttribute.array[ offset_custom + 11 ] = v3.w;
  1829. offset_custom += 12;
  1830. }
  1831. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1832. face = obj_faces[ chunk_faces4[ f ] ];
  1833. v1 = customAttribute.value[ face.a ];
  1834. v2 = customAttribute.value[ face.b ];
  1835. v3 = customAttribute.value[ face.c ];
  1836. v4 = customAttribute.value[ face.d ];
  1837. customAttribute.array[ offset_custom ] = v1.x;
  1838. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1839. customAttribute.array[ offset_custom + 2 ] = v1.z;
  1840. customAttribute.array[ offset_custom + 3 ] = v1.w;
  1841. customAttribute.array[ offset_custom + 4 ] = v2.x;
  1842. customAttribute.array[ offset_custom + 5 ] = v2.y;
  1843. customAttribute.array[ offset_custom + 6 ] = v2.z;
  1844. customAttribute.array[ offset_custom + 7 ] = v2.w;
  1845. customAttribute.array[ offset_custom + 8 ] = v3.x;
  1846. customAttribute.array[ offset_custom + 9 ] = v3.y;
  1847. customAttribute.array[ offset_custom + 10 ] = v3.z;
  1848. customAttribute.array[ offset_custom + 11 ] = v3.w;
  1849. customAttribute.array[ offset_custom + 12 ] = v4.x;
  1850. customAttribute.array[ offset_custom + 13 ] = v4.y;
  1851. customAttribute.array[ offset_custom + 14 ] = v4.z;
  1852. customAttribute.array[ offset_custom + 15 ] = v4.w;
  1853. offset_custom += 16;
  1854. }
  1855. } else if ( customAttribute.boundTo === "faces" ) {
  1856. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1857. value = customAttribute.value[ chunk_faces3[ f ] ];
  1858. v1 = value;
  1859. v2 = value;
  1860. v3 = value;
  1861. customAttribute.array[ offset_custom ] = v1.x;
  1862. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1863. customAttribute.array[ offset_custom + 2 ] = v1.z;
  1864. customAttribute.array[ offset_custom + 3 ] = v1.w;
  1865. customAttribute.array[ offset_custom + 4 ] = v2.x;
  1866. customAttribute.array[ offset_custom + 5 ] = v2.y;
  1867. customAttribute.array[ offset_custom + 6 ] = v2.z;
  1868. customAttribute.array[ offset_custom + 7 ] = v2.w;
  1869. customAttribute.array[ offset_custom + 8 ] = v3.x;
  1870. customAttribute.array[ offset_custom + 9 ] = v3.y;
  1871. customAttribute.array[ offset_custom + 10 ] = v3.z;
  1872. customAttribute.array[ offset_custom + 11 ] = v3.w;
  1873. offset_custom += 12;
  1874. }
  1875. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1876. value = customAttribute.value[ chunk_faces4[ f ] ];
  1877. v1 = value;
  1878. v2 = value;
  1879. v3 = value;
  1880. v4 = value;
  1881. customAttribute.array[ offset_custom ] = v1.x;
  1882. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1883. customAttribute.array[ offset_custom + 2 ] = v1.z;
  1884. customAttribute.array[ offset_custom + 3 ] = v1.w;
  1885. customAttribute.array[ offset_custom + 4 ] = v2.x;
  1886. customAttribute.array[ offset_custom + 5 ] = v2.y;
  1887. customAttribute.array[ offset_custom + 6 ] = v2.z;
  1888. customAttribute.array[ offset_custom + 7 ] = v2.w;
  1889. customAttribute.array[ offset_custom + 8 ] = v3.x;
  1890. customAttribute.array[ offset_custom + 9 ] = v3.y;
  1891. customAttribute.array[ offset_custom + 10 ] = v3.z;
  1892. customAttribute.array[ offset_custom + 11 ] = v3.w;
  1893. customAttribute.array[ offset_custom + 12 ] = v4.x;
  1894. customAttribute.array[ offset_custom + 13 ] = v4.y;
  1895. customAttribute.array[ offset_custom + 14 ] = v4.z;
  1896. customAttribute.array[ offset_custom + 15 ] = v4.w;
  1897. offset_custom += 16;
  1898. }
  1899. } else if ( customAttribute.boundTo === "faceVertices" ) {
  1900. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1901. value = customAttribute.value[ chunk_faces3[ f ] ];
  1902. v1 = value[ 0 ];
  1903. v2 = value[ 1 ];
  1904. v3 = value[ 2 ];
  1905. customAttribute.array[ offset_custom ] = v1.x;
  1906. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1907. customAttribute.array[ offset_custom + 2 ] = v1.z;
  1908. customAttribute.array[ offset_custom + 3 ] = v1.w;
  1909. customAttribute.array[ offset_custom + 4 ] = v2.x;
  1910. customAttribute.array[ offset_custom + 5 ] = v2.y;
  1911. customAttribute.array[ offset_custom + 6 ] = v2.z;
  1912. customAttribute.array[ offset_custom + 7 ] = v2.w;
  1913. customAttribute.array[ offset_custom + 8 ] = v3.x;
  1914. customAttribute.array[ offset_custom + 9 ] = v3.y;
  1915. customAttribute.array[ offset_custom + 10 ] = v3.z;
  1916. customAttribute.array[ offset_custom + 11 ] = v3.w;
  1917. offset_custom += 12;
  1918. }
  1919. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  1920. value = customAttribute.value[ chunk_faces4[ f ] ];
  1921. v1 = value[ 0 ];
  1922. v2 = value[ 1 ];
  1923. v3 = value[ 2 ];
  1924. v4 = value[ 3 ];
  1925. customAttribute.array[ offset_custom ] = v1.x;
  1926. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1927. customAttribute.array[ offset_custom + 2 ] = v1.z;
  1928. customAttribute.array[ offset_custom + 3 ] = v1.w;
  1929. customAttribute.array[ offset_custom + 4 ] = v2.x;
  1930. customAttribute.array[ offset_custom + 5 ] = v2.y;
  1931. customAttribute.array[ offset_custom + 6 ] = v2.z;
  1932. customAttribute.array[ offset_custom + 7 ] = v2.w;
  1933. customAttribute.array[ offset_custom + 8 ] = v3.x;
  1934. customAttribute.array[ offset_custom + 9 ] = v3.y;
  1935. customAttribute.array[ offset_custom + 10 ] = v3.z;
  1936. customAttribute.array[ offset_custom + 11 ] = v3.w;
  1937. customAttribute.array[ offset_custom + 12 ] = v4.x;
  1938. customAttribute.array[ offset_custom + 13 ] = v4.y;
  1939. customAttribute.array[ offset_custom + 14 ] = v4.z;
  1940. customAttribute.array[ offset_custom + 15 ] = v4.w;
  1941. offset_custom += 16;
  1942. }
  1943. }
  1944. }
  1945. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  1946. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  1947. }
  1948. }
  1949. if ( dispose ) {
  1950. delete geometryGroup.__inittedArrays;
  1951. delete geometryGroup.__colorArray;
  1952. delete geometryGroup.__normalArray;
  1953. delete geometryGroup.__tangentArray;
  1954. delete geometryGroup.__uvArray;
  1955. delete geometryGroup.__uv2Array;
  1956. delete geometryGroup.__faceArray;
  1957. delete geometryGroup.__vertexArray;
  1958. delete geometryGroup.__lineArray;
  1959. delete geometryGroup.__skinIndexArray;
  1960. delete geometryGroup.__skinWeightArray;
  1961. }
  1962. };
  1963. function setDirectBuffers ( geometry, hint, dispose ) {
  1964. var attributes = geometry.attributes;
  1965. var index = attributes[ "index" ];
  1966. var position = attributes[ "position" ];
  1967. var normal = attributes[ "normal" ];
  1968. var uv = attributes[ "uv" ];
  1969. var color = attributes[ "color" ];
  1970. var tangent = attributes[ "tangent" ];
  1971. if ( geometry.elementsNeedUpdate && index !== undefined ) {
  1972. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  1973. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, index.array, hint );
  1974. }
  1975. if ( geometry.verticesNeedUpdate && position !== undefined ) {
  1976. _gl.bindBuffer( _gl.ARRAY_BUFFER, position.buffer );
  1977. _gl.bufferData( _gl.ARRAY_BUFFER, position.array, hint );
  1978. }
  1979. if ( geometry.normalsNeedUpdate && normal !== undefined ) {
  1980. _gl.bindBuffer( _gl.ARRAY_BUFFER, normal.buffer );
  1981. _gl.bufferData( _gl.ARRAY_BUFFER, normal.array, hint );
  1982. }
  1983. if ( geometry.uvsNeedUpdate && uv !== undefined ) {
  1984. _gl.bindBuffer( _gl.ARRAY_BUFFER, uv.buffer );
  1985. _gl.bufferData( _gl.ARRAY_BUFFER, uv.array, hint );
  1986. }
  1987. if ( geometry.colorsNeedUpdate && color !== undefined ) {
  1988. _gl.bindBuffer( _gl.ARRAY_BUFFER, color.buffer );
  1989. _gl.bufferData( _gl.ARRAY_BUFFER, color.array, hint );
  1990. }
  1991. if ( geometry.tangentsNeedUpdate && tangent !== undefined ) {
  1992. _gl.bindBuffer( _gl.ARRAY_BUFFER, tangent.buffer );
  1993. _gl.bufferData( _gl.ARRAY_BUFFER, tangent.array, hint );
  1994. }
  1995. if ( dispose ) {
  1996. for ( var i in geometry.attributes ) {
  1997. delete geometry.attributes[ i ].array;
  1998. }
  1999. }
  2000. };
  2001. // Buffer rendering
  2002. this.renderBufferImmediate = function ( object, program, material ) {
  2003. if ( object.hasPositions && ! object.__webglVertexBuffer ) object.__webglVertexBuffer = _gl.createBuffer();
  2004. if ( object.hasNormals && ! object.__webglNormalBuffer ) object.__webglNormalBuffer = _gl.createBuffer();
  2005. if ( object.hasUvs && ! object.__webglUvBuffer ) object.__webglUvBuffer = _gl.createBuffer();
  2006. if ( object.hasColors && ! object.__webglColorBuffer ) object.__webglColorBuffer = _gl.createBuffer();
  2007. if ( object.hasPositions ) {
  2008. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglVertexBuffer );
  2009. _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
  2010. _gl.enableVertexAttribArray( program.attributes.position );
  2011. _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  2012. }
  2013. if ( object.hasNormals ) {
  2014. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglNormalBuffer );
  2015. if ( material.shading === THREE.FlatShading ) {
  2016. var nx, ny, nz,
  2017. nax, nbx, ncx, nay, nby, ncy, naz, nbz, ncz,
  2018. normalArray,
  2019. i, il = object.count * 3;
  2020. for( i = 0; i < il; i += 9 ) {
  2021. normalArray = object.normalArray;
  2022. nax = normalArray[ i ];
  2023. nay = normalArray[ i + 1 ];
  2024. naz = normalArray[ i + 2 ];
  2025. nbx = normalArray[ i + 3 ];
  2026. nby = normalArray[ i + 4 ];
  2027. nbz = normalArray[ i + 5 ];
  2028. ncx = normalArray[ i + 6 ];
  2029. ncy = normalArray[ i + 7 ];
  2030. ncz = normalArray[ i + 8 ];
  2031. nx = ( nax + nbx + ncx ) / 3;
  2032. ny = ( nay + nby + ncy ) / 3;
  2033. nz = ( naz + nbz + ncz ) / 3;
  2034. normalArray[ i ] = nx;
  2035. normalArray[ i + 1 ] = ny;
  2036. normalArray[ i + 2 ] = nz;
  2037. normalArray[ i + 3 ] = nx;
  2038. normalArray[ i + 4 ] = ny;
  2039. normalArray[ i + 5 ] = nz;
  2040. normalArray[ i + 6 ] = nx;
  2041. normalArray[ i + 7 ] = ny;
  2042. normalArray[ i + 8 ] = nz;
  2043. }
  2044. }
  2045. _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
  2046. _gl.enableVertexAttribArray( program.attributes.normal );
  2047. _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  2048. }
  2049. if ( object.hasUvs && material.map ) {
  2050. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglUvBuffer );
  2051. _gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
  2052. _gl.enableVertexAttribArray( program.attributes.uv );
  2053. _gl.vertexAttribPointer( program.attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  2054. }
  2055. if ( object.hasColors && material.vertexColors !== THREE.NoColors ) {
  2056. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglColorBuffer );
  2057. _gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
  2058. _gl.enableVertexAttribArray( program.attributes.color );
  2059. _gl.vertexAttribPointer( program.attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  2060. }
  2061. _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
  2062. object.count = 0;
  2063. };
  2064. this.renderBufferDirect = function ( camera, lights, fog, material, geometry, object ) {
  2065. if ( material.visible === false ) return;
  2066. var program, attributes, linewidth, primitives, a, attribute;
  2067. program = setProgram( camera, lights, fog, material, object );
  2068. attributes = program.attributes;
  2069. var updateBuffers = false,
  2070. wireframeBit = material.wireframe ? 1 : 0,
  2071. geometryHash = ( geometry.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  2072. if ( geometryHash !== _currentGeometryGroupHash ) {
  2073. _currentGeometryGroupHash = geometryHash;
  2074. updateBuffers = true;
  2075. }
  2076. if ( updateBuffers ) {
  2077. disableAttributes();
  2078. }
  2079. // render mesh
  2080. if ( object instanceof THREE.Mesh ) {
  2081. var index = geometry.attributes[ "index" ];
  2082. // indexed triangles
  2083. if ( index ) {
  2084. var offsets = geometry.offsets;
  2085. // if there is more than 1 chunk
  2086. // must set attribute pointers to use new offsets for each chunk
  2087. // even if geometry and materials didn't change
  2088. if ( offsets.length > 1 ) updateBuffers = true;
  2089. for ( var i = 0, il = offsets.length; i < il; i ++ ) {
  2090. var startIndex = offsets[ i ].index;
  2091. if ( updateBuffers ) {
  2092. // vertices
  2093. var position = geometry.attributes[ "position" ];
  2094. var positionSize = position.itemSize;
  2095. _gl.bindBuffer( _gl.ARRAY_BUFFER, position.buffer );
  2096. enableAttribute( attributes.position );
  2097. _gl.vertexAttribPointer( attributes.position, positionSize, _gl.FLOAT, false, 0, startIndex * positionSize * 4 ); // 4 bytes per Float32
  2098. // normals
  2099. var normal = geometry.attributes[ "normal" ];
  2100. if ( attributes.normal >= 0 && normal ) {
  2101. var normalSize = normal.itemSize;
  2102. _gl.bindBuffer( _gl.ARRAY_BUFFER, normal.buffer );
  2103. enableAttribute( attributes.normal );
  2104. _gl.vertexAttribPointer( attributes.normal, normalSize, _gl.FLOAT, false, 0, startIndex * normalSize * 4 );
  2105. }
  2106. // uvs
  2107. var uv = geometry.attributes[ "uv" ];
  2108. if ( attributes.uv >= 0 && uv ) {
  2109. var uvSize = uv.itemSize;
  2110. _gl.bindBuffer( _gl.ARRAY_BUFFER, uv.buffer );
  2111. enableAttribute( attributes.uv );
  2112. _gl.vertexAttribPointer( attributes.uv, uvSize, _gl.FLOAT, false, 0, startIndex * uvSize * 4 );
  2113. }
  2114. // colors
  2115. var color = geometry.attributes[ "color" ];
  2116. if ( attributes.color >= 0 && color ) {
  2117. var colorSize = color.itemSize;
  2118. _gl.bindBuffer( _gl.ARRAY_BUFFER, color.buffer );
  2119. enableAttribute( attributes.color );
  2120. _gl.vertexAttribPointer( attributes.color, colorSize, _gl.FLOAT, false, 0, startIndex * colorSize * 4 );
  2121. }
  2122. // tangents
  2123. var tangent = geometry.attributes[ "tangent" ];
  2124. if ( attributes.tangent >= 0 && tangent ) {
  2125. var tangentSize = tangent.itemSize;
  2126. _gl.bindBuffer( _gl.ARRAY_BUFFER, tangent.buffer );
  2127. enableAttribute( attributes.tangent );
  2128. _gl.vertexAttribPointer( attributes.tangent, tangentSize, _gl.FLOAT, false, 0, startIndex * tangentSize * 4 );
  2129. }
  2130. // indices
  2131. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  2132. }
  2133. // render indexed triangles
  2134. _gl.drawElements( _gl.TRIANGLES, offsets[ i ].count, _gl.UNSIGNED_SHORT, offsets[ i ].start * 2 ); // 2 bytes per Uint16
  2135. _this.info.render.calls ++;
  2136. _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
  2137. _this.info.render.faces += offsets[ i ].count / 3;
  2138. }
  2139. // non-indexed triangles
  2140. } else {
  2141. if ( updateBuffers ) {
  2142. // vertices
  2143. var position = geometry.attributes[ "position" ];
  2144. var positionSize = position.itemSize;
  2145. _gl.bindBuffer( _gl.ARRAY_BUFFER, position.buffer );
  2146. enableAttribute( attributes.position );
  2147. _gl.vertexAttribPointer( attributes.position, positionSize, _gl.FLOAT, false, 0, 0 );
  2148. // normals
  2149. var normal = geometry.attributes[ "normal" ];
  2150. if ( attributes.normal >= 0 && normal ) {
  2151. var normalSize = normal.itemSize;
  2152. _gl.bindBuffer( _gl.ARRAY_BUFFER, normal.buffer );
  2153. enableAttribute( attributes.normal );
  2154. _gl.vertexAttribPointer( attributes.normal, normalSize, _gl.FLOAT, false, 0, 0 );
  2155. }
  2156. // uvs
  2157. var uv = geometry.attributes[ "uv" ];
  2158. if ( attributes.uv >= 0 && uv ) {
  2159. var uvSize = uv.itemSize;
  2160. _gl.bindBuffer( _gl.ARRAY_BUFFER, uv.buffer );
  2161. enableAttribute( attributes.uv );
  2162. _gl.vertexAttribPointer( attributes.uv, uvSize, _gl.FLOAT, false, 0, 0 );
  2163. }
  2164. // colors
  2165. var color = geometry.attributes[ "color" ];
  2166. if ( attributes.color >= 0 && color ) {
  2167. var colorSize = color.itemSize;
  2168. _gl.bindBuffer( _gl.ARRAY_BUFFER, color.buffer );
  2169. enableAttribute( attributes.color );
  2170. _gl.vertexAttribPointer( attributes.color, colorSize, _gl.FLOAT, false, 0, 0 );
  2171. }
  2172. // tangents
  2173. var tangent = geometry.attributes[ "tangent" ];
  2174. if ( attributes.tangent >= 0 && tangent ) {
  2175. var tangentSize = tangent.itemSize;
  2176. _gl.bindBuffer( _gl.ARRAY_BUFFER, tangent.buffer );
  2177. enableAttribute( attributes.tangent );
  2178. _gl.vertexAttribPointer( attributes.tangent, tangentSize, _gl.FLOAT, false, 0, 0 );
  2179. }
  2180. }
  2181. // render non-indexed triangles
  2182. _gl.drawArrays( _gl.TRIANGLES, 0, position.numItems / 3 );
  2183. _this.info.render.calls ++;
  2184. _this.info.render.vertices += position.numItems / 3;
  2185. _this.info.render.faces += position.numItems / 3 / 3;
  2186. }
  2187. // render particles
  2188. } else if ( object instanceof THREE.ParticleSystem ) {
  2189. if ( updateBuffers ) {
  2190. // vertices
  2191. var position = geometry.attributes[ "position" ];
  2192. var positionSize = position.itemSize;
  2193. _gl.bindBuffer( _gl.ARRAY_BUFFER, position.buffer );
  2194. enableAttribute( attributes.position );
  2195. _gl.vertexAttribPointer( attributes.position, positionSize, _gl.FLOAT, false, 0, 0 );
  2196. // colors
  2197. var color = geometry.attributes[ "color" ];
  2198. if ( attributes.color >= 0 && color ) {
  2199. var colorSize = color.itemSize;
  2200. _gl.bindBuffer( _gl.ARRAY_BUFFER, color.buffer );
  2201. enableAttribute( attributes.color );
  2202. _gl.vertexAttribPointer( attributes.color, colorSize, _gl.FLOAT, false, 0, 0 );
  2203. }
  2204. // render particles
  2205. _gl.drawArrays( _gl.POINTS, 0, position.numItems / 3 );
  2206. _this.info.render.calls ++;
  2207. _this.info.render.points += position.numItems / 3;
  2208. }
  2209. } else if ( object instanceof THREE.Line ) {
  2210. if ( updateBuffers ) {
  2211. // vertices
  2212. var position = geometry.attributes[ "position" ];
  2213. var positionSize = position.itemSize;
  2214. _gl.bindBuffer( _gl.ARRAY_BUFFER, position.buffer );
  2215. enableAttribute( attributes.position );
  2216. _gl.vertexAttribPointer( attributes.position, positionSize, _gl.FLOAT, false, 0, 0 );
  2217. // colors
  2218. var color = geometry.attributes[ "color" ];
  2219. if ( attributes.color >= 0 && color ) {
  2220. var colorSize = color.itemSize;
  2221. _gl.bindBuffer( _gl.ARRAY_BUFFER, color.buffer );
  2222. enableAttribute( attributes.color );
  2223. _gl.vertexAttribPointer( attributes.color, colorSize, _gl.FLOAT, false, 0, 0 );
  2224. }
  2225. // render lines
  2226. setLineWidth( material.linewidth );
  2227. _gl.drawArrays( _gl.LINE_STRIP, 0, position.numItems / 3 );
  2228. _this.info.render.calls ++;
  2229. _this.info.render.points += position.numItems;
  2230. }
  2231. }
  2232. };
  2233. this.renderBuffer = function ( camera, lights, fog, material, geometryGroup, object ) {
  2234. if ( material.visible === false ) return;
  2235. var program, attributes, linewidth, primitives, a, attribute, i, il;
  2236. program = setProgram( camera, lights, fog, material, object );
  2237. attributes = program.attributes;
  2238. var updateBuffers = false,
  2239. wireframeBit = material.wireframe ? 1 : 0,
  2240. geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  2241. if ( geometryGroupHash !== _currentGeometryGroupHash ) {
  2242. _currentGeometryGroupHash = geometryGroupHash;
  2243. updateBuffers = true;
  2244. }
  2245. if ( updateBuffers ) {
  2246. disableAttributes();
  2247. }
  2248. // vertices
  2249. if ( !material.morphTargets && attributes.position >= 0 ) {
  2250. if ( updateBuffers ) {
  2251. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  2252. enableAttribute( attributes.position );
  2253. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  2254. }
  2255. } else {
  2256. if ( object.morphTargetBase ) {
  2257. setupMorphTargets( material, geometryGroup, object );
  2258. }
  2259. }
  2260. if ( updateBuffers ) {
  2261. // custom attributes
  2262. // Use the per-geometryGroup custom attribute arrays which are setup in initMeshBuffers
  2263. if ( geometryGroup.__webglCustomAttributesList ) {
  2264. for ( i = 0, il = geometryGroup.__webglCustomAttributesList.length; i < il; i ++ ) {
  2265. attribute = geometryGroup.__webglCustomAttributesList[ i ];
  2266. if ( attributes[ attribute.buffer.belongsToAttribute ] >= 0 ) {
  2267. _gl.bindBuffer( _gl.ARRAY_BUFFER, attribute.buffer );
  2268. enableAttribute( attributes[ attribute.buffer.belongsToAttribute ] );
  2269. _gl.vertexAttribPointer( attributes[ attribute.buffer.belongsToAttribute ], attribute.size, _gl.FLOAT, false, 0, 0 );
  2270. }
  2271. }
  2272. }
  2273. // colors
  2274. if ( attributes.color >= 0 ) {
  2275. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  2276. enableAttribute( attributes.color );
  2277. _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  2278. }
  2279. // normals
  2280. if ( attributes.normal >= 0 ) {
  2281. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  2282. enableAttribute( attributes.normal );
  2283. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  2284. }
  2285. // tangents
  2286. if ( attributes.tangent >= 0 ) {
  2287. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  2288. enableAttribute( attributes.tangent );
  2289. _gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 );
  2290. }
  2291. // uvs
  2292. if ( attributes.uv >= 0 ) {
  2293. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  2294. enableAttribute( attributes.uv );
  2295. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  2296. }
  2297. if ( attributes.uv2 >= 0 ) {
  2298. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  2299. enableAttribute( attributes.uv2 );
  2300. _gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
  2301. }
  2302. if ( material.skinning &&
  2303. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  2304. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  2305. enableAttribute( attributes.skinIndex );
  2306. _gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 );
  2307. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  2308. enableAttribute( attributes.skinWeight );
  2309. _gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 );
  2310. }
  2311. // line distances
  2312. if ( attributes.lineDistance >= 0 ) {
  2313. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglLineDistanceBuffer );
  2314. enableAttribute( attributes.lineDistance );
  2315. _gl.vertexAttribPointer( attributes.lineDistance, 1, _gl.FLOAT, false, 0, 0 );
  2316. }
  2317. }
  2318. // render mesh
  2319. if ( object instanceof THREE.Mesh ) {
  2320. // wireframe
  2321. if ( material.wireframe ) {
  2322. setLineWidth( material.wireframeLinewidth );
  2323. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  2324. _gl.drawElements( _gl.LINES, geometryGroup.__webglLineCount, _gl.UNSIGNED_SHORT, 0 );
  2325. // triangles
  2326. } else {
  2327. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  2328. _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webglFaceCount, _gl.UNSIGNED_SHORT, 0 );
  2329. }
  2330. _this.info.render.calls ++;
  2331. _this.info.render.vertices += geometryGroup.__webglFaceCount;
  2332. _this.info.render.faces += geometryGroup.__webglFaceCount / 3;
  2333. // render lines
  2334. } else if ( object instanceof THREE.Line ) {
  2335. primitives = ( object.type === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  2336. setLineWidth( material.linewidth );
  2337. _gl.drawArrays( primitives, 0, geometryGroup.__webglLineCount );
  2338. _this.info.render.calls ++;
  2339. // render particles
  2340. } else if ( object instanceof THREE.ParticleSystem ) {
  2341. _gl.drawArrays( _gl.POINTS, 0, geometryGroup.__webglParticleCount );
  2342. _this.info.render.calls ++;
  2343. _this.info.render.points += geometryGroup.__webglParticleCount;
  2344. // render ribbon
  2345. } else if ( object instanceof THREE.Ribbon ) {
  2346. _gl.drawArrays( _gl.TRIANGLE_STRIP, 0, geometryGroup.__webglVertexCount );
  2347. _this.info.render.calls ++;
  2348. }
  2349. };
  2350. function enableAttribute( attribute ) {
  2351. if ( ! _enabledAttributes[ attribute ] ) {
  2352. _gl.enableVertexAttribArray( attribute );
  2353. _enabledAttributes[ attribute ] = true;
  2354. }
  2355. };
  2356. function disableAttributes() {
  2357. for ( var attribute in _enabledAttributes ) {
  2358. if ( _enabledAttributes[ attribute ] ) {
  2359. _gl.disableVertexAttribArray( attribute );
  2360. _enabledAttributes[ attribute ] = false;
  2361. }
  2362. }
  2363. };
  2364. function setupMorphTargets ( material, geometryGroup, object ) {
  2365. // set base
  2366. var attributes = material.program.attributes;
  2367. if ( object.morphTargetBase !== -1 && attributes.position >= 0 ) {
  2368. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ] );
  2369. enableAttribute( attributes.position );
  2370. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  2371. } else if ( attributes.position >= 0 ) {
  2372. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  2373. enableAttribute( attributes.position );
  2374. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  2375. }
  2376. if ( object.morphTargetForcedOrder.length ) {
  2377. // set forced order
  2378. var m = 0;
  2379. var order = object.morphTargetForcedOrder;
  2380. var influences = object.morphTargetInfluences;
  2381. while ( m < material.numSupportedMorphTargets && m < order.length ) {
  2382. if ( attributes[ "morphTarget" + m ] >= 0 ) {
  2383. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ] );
  2384. enableAttribute( attributes[ "morphTarget" + m ] );
  2385. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  2386. }
  2387. if ( attributes[ "morphNormal" + m ] >= 0 && material.morphNormals ) {
  2388. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ order[ m ] ] );
  2389. enableAttribute( attributes[ "morphNormal" + m ] );
  2390. _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
  2391. }
  2392. object.__webglMorphTargetInfluences[ m ] = influences[ order[ m ] ];
  2393. m ++;
  2394. }
  2395. } else {
  2396. // find the most influencing
  2397. var influence, activeInfluenceIndices = [];
  2398. var influences = object.morphTargetInfluences;
  2399. var i, il = influences.length;
  2400. for ( i = 0; i < il; i ++ ) {
  2401. influence = influences[ i ];
  2402. if ( influence > 0 ) {
  2403. activeInfluenceIndices.push( [ influence, i ] );
  2404. }
  2405. }
  2406. if ( activeInfluenceIndices.length > material.numSupportedMorphTargets ) {
  2407. activeInfluenceIndices.sort( numericalSort );
  2408. activeInfluenceIndices.length = material.numSupportedMorphTargets;
  2409. } else if ( activeInfluenceIndices.length > material.numSupportedMorphNormals ) {
  2410. activeInfluenceIndices.sort( numericalSort );
  2411. } else if ( activeInfluenceIndices.length === 0 ) {
  2412. activeInfluenceIndices.push( [ 0, 0 ] );
  2413. };
  2414. var influenceIndex, m = 0;
  2415. while ( m < material.numSupportedMorphTargets ) {
  2416. if ( activeInfluenceIndices[ m ] ) {
  2417. influenceIndex = activeInfluenceIndices[ m ][ 1 ];
  2418. if ( attributes[ "morphTarget" + m ] >= 0 ) {
  2419. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ influenceIndex ] );
  2420. enableAttribute( attributes[ "morphTarget" + m ] );
  2421. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  2422. }
  2423. if ( attributes[ "morphNormal" + m ] >= 0 && material.morphNormals ) {
  2424. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ influenceIndex ] );
  2425. enableAttribute( attributes[ "morphNormal" + m ] );
  2426. _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
  2427. }
  2428. object.__webglMorphTargetInfluences[ m ] = influences[ influenceIndex ];
  2429. } else {
  2430. /*
  2431. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  2432. if ( material.morphNormals ) {
  2433. _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
  2434. }
  2435. */
  2436. object.__webglMorphTargetInfluences[ m ] = 0;
  2437. }
  2438. m ++;
  2439. }
  2440. }
  2441. // load updated influences uniform
  2442. if ( material.program.uniforms.morphTargetInfluences !== null ) {
  2443. _gl.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webglMorphTargetInfluences );
  2444. }
  2445. };
  2446. // Sorting
  2447. function painterSortStable ( a, b ) {
  2448. if ( a.z !== b.z ) {
  2449. return b.z - a.z;
  2450. } else {
  2451. return b.id - a.id;
  2452. }
  2453. };
  2454. function numericalSort ( a, b ) {
  2455. return b[ 0 ] - a[ 0 ];
  2456. };
  2457. // Rendering
  2458. this.render = function ( scene, camera, renderTarget, forceClear ) {
  2459. if ( camera instanceof THREE.Camera === false ) {
  2460. console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
  2461. return;
  2462. }
  2463. var i, il,
  2464. webglObject, object,
  2465. renderList,
  2466. lights = scene.__lights,
  2467. fog = scene.fog;
  2468. // reset caching for this frame
  2469. _currentMaterialId = -1;
  2470. _lightsNeedUpdate = true;
  2471. // update scene graph
  2472. if ( this.autoUpdateScene ) scene.updateMatrixWorld();
  2473. // update camera matrices and frustum
  2474. if ( camera.parent === undefined ) camera.updateMatrixWorld();
  2475. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  2476. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  2477. _frustum.setFromMatrix( _projScreenMatrix );
  2478. // update WebGL objects
  2479. if ( this.autoUpdateObjects ) this.initWebGLObjects( scene );
  2480. // custom render plugins (pre pass)
  2481. renderPlugins( this.renderPluginsPre, scene, camera );
  2482. //
  2483. _this.info.render.calls = 0;
  2484. _this.info.render.vertices = 0;
  2485. _this.info.render.faces = 0;
  2486. _this.info.render.points = 0;
  2487. this.setRenderTarget( renderTarget );
  2488. if ( this.autoClear || forceClear ) {
  2489. this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );
  2490. }
  2491. // set matrices for regular objects (frustum culled)
  2492. renderList = scene.__webglObjects;
  2493. for ( i = 0, il = renderList.length; i < il; i ++ ) {
  2494. webglObject = renderList[ i ];
  2495. object = webglObject.object;
  2496. webglObject.render = false;
  2497. if ( object.visible ) {
  2498. if ( ! ( object instanceof THREE.Mesh || object instanceof THREE.ParticleSystem ) || ! ( object.frustumCulled ) || _frustum.intersectsObject( object ) ) {
  2499. setupMatrices( object, camera );
  2500. unrollBufferMaterial( webglObject );
  2501. webglObject.render = true;
  2502. if ( this.sortObjects === true ) {
  2503. if ( object.renderDepth !== null ) {
  2504. webglObject.z = object.renderDepth;
  2505. } else {
  2506. _vector3.getPositionFromMatrix( object.matrixWorld );
  2507. _vector3.applyProjection( _projScreenMatrix );
  2508. webglObject.z = _vector3.z;
  2509. }
  2510. webglObject.id = object.id;
  2511. }
  2512. }
  2513. }
  2514. }
  2515. if ( this.sortObjects ) {
  2516. renderList.sort( painterSortStable );
  2517. }
  2518. // set matrices for immediate objects
  2519. renderList = scene.__webglObjectsImmediate;
  2520. for ( i = 0, il = renderList.length; i < il; i ++ ) {
  2521. webglObject = renderList[ i ];
  2522. object = webglObject.object;
  2523. if ( object.visible ) {
  2524. setupMatrices( object, camera );
  2525. unrollImmediateBufferMaterial( webglObject );
  2526. }
  2527. }
  2528. if ( scene.overrideMaterial ) {
  2529. var material = scene.overrideMaterial;
  2530. this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  2531. this.setDepthTest( material.depthTest );
  2532. this.setDepthWrite( material.depthWrite );
  2533. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  2534. renderObjects( scene.__webglObjects, false, "", camera, lights, fog, true, material );
  2535. renderObjectsImmediate( scene.__webglObjectsImmediate, "", camera, lights, fog, false, material );
  2536. } else {
  2537. var material = null;
  2538. // opaque pass (front-to-back order)
  2539. this.setBlending( THREE.NoBlending );
  2540. renderObjects( scene.__webglObjects, true, "opaque", camera, lights, fog, false, material );
  2541. renderObjectsImmediate( scene.__webglObjectsImmediate, "opaque", camera, lights, fog, false, material );
  2542. // transparent pass (back-to-front order)
  2543. renderObjects( scene.__webglObjects, false, "transparent", camera, lights, fog, true, material );
  2544. renderObjectsImmediate( scene.__webglObjectsImmediate, "transparent", camera, lights, fog, true, material );
  2545. }
  2546. // custom render plugins (post pass)
  2547. renderPlugins( this.renderPluginsPost, scene, camera );
  2548. // Generate mipmap if we're using any kind of mipmap filtering
  2549. if ( renderTarget && renderTarget.generateMipmaps && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
  2550. updateRenderTargetMipmap( renderTarget );
  2551. }
  2552. // Ensure depth buffer writing is enabled so it can be cleared on next render
  2553. this.setDepthTest( true );
  2554. this.setDepthWrite( true );
  2555. // _gl.finish();
  2556. };
  2557. function renderPlugins( plugins, scene, camera ) {
  2558. if ( ! plugins.length ) return;
  2559. for ( var i = 0, il = plugins.length; i < il; i ++ ) {
  2560. // reset state for plugin (to start from clean slate)
  2561. _currentProgram = null;
  2562. _currentCamera = null;
  2563. _oldBlending = -1;
  2564. _oldDepthTest = -1;
  2565. _oldDepthWrite = -1;
  2566. _oldDoubleSided = -1;
  2567. _oldFlipSided = -1;
  2568. _currentGeometryGroupHash = -1;
  2569. _currentMaterialId = -1;
  2570. _lightsNeedUpdate = true;
  2571. plugins[ i ].render( scene, camera, _currentWidth, _currentHeight );
  2572. // reset state after plugin (anything could have changed)
  2573. _currentProgram = null;
  2574. _currentCamera = null;
  2575. _oldBlending = -1;
  2576. _oldDepthTest = -1;
  2577. _oldDepthWrite = -1;
  2578. _oldDoubleSided = -1;
  2579. _oldFlipSided = -1;
  2580. _currentGeometryGroupHash = -1;
  2581. _currentMaterialId = -1;
  2582. _lightsNeedUpdate = true;
  2583. }
  2584. };
  2585. function renderObjects ( renderList, reverse, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
  2586. var webglObject, object, buffer, material, start, end, delta;
  2587. if ( reverse ) {
  2588. start = renderList.length - 1;
  2589. end = -1;
  2590. delta = -1;
  2591. } else {
  2592. start = 0;
  2593. end = renderList.length;
  2594. delta = 1;
  2595. }
  2596. for ( var i = start; i !== end; i += delta ) {
  2597. webglObject = renderList[ i ];
  2598. if ( webglObject.render ) {
  2599. object = webglObject.object;
  2600. buffer = webglObject.buffer;
  2601. if ( overrideMaterial ) {
  2602. material = overrideMaterial;
  2603. } else {
  2604. material = webglObject[ materialType ];
  2605. if ( ! material ) continue;
  2606. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  2607. _this.setDepthTest( material.depthTest );
  2608. _this.setDepthWrite( material.depthWrite );
  2609. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  2610. }
  2611. _this.setMaterialFaces( material );
  2612. if ( buffer instanceof THREE.BufferGeometry ) {
  2613. _this.renderBufferDirect( camera, lights, fog, material, buffer, object );
  2614. } else {
  2615. _this.renderBuffer( camera, lights, fog, material, buffer, object );
  2616. }
  2617. }
  2618. }
  2619. };
  2620. function renderObjectsImmediate ( renderList, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
  2621. var webglObject, object, material, program;
  2622. for ( var i = 0, il = renderList.length; i < il; i ++ ) {
  2623. webglObject = renderList[ i ];
  2624. object = webglObject.object;
  2625. if ( object.visible ) {
  2626. if ( overrideMaterial ) {
  2627. material = overrideMaterial;
  2628. } else {
  2629. material = webglObject[ materialType ];
  2630. if ( ! material ) continue;
  2631. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  2632. _this.setDepthTest( material.depthTest );
  2633. _this.setDepthWrite( material.depthWrite );
  2634. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  2635. }
  2636. _this.renderImmediateObject( camera, lights, fog, material, object );
  2637. }
  2638. }
  2639. };
  2640. this.renderImmediateObject = function ( camera, lights, fog, material, object ) {
  2641. var program = setProgram( camera, lights, fog, material, object );
  2642. _currentGeometryGroupHash = -1;
  2643. _this.setMaterialFaces( material );
  2644. if ( object.immediateRenderCallback ) {
  2645. object.immediateRenderCallback( program, _gl, _frustum );
  2646. } else {
  2647. object.render( function( object ) { _this.renderBufferImmediate( object, program, material ); } );
  2648. }
  2649. };
  2650. function unrollImmediateBufferMaterial ( globject ) {
  2651. var object = globject.object,
  2652. material = object.material;
  2653. if ( material.transparent ) {
  2654. globject.transparent = material;
  2655. globject.opaque = null;
  2656. } else {
  2657. globject.opaque = material;
  2658. globject.transparent = null;
  2659. }
  2660. };
  2661. function unrollBufferMaterial ( globject ) {
  2662. var object = globject.object,
  2663. buffer = globject.buffer,
  2664. material, materialIndex, meshMaterial;
  2665. meshMaterial = object.material;
  2666. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  2667. materialIndex = buffer.materialIndex;
  2668. material = meshMaterial.materials[ materialIndex ];
  2669. if ( material.transparent ) {
  2670. globject.transparent = material;
  2671. globject.opaque = null;
  2672. } else {
  2673. globject.opaque = material;
  2674. globject.transparent = null;
  2675. }
  2676. } else {
  2677. material = meshMaterial;
  2678. if ( material ) {
  2679. if ( material.transparent ) {
  2680. globject.transparent = material;
  2681. globject.opaque = null;
  2682. } else {
  2683. globject.opaque = material;
  2684. globject.transparent = null;
  2685. }
  2686. }
  2687. }
  2688. };
  2689. // Geometry splitting
  2690. function sortFacesByMaterial ( geometry, material ) {
  2691. var f, fl, face, materialIndex, vertices,
  2692. groupHash, hash_map = {};
  2693. var numMorphTargets = geometry.morphTargets.length;
  2694. var numMorphNormals = geometry.morphNormals.length;
  2695. var usesFaceMaterial = material instanceof THREE.MeshFaceMaterial;
  2696. geometry.geometryGroups = {};
  2697. for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  2698. face = geometry.faces[ f ];
  2699. materialIndex = usesFaceMaterial ? face.materialIndex : 0;
  2700. if ( hash_map[ materialIndex ] === undefined ) {
  2701. hash_map[ materialIndex ] = { 'hash': materialIndex, 'counter': 0 };
  2702. }
  2703. groupHash = hash_map[ materialIndex ].hash + '_' + hash_map[ materialIndex ].counter;
  2704. if ( geometry.geometryGroups[ groupHash ] === undefined ) {
  2705. geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'faces4': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
  2706. }
  2707. vertices = face instanceof THREE.Face3 ? 3 : 4;
  2708. if ( geometry.geometryGroups[ groupHash ].vertices + vertices > 65535 ) {
  2709. hash_map[ materialIndex ].counter += 1;
  2710. groupHash = hash_map[ materialIndex ].hash + '_' + hash_map[ materialIndex ].counter;
  2711. if ( geometry.geometryGroups[ groupHash ] === undefined ) {
  2712. geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'faces4': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
  2713. }
  2714. }
  2715. if ( face instanceof THREE.Face3 ) {
  2716. geometry.geometryGroups[ groupHash ].faces3.push( f );
  2717. } else {
  2718. geometry.geometryGroups[ groupHash ].faces4.push( f );
  2719. }
  2720. geometry.geometryGroups[ groupHash ].vertices += vertices;
  2721. }
  2722. geometry.geometryGroupsList = [];
  2723. for ( var g in geometry.geometryGroups ) {
  2724. geometry.geometryGroups[ g ].id = _geometryGroupCounter ++;
  2725. geometry.geometryGroupsList.push( geometry.geometryGroups[ g ] );
  2726. }
  2727. };
  2728. // Objects refresh
  2729. this.initWebGLObjects = function ( scene ) {
  2730. if ( !scene.__webglObjects ) {
  2731. scene.__webglObjects = [];
  2732. scene.__webglObjectsImmediate = [];
  2733. scene.__webglSprites = [];
  2734. scene.__webglFlares = [];
  2735. }
  2736. while ( scene.__objectsAdded.length ) {
  2737. addObject( scene.__objectsAdded[ 0 ], scene );
  2738. scene.__objectsAdded.splice( 0, 1 );
  2739. }
  2740. while ( scene.__objectsRemoved.length ) {
  2741. removeObject( scene.__objectsRemoved[ 0 ], scene );
  2742. scene.__objectsRemoved.splice( 0, 1 );
  2743. }
  2744. // update must be called after objects adding / removal
  2745. for ( var o = 0, ol = scene.__webglObjects.length; o < ol; o ++ ) {
  2746. updateObject( scene.__webglObjects[ o ].object );
  2747. }
  2748. };
  2749. // Objects adding
  2750. function addObject ( object, scene ) {
  2751. var g, geometry, material, geometryGroup;
  2752. if ( ! object.__webglInit ) {
  2753. object.__webglInit = true;
  2754. object._modelViewMatrix = new THREE.Matrix4();
  2755. object._normalMatrix = new THREE.Matrix3();
  2756. if ( object.geometry !== undefined && object.geometry.__webglInit === undefined ) {
  2757. object.geometry.__webglInit = true;
  2758. object.geometry.addEventListener( 'dispose', onGeometryDispose );
  2759. }
  2760. if ( object instanceof THREE.Mesh ) {
  2761. geometry = object.geometry;
  2762. material = object.material;
  2763. if ( geometry instanceof THREE.Geometry ) {
  2764. if ( geometry.geometryGroups === undefined ) {
  2765. sortFacesByMaterial( geometry, material );
  2766. }
  2767. // create separate VBOs per geometry chunk
  2768. for ( g in geometry.geometryGroups ) {
  2769. geometryGroup = geometry.geometryGroups[ g ];
  2770. // initialise VBO on the first access
  2771. if ( ! geometryGroup.__webglVertexBuffer ) {
  2772. createMeshBuffers( geometryGroup );
  2773. initMeshBuffers( geometryGroup, object );
  2774. geometry.verticesNeedUpdate = true;
  2775. geometry.morphTargetsNeedUpdate = true;
  2776. geometry.elementsNeedUpdate = true;
  2777. geometry.uvsNeedUpdate = true;
  2778. geometry.normalsNeedUpdate = true;
  2779. geometry.tangentsNeedUpdate = true;
  2780. geometry.colorsNeedUpdate = true;
  2781. }
  2782. }
  2783. } else if ( geometry instanceof THREE.BufferGeometry ) {
  2784. initDirectBuffers( geometry );
  2785. }
  2786. } else if ( object instanceof THREE.Ribbon ) {
  2787. geometry = object.geometry;
  2788. if ( ! geometry.__webglVertexBuffer ) {
  2789. createRibbonBuffers( geometry );
  2790. initRibbonBuffers( geometry, object );
  2791. geometry.verticesNeedUpdate = true;
  2792. geometry.colorsNeedUpdate = true;
  2793. geometry.normalsNeedUpdate = true;
  2794. }
  2795. } else if ( object instanceof THREE.Line ) {
  2796. geometry = object.geometry;
  2797. if ( ! geometry.__webglVertexBuffer ) {
  2798. if ( geometry instanceof THREE.Geometry ) {
  2799. createLineBuffers( geometry );
  2800. initLineBuffers( geometry, object );
  2801. geometry.verticesNeedUpdate = true;
  2802. geometry.colorsNeedUpdate = true;
  2803. geometry.lineDistancesNeedUpdate = true;
  2804. } else if ( geometry instanceof THREE.BufferGeometry ) {
  2805. initDirectBuffers( geometry );
  2806. }
  2807. }
  2808. } else if ( object instanceof THREE.ParticleSystem ) {
  2809. geometry = object.geometry;
  2810. if ( ! geometry.__webglVertexBuffer ) {
  2811. if ( geometry instanceof THREE.Geometry ) {
  2812. createParticleBuffers( geometry );
  2813. initParticleBuffers( geometry, object );
  2814. geometry.verticesNeedUpdate = true;
  2815. geometry.colorsNeedUpdate = true;
  2816. } else if ( geometry instanceof THREE.BufferGeometry ) {
  2817. initDirectBuffers( geometry );
  2818. }
  2819. }
  2820. }
  2821. }
  2822. if ( ! object.__webglActive ) {
  2823. if ( object instanceof THREE.Mesh ) {
  2824. geometry = object.geometry;
  2825. if ( geometry instanceof THREE.BufferGeometry ) {
  2826. addBuffer( scene.__webglObjects, geometry, object );
  2827. } else if ( geometry instanceof THREE.Geometry ) {
  2828. for ( g in geometry.geometryGroups ) {
  2829. geometryGroup = geometry.geometryGroups[ g ];
  2830. addBuffer( scene.__webglObjects, geometryGroup, object );
  2831. }
  2832. }
  2833. } else if ( object instanceof THREE.Ribbon ||
  2834. object instanceof THREE.Line ||
  2835. object instanceof THREE.ParticleSystem ) {
  2836. geometry = object.geometry;
  2837. addBuffer( scene.__webglObjects, geometry, object );
  2838. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  2839. addBufferImmediate( scene.__webglObjectsImmediate, object );
  2840. } else if ( object instanceof THREE.Sprite ) {
  2841. scene.__webglSprites.push( object );
  2842. } else if ( object instanceof THREE.LensFlare ) {
  2843. scene.__webglFlares.push( object );
  2844. }
  2845. object.__webglActive = true;
  2846. }
  2847. };
  2848. function addBuffer ( objlist, buffer, object ) {
  2849. objlist.push(
  2850. {
  2851. buffer: buffer,
  2852. object: object,
  2853. opaque: null,
  2854. transparent: null
  2855. }
  2856. );
  2857. };
  2858. function addBufferImmediate ( objlist, object ) {
  2859. objlist.push(
  2860. {
  2861. object: object,
  2862. opaque: null,
  2863. transparent: null
  2864. }
  2865. );
  2866. };
  2867. // Objects updates
  2868. function updateObject ( object ) {
  2869. var geometry = object.geometry,
  2870. geometryGroup, customAttributesDirty, material;
  2871. if ( object instanceof THREE.Mesh ) {
  2872. if ( geometry instanceof THREE.BufferGeometry ) {
  2873. if ( geometry.verticesNeedUpdate || geometry.elementsNeedUpdate ||
  2874. geometry.uvsNeedUpdate || geometry.normalsNeedUpdate ||
  2875. geometry.colorsNeedUpdate || geometry.tangentsNeedUpdate ) {
  2876. setDirectBuffers( geometry, _gl.DYNAMIC_DRAW, !geometry.dynamic );
  2877. }
  2878. geometry.verticesNeedUpdate = false;
  2879. geometry.elementsNeedUpdate = false;
  2880. geometry.uvsNeedUpdate = false;
  2881. geometry.normalsNeedUpdate = false;
  2882. geometry.colorsNeedUpdate = false;
  2883. geometry.tangentsNeedUpdate = false;
  2884. } else {
  2885. // check all geometry groups
  2886. for( var i = 0, il = geometry.geometryGroupsList.length; i < il; i ++ ) {
  2887. geometryGroup = geometry.geometryGroupsList[ i ];
  2888. material = getBufferMaterial( object, geometryGroup );
  2889. if ( geometry.buffersNeedUpdate ) {
  2890. initMeshBuffers( geometryGroup, object );
  2891. }
  2892. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  2893. if ( geometry.verticesNeedUpdate || geometry.morphTargetsNeedUpdate || geometry.elementsNeedUpdate ||
  2894. geometry.uvsNeedUpdate || geometry.normalsNeedUpdate ||
  2895. geometry.colorsNeedUpdate || geometry.tangentsNeedUpdate || customAttributesDirty ) {
  2896. setMeshBuffers( geometryGroup, object, _gl.DYNAMIC_DRAW, !geometry.dynamic, material );
  2897. }
  2898. }
  2899. geometry.verticesNeedUpdate = false;
  2900. geometry.morphTargetsNeedUpdate = false;
  2901. geometry.elementsNeedUpdate = false;
  2902. geometry.uvsNeedUpdate = false;
  2903. geometry.normalsNeedUpdate = false;
  2904. geometry.colorsNeedUpdate = false;
  2905. geometry.tangentsNeedUpdate = false;
  2906. geometry.buffersNeedUpdate = false;
  2907. material.attributes && clearCustomAttributes( material );
  2908. }
  2909. } else if ( object instanceof THREE.Ribbon ) {
  2910. material = getBufferMaterial( object, geometry );
  2911. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  2912. if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || geometry.normalsNeedUpdate || customAttributesDirty ) {
  2913. setRibbonBuffers( geometry, _gl.DYNAMIC_DRAW );
  2914. }
  2915. geometry.verticesNeedUpdate = false;
  2916. geometry.colorsNeedUpdate = false;
  2917. geometry.normalsNeedUpdate = false;
  2918. material.attributes && clearCustomAttributes( material );
  2919. } else if ( object instanceof THREE.Line ) {
  2920. if ( geometry instanceof THREE.BufferGeometry ) {
  2921. if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate ) {
  2922. setDirectBuffers( geometry, _gl.DYNAMIC_DRAW, !geometry.dynamic );
  2923. }
  2924. geometry.verticesNeedUpdate = false;
  2925. geometry.colorsNeedUpdate = false;
  2926. } else {
  2927. material = getBufferMaterial( object, geometry );
  2928. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  2929. if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || geometry.lineDistancesNeedUpdate || customAttributesDirty ) {
  2930. setLineBuffers( geometry, _gl.DYNAMIC_DRAW );
  2931. }
  2932. geometry.verticesNeedUpdate = false;
  2933. geometry.colorsNeedUpdate = false;
  2934. geometry.lineDistancesNeedUpdate = false;
  2935. material.attributes && clearCustomAttributes( material );
  2936. }
  2937. } else if ( object instanceof THREE.ParticleSystem ) {
  2938. if ( geometry instanceof THREE.BufferGeometry ) {
  2939. if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate ) {
  2940. setDirectBuffers( geometry, _gl.DYNAMIC_DRAW, !geometry.dynamic );
  2941. }
  2942. geometry.verticesNeedUpdate = false;
  2943. geometry.colorsNeedUpdate = false;
  2944. } else {
  2945. material = getBufferMaterial( object, geometry );
  2946. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  2947. if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || object.sortParticles || customAttributesDirty ) {
  2948. setParticleBuffers( geometry, _gl.DYNAMIC_DRAW, object );
  2949. }
  2950. geometry.verticesNeedUpdate = false;
  2951. geometry.colorsNeedUpdate = false;
  2952. material.attributes && clearCustomAttributes( material );
  2953. }
  2954. }
  2955. };
  2956. // Objects updates - custom attributes check
  2957. function areCustomAttributesDirty ( material ) {
  2958. for ( var a in material.attributes ) {
  2959. if ( material.attributes[ a ].needsUpdate ) return true;
  2960. }
  2961. return false;
  2962. };
  2963. function clearCustomAttributes ( material ) {
  2964. for ( var a in material.attributes ) {
  2965. material.attributes[ a ].needsUpdate = false;
  2966. }
  2967. };
  2968. // Objects removal
  2969. function removeObject ( object, scene ) {
  2970. if ( object instanceof THREE.Mesh ||
  2971. object instanceof THREE.ParticleSystem ||
  2972. object instanceof THREE.Ribbon ||
  2973. object instanceof THREE.Line ) {
  2974. removeInstances( scene.__webglObjects, object );
  2975. } else if ( object instanceof THREE.Sprite ) {
  2976. removeInstancesDirect( scene.__webglSprites, object );
  2977. } else if ( object instanceof THREE.LensFlare ) {
  2978. removeInstancesDirect( scene.__webglFlares, object );
  2979. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  2980. removeInstances( scene.__webglObjectsImmediate, object );
  2981. }
  2982. object.__webglActive = false;
  2983. };
  2984. function removeInstances ( objlist, object ) {
  2985. for ( var o = objlist.length - 1; o >= 0; o -- ) {
  2986. if ( objlist[ o ].object === object ) {
  2987. objlist.splice( o, 1 );
  2988. }
  2989. }
  2990. };
  2991. function removeInstancesDirect ( objlist, object ) {
  2992. for ( var o = objlist.length - 1; o >= 0; o -- ) {
  2993. if ( objlist[ o ] === object ) {
  2994. objlist.splice( o, 1 );
  2995. }
  2996. }
  2997. };
  2998. // Materials
  2999. this.initMaterial = function ( material, lights, fog, object ) {
  3000. material.addEventListener( 'dispose', onMaterialDispose );
  3001. var u, a, identifiers, i, parameters, maxLightCount, maxBones, maxShadows, shaderID;
  3002. if ( material instanceof THREE.MeshDepthMaterial ) {
  3003. shaderID = 'depth';
  3004. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  3005. shaderID = 'normal';
  3006. } else if ( material instanceof THREE.MeshBasicMaterial ) {
  3007. shaderID = 'basic';
  3008. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  3009. shaderID = 'lambert';
  3010. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  3011. shaderID = 'phong';
  3012. } else if ( material instanceof THREE.LineBasicMaterial ) {
  3013. shaderID = 'basic';
  3014. } else if ( material instanceof THREE.LineDashedMaterial ) {
  3015. shaderID = 'dashed';
  3016. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  3017. shaderID = 'particle_basic';
  3018. }
  3019. if ( shaderID ) {
  3020. setMaterialShaders( material, THREE.ShaderLib[ shaderID ] );
  3021. }
  3022. // heuristics to create shader parameters according to lights in the scene
  3023. // (not to blow over maxLights budget)
  3024. maxLightCount = allocateLights( lights );
  3025. maxShadows = allocateShadows( lights );
  3026. maxBones = allocateBones( object );
  3027. parameters = {
  3028. map: !!material.map,
  3029. envMap: !!material.envMap,
  3030. lightMap: !!material.lightMap,
  3031. bumpMap: !!material.bumpMap,
  3032. normalMap: !!material.normalMap,
  3033. specularMap: !!material.specularMap,
  3034. vertexColors: material.vertexColors,
  3035. fog: fog,
  3036. useFog: material.fog,
  3037. fogExp: fog instanceof THREE.FogExp2,
  3038. sizeAttenuation: material.sizeAttenuation,
  3039. skinning: material.skinning,
  3040. maxBones: maxBones,
  3041. useVertexTexture: _supportsBoneTextures && object && object.useVertexTexture,
  3042. boneTextureWidth: object && object.boneTextureWidth,
  3043. boneTextureHeight: object && object.boneTextureHeight,
  3044. morphTargets: material.morphTargets,
  3045. morphNormals: material.morphNormals,
  3046. maxMorphTargets: this.maxMorphTargets,
  3047. maxMorphNormals: this.maxMorphNormals,
  3048. maxDirLights: maxLightCount.directional,
  3049. maxPointLights: maxLightCount.point,
  3050. maxSpotLights: maxLightCount.spot,
  3051. maxHemiLights: maxLightCount.hemi,
  3052. maxShadows: maxShadows,
  3053. shadowMapEnabled: this.shadowMapEnabled && object.receiveShadow,
  3054. shadowMapType: this.shadowMapType,
  3055. shadowMapDebug: this.shadowMapDebug,
  3056. shadowMapCascade: this.shadowMapCascade,
  3057. alphaTest: material.alphaTest,
  3058. metal: material.metal,
  3059. perPixel: material.perPixel,
  3060. wrapAround: material.wrapAround,
  3061. doubleSided: material.side === THREE.DoubleSide,
  3062. flipSided: material.side === THREE.BackSide
  3063. };
  3064. material.program = buildProgram( shaderID, material.fragmentShader, material.vertexShader, material.uniforms, material.attributes, material.defines, parameters );
  3065. var attributes = material.program.attributes;
  3066. if ( material.morphTargets ) {
  3067. material.numSupportedMorphTargets = 0;
  3068. var id, base = "morphTarget";
  3069. for ( i = 0; i < this.maxMorphTargets; i ++ ) {
  3070. id = base + i;
  3071. if ( attributes[ id ] >= 0 ) {
  3072. material.numSupportedMorphTargets ++;
  3073. }
  3074. }
  3075. }
  3076. if ( material.morphNormals ) {
  3077. material.numSupportedMorphNormals = 0;
  3078. var id, base = "morphNormal";
  3079. for ( i = 0; i < this.maxMorphNormals; i ++ ) {
  3080. id = base + i;
  3081. if ( attributes[ id ] >= 0 ) {
  3082. material.numSupportedMorphNormals ++;
  3083. }
  3084. }
  3085. }
  3086. material.uniformsList = [];
  3087. for ( u in material.uniforms ) {
  3088. material.uniformsList.push( [ material.uniforms[ u ], u ] );
  3089. }
  3090. };
  3091. function setMaterialShaders( material, shaders ) {
  3092. material.uniforms = THREE.UniformsUtils.clone( shaders.uniforms );
  3093. material.vertexShader = shaders.vertexShader;
  3094. material.fragmentShader = shaders.fragmentShader;
  3095. };
  3096. function setProgram( camera, lights, fog, material, object ) {
  3097. _usedTextureUnits = 0;
  3098. if ( material.needsUpdate ) {
  3099. if ( material.program ) deallocateMaterial( material );
  3100. _this.initMaterial( material, lights, fog, object );
  3101. material.needsUpdate = false;
  3102. }
  3103. if ( material.morphTargets ) {
  3104. if ( ! object.__webglMorphTargetInfluences ) {
  3105. object.__webglMorphTargetInfluences = new Float32Array( _this.maxMorphTargets );
  3106. }
  3107. }
  3108. var refreshMaterial = false;
  3109. var program = material.program,
  3110. p_uniforms = program.uniforms,
  3111. m_uniforms = material.uniforms;
  3112. if ( program !== _currentProgram ) {
  3113. _gl.useProgram( program );
  3114. _currentProgram = program;
  3115. refreshMaterial = true;
  3116. }
  3117. if ( material.id !== _currentMaterialId ) {
  3118. _currentMaterialId = material.id;
  3119. refreshMaterial = true;
  3120. }
  3121. if ( refreshMaterial || camera !== _currentCamera ) {
  3122. _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, camera.projectionMatrix.elements );
  3123. if ( camera !== _currentCamera ) _currentCamera = camera;
  3124. }
  3125. // skinning uniforms must be set even if material didn't change
  3126. // auto-setting of texture unit for bone texture must go before other textures
  3127. // not sure why, but otherwise weird things happen
  3128. if ( material.skinning ) {
  3129. if ( _supportsBoneTextures && object.useVertexTexture ) {
  3130. if ( p_uniforms.boneTexture !== null ) {
  3131. var textureUnit = getTextureUnit();
  3132. _gl.uniform1i( p_uniforms.boneTexture, textureUnit );
  3133. _this.setTexture( object.boneTexture, textureUnit );
  3134. }
  3135. } else {
  3136. if ( p_uniforms.boneGlobalMatrices !== null ) {
  3137. _gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.boneMatrices );
  3138. }
  3139. }
  3140. }
  3141. if ( refreshMaterial ) {
  3142. // refresh uniforms common to several materials
  3143. if ( fog && material.fog ) {
  3144. refreshUniformsFog( m_uniforms, fog );
  3145. }
  3146. if ( material instanceof THREE.MeshPhongMaterial ||
  3147. material instanceof THREE.MeshLambertMaterial ||
  3148. material.lights ) {
  3149. if ( _lightsNeedUpdate ) {
  3150. setupLights( program, lights );
  3151. _lightsNeedUpdate = false;
  3152. }
  3153. refreshUniformsLights( m_uniforms, _lights );
  3154. }
  3155. if ( material instanceof THREE.MeshBasicMaterial ||
  3156. material instanceof THREE.MeshLambertMaterial ||
  3157. material instanceof THREE.MeshPhongMaterial ) {
  3158. refreshUniformsCommon( m_uniforms, material );
  3159. }
  3160. // refresh single material specific uniforms
  3161. if ( material instanceof THREE.LineBasicMaterial ) {
  3162. refreshUniformsLine( m_uniforms, material );
  3163. } else if ( material instanceof THREE.LineDashedMaterial ) {
  3164. refreshUniformsLine( m_uniforms, material );
  3165. refreshUniformsDash( m_uniforms, material );
  3166. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  3167. refreshUniformsParticle( m_uniforms, material );
  3168. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  3169. refreshUniformsPhong( m_uniforms, material );
  3170. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  3171. refreshUniformsLambert( m_uniforms, material );
  3172. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  3173. m_uniforms.mNear.value = camera.near;
  3174. m_uniforms.mFar.value = camera.far;
  3175. m_uniforms.opacity.value = material.opacity;
  3176. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  3177. m_uniforms.opacity.value = material.opacity;
  3178. }
  3179. if ( object.receiveShadow && ! material._shadowPass ) {
  3180. refreshUniformsShadow( m_uniforms, lights );
  3181. }
  3182. // load common uniforms
  3183. loadUniformsGeneric( program, material.uniformsList );
  3184. // load material specific uniforms
  3185. // (shader material also gets them for the sake of genericity)
  3186. if ( material instanceof THREE.ShaderMaterial ||
  3187. material instanceof THREE.MeshPhongMaterial ||
  3188. material.envMap ) {
  3189. if ( p_uniforms.cameraPosition !== null ) {
  3190. _vector3.getPositionFromMatrix( camera.matrixWorld );
  3191. _gl.uniform3f( p_uniforms.cameraPosition, _vector3.x, _vector3.y, _vector3.z );
  3192. }
  3193. }
  3194. if ( material instanceof THREE.MeshPhongMaterial ||
  3195. material instanceof THREE.MeshLambertMaterial ||
  3196. material instanceof THREE.ShaderMaterial ||
  3197. material.skinning ) {
  3198. if ( p_uniforms.viewMatrix !== null ) {
  3199. _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, camera.matrixWorldInverse.elements );
  3200. }
  3201. }
  3202. }
  3203. loadUniformsMatrices( p_uniforms, object );
  3204. if ( p_uniforms.modelMatrix !== null ) {
  3205. _gl.uniformMatrix4fv( p_uniforms.modelMatrix, false, object.matrixWorld.elements );
  3206. }
  3207. return program;
  3208. };
  3209. // Uniforms (refresh uniforms objects)
  3210. function refreshUniformsCommon ( uniforms, material ) {
  3211. uniforms.opacity.value = material.opacity;
  3212. if ( _this.gammaInput ) {
  3213. uniforms.diffuse.value.copyGammaToLinear( material.color );
  3214. } else {
  3215. uniforms.diffuse.value = material.color;
  3216. }
  3217. uniforms.map.value = material.map;
  3218. uniforms.lightMap.value = material.lightMap;
  3219. uniforms.specularMap.value = material.specularMap;
  3220. if ( material.bumpMap ) {
  3221. uniforms.bumpMap.value = material.bumpMap;
  3222. uniforms.bumpScale.value = material.bumpScale;
  3223. }
  3224. if ( material.normalMap ) {
  3225. uniforms.normalMap.value = material.normalMap;
  3226. uniforms.normalScale.value.copy( material.normalScale );
  3227. }
  3228. // uv repeat and offset setting priorities
  3229. // 1. color map
  3230. // 2. specular map
  3231. // 3. normal map
  3232. // 4. bump map
  3233. var uvScaleMap;
  3234. if ( material.map ) {
  3235. uvScaleMap = material.map;
  3236. } else if ( material.specularMap ) {
  3237. uvScaleMap = material.specularMap;
  3238. } else if ( material.normalMap ) {
  3239. uvScaleMap = material.normalMap;
  3240. } else if ( material.bumpMap ) {
  3241. uvScaleMap = material.bumpMap;
  3242. }
  3243. if ( uvScaleMap !== undefined ) {
  3244. var offset = uvScaleMap.offset;
  3245. var repeat = uvScaleMap.repeat;
  3246. uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
  3247. }
  3248. uniforms.envMap.value = material.envMap;
  3249. uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : -1;
  3250. if ( _this.gammaInput ) {
  3251. //uniforms.reflectivity.value = material.reflectivity * material.reflectivity;
  3252. uniforms.reflectivity.value = material.reflectivity;
  3253. } else {
  3254. uniforms.reflectivity.value = material.reflectivity;
  3255. }
  3256. uniforms.refractionRatio.value = material.refractionRatio;
  3257. uniforms.combine.value = material.combine;
  3258. uniforms.useRefract.value = material.envMap && material.envMap.mapping instanceof THREE.CubeRefractionMapping;
  3259. };
  3260. function refreshUniformsLine ( uniforms, material ) {
  3261. uniforms.diffuse.value = material.color;
  3262. uniforms.opacity.value = material.opacity;
  3263. };
  3264. function refreshUniformsDash ( uniforms, material ) {
  3265. uniforms.dashSize.value = material.dashSize;
  3266. uniforms.totalSize.value = material.dashSize + material.gapSize;
  3267. uniforms.scale.value = material.scale;
  3268. };
  3269. function refreshUniformsParticle ( uniforms, material ) {
  3270. uniforms.psColor.value = material.color;
  3271. uniforms.opacity.value = material.opacity;
  3272. uniforms.size.value = material.size;
  3273. uniforms.scale.value = _canvas.height / 2.0; // TODO: Cache this.
  3274. uniforms.map.value = material.map;
  3275. };
  3276. function refreshUniformsFog ( uniforms, fog ) {
  3277. uniforms.fogColor.value = fog.color;
  3278. if ( fog instanceof THREE.Fog ) {
  3279. uniforms.fogNear.value = fog.near;
  3280. uniforms.fogFar.value = fog.far;
  3281. } else if ( fog instanceof THREE.FogExp2 ) {
  3282. uniforms.fogDensity.value = fog.density;
  3283. }
  3284. };
  3285. function refreshUniformsPhong ( uniforms, material ) {
  3286. uniforms.shininess.value = material.shininess;
  3287. if ( _this.gammaInput ) {
  3288. uniforms.ambient.value.copyGammaToLinear( material.ambient );
  3289. uniforms.emissive.value.copyGammaToLinear( material.emissive );
  3290. uniforms.specular.value.copyGammaToLinear( material.specular );
  3291. } else {
  3292. uniforms.ambient.value = material.ambient;
  3293. uniforms.emissive.value = material.emissive;
  3294. uniforms.specular.value = material.specular;
  3295. }
  3296. if ( material.wrapAround ) {
  3297. uniforms.wrapRGB.value.copy( material.wrapRGB );
  3298. }
  3299. };
  3300. function refreshUniformsLambert ( uniforms, material ) {
  3301. if ( _this.gammaInput ) {
  3302. uniforms.ambient.value.copyGammaToLinear( material.ambient );
  3303. uniforms.emissive.value.copyGammaToLinear( material.emissive );
  3304. } else {
  3305. uniforms.ambient.value = material.ambient;
  3306. uniforms.emissive.value = material.emissive;
  3307. }
  3308. if ( material.wrapAround ) {
  3309. uniforms.wrapRGB.value.copy( material.wrapRGB );
  3310. }
  3311. };
  3312. function refreshUniformsLights ( uniforms, lights ) {
  3313. uniforms.ambientLightColor.value = lights.ambient;
  3314. uniforms.directionalLightColor.value = lights.directional.colors;
  3315. uniforms.directionalLightDirection.value = lights.directional.positions;
  3316. uniforms.pointLightColor.value = lights.point.colors;
  3317. uniforms.pointLightPosition.value = lights.point.positions;
  3318. uniforms.pointLightDistance.value = lights.point.distances;
  3319. uniforms.spotLightColor.value = lights.spot.colors;
  3320. uniforms.spotLightPosition.value = lights.spot.positions;
  3321. uniforms.spotLightDistance.value = lights.spot.distances;
  3322. uniforms.spotLightDirection.value = lights.spot.directions;
  3323. uniforms.spotLightAngleCos.value = lights.spot.anglesCos;
  3324. uniforms.spotLightExponent.value = lights.spot.exponents;
  3325. uniforms.hemisphereLightSkyColor.value = lights.hemi.skyColors;
  3326. uniforms.hemisphereLightGroundColor.value = lights.hemi.groundColors;
  3327. uniforms.hemisphereLightDirection.value = lights.hemi.positions;
  3328. };
  3329. function refreshUniformsShadow ( uniforms, lights ) {
  3330. if ( uniforms.shadowMatrix ) {
  3331. var j = 0;
  3332. for ( var i = 0, il = lights.length; i < il; i ++ ) {
  3333. var light = lights[ i ];
  3334. if ( ! light.castShadow ) continue;
  3335. if ( light instanceof THREE.SpotLight || ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) ) {
  3336. uniforms.shadowMap.value[ j ] = light.shadowMap;
  3337. uniforms.shadowMapSize.value[ j ] = light.shadowMapSize;
  3338. uniforms.shadowMatrix.value[ j ] = light.shadowMatrix;
  3339. uniforms.shadowDarkness.value[ j ] = light.shadowDarkness;
  3340. uniforms.shadowBias.value[ j ] = light.shadowBias;
  3341. j ++;
  3342. }
  3343. }
  3344. }
  3345. };
  3346. // Uniforms (load to GPU)
  3347. function loadUniformsMatrices ( uniforms, object ) {
  3348. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrix.elements );
  3349. if ( uniforms.normalMatrix ) {
  3350. _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrix.elements );
  3351. }
  3352. };
  3353. function getTextureUnit() {
  3354. var textureUnit = _usedTextureUnits;
  3355. if ( textureUnit >= _maxTextures ) {
  3356. console.warn( "WebGLRenderer: trying to use " + textureUnit + " texture units while this GPU supports only " + _maxTextures );
  3357. }
  3358. _usedTextureUnits += 1;
  3359. return textureUnit;
  3360. };
  3361. function loadUniformsGeneric ( program, uniforms ) {
  3362. var uniform, value, type, location, texture, textureUnit, i, il, j, jl, offset;
  3363. for ( j = 0, jl = uniforms.length; j < jl; j ++ ) {
  3364. location = program.uniforms[ uniforms[ j ][ 1 ] ];
  3365. if ( !location ) continue;
  3366. uniform = uniforms[ j ][ 0 ];
  3367. type = uniform.type;
  3368. value = uniform.value;
  3369. if ( type === "i" ) { // single integer
  3370. _gl.uniform1i( location, value );
  3371. } else if ( type === "f" ) { // single float
  3372. _gl.uniform1f( location, value );
  3373. } else if ( type === "v2" ) { // single THREE.Vector2
  3374. _gl.uniform2f( location, value.x, value.y );
  3375. } else if ( type === "v3" ) { // single THREE.Vector3
  3376. _gl.uniform3f( location, value.x, value.y, value.z );
  3377. } else if ( type === "v4" ) { // single THREE.Vector4
  3378. _gl.uniform4f( location, value.x, value.y, value.z, value.w );
  3379. } else if ( type === "c" ) { // single THREE.Color
  3380. _gl.uniform3f( location, value.r, value.g, value.b );
  3381. } else if ( type === "iv1" ) { // flat array of integers (JS or typed array)
  3382. _gl.uniform1iv( location, value );
  3383. } else if ( type === "iv" ) { // flat array of integers with 3 x N size (JS or typed array)
  3384. _gl.uniform3iv( location, value );
  3385. } else if ( type === "fv1" ) { // flat array of floats (JS or typed array)
  3386. _gl.uniform1fv( location, value );
  3387. } else if ( type === "fv" ) { // flat array of floats with 3 x N size (JS or typed array)
  3388. _gl.uniform3fv( location, value );
  3389. } else if ( type === "v2v" ) { // array of THREE.Vector2
  3390. if ( uniform._array === undefined ) {
  3391. uniform._array = new Float32Array( 2 * value.length );
  3392. }
  3393. for ( i = 0, il = value.length; i < il; i ++ ) {
  3394. offset = i * 2;
  3395. uniform._array[ offset ] = value[ i ].x;
  3396. uniform._array[ offset + 1 ] = value[ i ].y;
  3397. }
  3398. _gl.uniform2fv( location, uniform._array );
  3399. } else if ( type === "v3v" ) { // array of THREE.Vector3
  3400. if ( uniform._array === undefined ) {
  3401. uniform._array = new Float32Array( 3 * value.length );
  3402. }
  3403. for ( i = 0, il = value.length; i < il; i ++ ) {
  3404. offset = i * 3;
  3405. uniform._array[ offset ] = value[ i ].x;
  3406. uniform._array[ offset + 1 ] = value[ i ].y;
  3407. uniform._array[ offset + 2 ] = value[ i ].z;
  3408. }
  3409. _gl.uniform3fv( location, uniform._array );
  3410. } else if ( type === "v4v" ) { // array of THREE.Vector4
  3411. if ( uniform._array === undefined ) {
  3412. uniform._array = new Float32Array( 4 * value.length );
  3413. }
  3414. for ( i = 0, il = value.length; i < il; i ++ ) {
  3415. offset = i * 4;
  3416. uniform._array[ offset ] = value[ i ].x;
  3417. uniform._array[ offset + 1 ] = value[ i ].y;
  3418. uniform._array[ offset + 2 ] = value[ i ].z;
  3419. uniform._array[ offset + 3 ] = value[ i ].w;
  3420. }
  3421. _gl.uniform4fv( location, uniform._array );
  3422. } else if ( type === "m4") { // single THREE.Matrix4
  3423. if ( uniform._array === undefined ) {
  3424. uniform._array = new Float32Array( 16 );
  3425. }
  3426. value.flattenToArray( uniform._array );
  3427. _gl.uniformMatrix4fv( location, false, uniform._array );
  3428. } else if ( type === "m4v" ) { // array of THREE.Matrix4
  3429. if ( uniform._array === undefined ) {
  3430. uniform._array = new Float32Array( 16 * value.length );
  3431. }
  3432. for ( i = 0, il = value.length; i < il; i ++ ) {
  3433. value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
  3434. }
  3435. _gl.uniformMatrix4fv( location, false, uniform._array );
  3436. } else if ( type === "t" ) { // single THREE.Texture (2d or cube)
  3437. texture = value;
  3438. textureUnit = getTextureUnit();
  3439. _gl.uniform1i( location, textureUnit );
  3440. if ( !texture ) continue;
  3441. if ( texture.image instanceof Array && texture.image.length === 6 ) {
  3442. setCubeTexture( texture, textureUnit );
  3443. } else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
  3444. setCubeTextureDynamic( texture, textureUnit );
  3445. } else {
  3446. _this.setTexture( texture, textureUnit );
  3447. }
  3448. } else if ( type === "tv" ) { // array of THREE.Texture (2d)
  3449. if ( uniform._array === undefined ) {
  3450. uniform._array = [];
  3451. }
  3452. for( i = 0, il = uniform.value.length; i < il; i ++ ) {
  3453. uniform._array[ i ] = getTextureUnit();
  3454. }
  3455. _gl.uniform1iv( location, uniform._array );
  3456. for( i = 0, il = uniform.value.length; i < il; i ++ ) {
  3457. texture = uniform.value[ i ];
  3458. textureUnit = uniform._array[ i ];
  3459. if ( !texture ) continue;
  3460. _this.setTexture( texture, textureUnit );
  3461. }
  3462. }
  3463. }
  3464. };
  3465. function setupMatrices ( object, camera ) {
  3466. object._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  3467. object._normalMatrix.getInverse( object._modelViewMatrix );
  3468. object._normalMatrix.transpose();
  3469. };
  3470. //
  3471. function setColorGamma( array, offset, color, intensitySq ) {
  3472. array[ offset ] = color.r * color.r * intensitySq;
  3473. array[ offset + 1 ] = color.g * color.g * intensitySq;
  3474. array[ offset + 2 ] = color.b * color.b * intensitySq;
  3475. };
  3476. function setColorLinear( array, offset, color, intensity ) {
  3477. array[ offset ] = color.r * intensity;
  3478. array[ offset + 1 ] = color.g * intensity;
  3479. array[ offset + 2 ] = color.b * intensity;
  3480. };
  3481. function setupLights ( program, lights ) {
  3482. var l, ll, light, n,
  3483. r = 0, g = 0, b = 0,
  3484. color, skyColor, groundColor,
  3485. intensity, intensitySq,
  3486. position,
  3487. distance,
  3488. zlights = _lights,
  3489. dirColors = zlights.directional.colors,
  3490. dirPositions = zlights.directional.positions,
  3491. pointColors = zlights.point.colors,
  3492. pointPositions = zlights.point.positions,
  3493. pointDistances = zlights.point.distances,
  3494. spotColors = zlights.spot.colors,
  3495. spotPositions = zlights.spot.positions,
  3496. spotDistances = zlights.spot.distances,
  3497. spotDirections = zlights.spot.directions,
  3498. spotAnglesCos = zlights.spot.anglesCos,
  3499. spotExponents = zlights.spot.exponents,
  3500. hemiSkyColors = zlights.hemi.skyColors,
  3501. hemiGroundColors = zlights.hemi.groundColors,
  3502. hemiPositions = zlights.hemi.positions,
  3503. dirLength = 0,
  3504. pointLength = 0,
  3505. spotLength = 0,
  3506. hemiLength = 0,
  3507. dirCount = 0,
  3508. pointCount = 0,
  3509. spotCount = 0,
  3510. hemiCount = 0,
  3511. dirOffset = 0,
  3512. pointOffset = 0,
  3513. spotOffset = 0,
  3514. hemiOffset = 0;
  3515. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  3516. light = lights[ l ];
  3517. if ( light.onlyShadow ) continue;
  3518. color = light.color;
  3519. intensity = light.intensity;
  3520. distance = light.distance;
  3521. if ( light instanceof THREE.AmbientLight ) {
  3522. if ( ! light.visible ) continue;
  3523. if ( _this.gammaInput ) {
  3524. r += color.r * color.r;
  3525. g += color.g * color.g;
  3526. b += color.b * color.b;
  3527. } else {
  3528. r += color.r;
  3529. g += color.g;
  3530. b += color.b;
  3531. }
  3532. } else if ( light instanceof THREE.DirectionalLight ) {
  3533. dirCount += 1;
  3534. if ( ! light.visible ) continue;
  3535. _direction.getPositionFromMatrix( light.matrixWorld );
  3536. _vector3.getPositionFromMatrix( light.target.matrixWorld );
  3537. _direction.sub( _vector3 );
  3538. _direction.normalize();
  3539. // skip lights with undefined direction
  3540. // these create troubles in OpenGL (making pixel black)
  3541. if ( _direction.x === 0 && _direction.y === 0 && _direction.z === 0 ) continue;
  3542. dirOffset = dirLength * 3;
  3543. dirPositions[ dirOffset ] = _direction.x;
  3544. dirPositions[ dirOffset + 1 ] = _direction.y;
  3545. dirPositions[ dirOffset + 2 ] = _direction.z;
  3546. if ( _this.gammaInput ) {
  3547. setColorGamma( dirColors, dirOffset, color, intensity * intensity );
  3548. } else {
  3549. setColorLinear( dirColors, dirOffset, color, intensity );
  3550. }
  3551. dirLength += 1;
  3552. } else if ( light instanceof THREE.PointLight ) {
  3553. pointCount += 1;
  3554. if ( ! light.visible ) continue;
  3555. pointOffset = pointLength * 3;
  3556. if ( _this.gammaInput ) {
  3557. setColorGamma( pointColors, pointOffset, color, intensity * intensity );
  3558. } else {
  3559. setColorLinear( pointColors, pointOffset, color, intensity );
  3560. }
  3561. _vector3.getPositionFromMatrix( light.matrixWorld );
  3562. pointPositions[ pointOffset ] = _vector3.x;
  3563. pointPositions[ pointOffset + 1 ] = _vector3.y;
  3564. pointPositions[ pointOffset + 2 ] = _vector3.z;
  3565. pointDistances[ pointLength ] = distance;
  3566. pointLength += 1;
  3567. } else if ( light instanceof THREE.SpotLight ) {
  3568. spotCount += 1;
  3569. if ( ! light.visible ) continue;
  3570. spotOffset = spotLength * 3;
  3571. if ( _this.gammaInput ) {
  3572. setColorGamma( spotColors, spotOffset, color, intensity * intensity );
  3573. } else {
  3574. setColorLinear( spotColors, spotOffset, color, intensity );
  3575. }
  3576. _vector3.getPositionFromMatrix( light.matrixWorld );
  3577. spotPositions[ spotOffset ] = _vector3.x;
  3578. spotPositions[ spotOffset + 1 ] = _vector3.y;
  3579. spotPositions[ spotOffset + 2 ] = _vector3.z;
  3580. spotDistances[ spotLength ] = distance;
  3581. _direction.copy( _vector3 );
  3582. _vector3.getPositionFromMatrix( light.target.matrixWorld );
  3583. _direction.sub( _vector3 );
  3584. _direction.normalize();
  3585. spotDirections[ spotOffset ] = _direction.x;
  3586. spotDirections[ spotOffset + 1 ] = _direction.y;
  3587. spotDirections[ spotOffset + 2 ] = _direction.z;
  3588. spotAnglesCos[ spotLength ] = Math.cos( light.angle );
  3589. spotExponents[ spotLength ] = light.exponent;
  3590. spotLength += 1;
  3591. } else if ( light instanceof THREE.HemisphereLight ) {
  3592. hemiCount += 1;
  3593. if ( ! light.visible ) continue;
  3594. _direction.getPositionFromMatrix( light.matrixWorld );
  3595. _direction.normalize();
  3596. // skip lights with undefined direction
  3597. // these create troubles in OpenGL (making pixel black)
  3598. if ( _direction.x === 0 && _direction.y === 0 && _direction.z === 0 ) continue;
  3599. hemiOffset = hemiLength * 3;
  3600. hemiPositions[ hemiOffset ] = _direction.x;
  3601. hemiPositions[ hemiOffset + 1 ] = _direction.y;
  3602. hemiPositions[ hemiOffset + 2 ] = _direction.z;
  3603. skyColor = light.color;
  3604. groundColor = light.groundColor;
  3605. if ( _this.gammaInput ) {
  3606. intensitySq = intensity * intensity;
  3607. setColorGamma( hemiSkyColors, hemiOffset, skyColor, intensitySq );
  3608. setColorGamma( hemiGroundColors, hemiOffset, groundColor, intensitySq );
  3609. } else {
  3610. setColorLinear( hemiSkyColors, hemiOffset, skyColor, intensity );
  3611. setColorLinear( hemiGroundColors, hemiOffset, groundColor, intensity );
  3612. }
  3613. hemiLength += 1;
  3614. }
  3615. }
  3616. // null eventual remains from removed lights
  3617. // (this is to avoid if in shader)
  3618. for ( l = dirLength * 3, ll = Math.max( dirColors.length, dirCount * 3 ); l < ll; l ++ ) dirColors[ l ] = 0.0;
  3619. for ( l = pointLength * 3, ll = Math.max( pointColors.length, pointCount * 3 ); l < ll; l ++ ) pointColors[ l ] = 0.0;
  3620. for ( l = spotLength * 3, ll = Math.max( spotColors.length, spotCount * 3 ); l < ll; l ++ ) spotColors[ l ] = 0.0;
  3621. for ( l = hemiLength * 3, ll = Math.max( hemiSkyColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiSkyColors[ l ] = 0.0;
  3622. for ( l = hemiLength * 3, ll = Math.max( hemiGroundColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiGroundColors[ l ] = 0.0;
  3623. zlights.directional.length = dirLength;
  3624. zlights.point.length = pointLength;
  3625. zlights.spot.length = spotLength;
  3626. zlights.hemi.length = hemiLength;
  3627. zlights.ambient[ 0 ] = r;
  3628. zlights.ambient[ 1 ] = g;
  3629. zlights.ambient[ 2 ] = b;
  3630. };
  3631. // GL state setting
  3632. this.setFaceCulling = function ( cullFace, frontFaceDirection ) {
  3633. if ( cullFace === THREE.CullFaceNone ) {
  3634. _gl.disable( _gl.CULL_FACE );
  3635. } else {
  3636. if ( frontFaceDirection === THREE.FrontFaceDirectionCW ) {
  3637. _gl.frontFace( _gl.CW );
  3638. } else {
  3639. _gl.frontFace( _gl.CCW );
  3640. }
  3641. if ( cullFace === THREE.CullFaceBack ) {
  3642. _gl.cullFace( _gl.BACK );
  3643. } else if ( cullFace === THREE.CullFaceFront ) {
  3644. _gl.cullFace( _gl.FRONT );
  3645. } else {
  3646. _gl.cullFace( _gl.FRONT_AND_BACK );
  3647. }
  3648. _gl.enable( _gl.CULL_FACE );
  3649. }
  3650. };
  3651. this.setMaterialFaces = function ( material ) {
  3652. var doubleSided = material.side === THREE.DoubleSide;
  3653. var flipSided = material.side === THREE.BackSide;
  3654. if ( _oldDoubleSided !== doubleSided ) {
  3655. if ( doubleSided ) {
  3656. _gl.disable( _gl.CULL_FACE );
  3657. } else {
  3658. _gl.enable( _gl.CULL_FACE );
  3659. }
  3660. _oldDoubleSided = doubleSided;
  3661. }
  3662. if ( _oldFlipSided !== flipSided ) {
  3663. if ( flipSided ) {
  3664. _gl.frontFace( _gl.CW );
  3665. } else {
  3666. _gl.frontFace( _gl.CCW );
  3667. }
  3668. _oldFlipSided = flipSided;
  3669. }
  3670. };
  3671. this.setDepthTest = function ( depthTest ) {
  3672. if ( _oldDepthTest !== depthTest ) {
  3673. if ( depthTest ) {
  3674. _gl.enable( _gl.DEPTH_TEST );
  3675. } else {
  3676. _gl.disable( _gl.DEPTH_TEST );
  3677. }
  3678. _oldDepthTest = depthTest;
  3679. }
  3680. };
  3681. this.setDepthWrite = function ( depthWrite ) {
  3682. if ( _oldDepthWrite !== depthWrite ) {
  3683. _gl.depthMask( depthWrite );
  3684. _oldDepthWrite = depthWrite;
  3685. }
  3686. };
  3687. function setLineWidth ( width ) {
  3688. if ( width !== _oldLineWidth ) {
  3689. _gl.lineWidth( width );
  3690. _oldLineWidth = width;
  3691. }
  3692. };
  3693. function setPolygonOffset ( polygonoffset, factor, units ) {
  3694. if ( _oldPolygonOffset !== polygonoffset ) {
  3695. if ( polygonoffset ) {
  3696. _gl.enable( _gl.POLYGON_OFFSET_FILL );
  3697. } else {
  3698. _gl.disable( _gl.POLYGON_OFFSET_FILL );
  3699. }
  3700. _oldPolygonOffset = polygonoffset;
  3701. }
  3702. if ( polygonoffset && ( _oldPolygonOffsetFactor !== factor || _oldPolygonOffsetUnits !== units ) ) {
  3703. _gl.polygonOffset( factor, units );
  3704. _oldPolygonOffsetFactor = factor;
  3705. _oldPolygonOffsetUnits = units;
  3706. }
  3707. };
  3708. this.setBlending = function ( blending, blendEquation, blendSrc, blendDst ) {
  3709. if ( blending !== _oldBlending ) {
  3710. if ( blending === THREE.NoBlending ) {
  3711. _gl.disable( _gl.BLEND );
  3712. } else if ( blending === THREE.AdditiveBlending ) {
  3713. _gl.enable( _gl.BLEND );
  3714. _gl.blendEquation( _gl.FUNC_ADD );
  3715. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE );
  3716. } else if ( blending === THREE.SubtractiveBlending ) {
  3717. // TODO: Find blendFuncSeparate() combination
  3718. _gl.enable( _gl.BLEND );
  3719. _gl.blendEquation( _gl.FUNC_ADD );
  3720. _gl.blendFunc( _gl.ZERO, _gl.ONE_MINUS_SRC_COLOR );
  3721. } else if ( blending === THREE.MultiplyBlending ) {
  3722. // TODO: Find blendFuncSeparate() combination
  3723. _gl.enable( _gl.BLEND );
  3724. _gl.blendEquation( _gl.FUNC_ADD );
  3725. _gl.blendFunc( _gl.ZERO, _gl.SRC_COLOR );
  3726. } else if ( blending === THREE.CustomBlending ) {
  3727. _gl.enable( _gl.BLEND );
  3728. } else {
  3729. _gl.enable( _gl.BLEND );
  3730. _gl.blendEquationSeparate( _gl.FUNC_ADD, _gl.FUNC_ADD );
  3731. _gl.blendFuncSeparate( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA, _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  3732. }
  3733. _oldBlending = blending;
  3734. }
  3735. if ( blending === THREE.CustomBlending ) {
  3736. if ( blendEquation !== _oldBlendEquation ) {
  3737. _gl.blendEquation( paramThreeToGL( blendEquation ) );
  3738. _oldBlendEquation = blendEquation;
  3739. }
  3740. if ( blendSrc !== _oldBlendSrc || blendDst !== _oldBlendDst ) {
  3741. _gl.blendFunc( paramThreeToGL( blendSrc ), paramThreeToGL( blendDst ) );
  3742. _oldBlendSrc = blendSrc;
  3743. _oldBlendDst = blendDst;
  3744. }
  3745. } else {
  3746. _oldBlendEquation = null;
  3747. _oldBlendSrc = null;
  3748. _oldBlendDst = null;
  3749. }
  3750. };
  3751. // Defines
  3752. function generateDefines ( defines ) {
  3753. var value, chunk, chunks = [];
  3754. for ( var d in defines ) {
  3755. value = defines[ d ];
  3756. if ( value === false ) continue;
  3757. chunk = "#define " + d + " " + value;
  3758. chunks.push( chunk );
  3759. }
  3760. return chunks.join( "\n" );
  3761. };
  3762. // Shaders
  3763. function buildProgram ( shaderID, fragmentShader, vertexShader, uniforms, attributes, defines, parameters ) {
  3764. var p, pl, d, program, code;
  3765. var chunks = [];
  3766. // Generate code
  3767. if ( shaderID ) {
  3768. chunks.push( shaderID );
  3769. } else {
  3770. chunks.push( fragmentShader );
  3771. chunks.push( vertexShader );
  3772. }
  3773. for ( d in defines ) {
  3774. chunks.push( d );
  3775. chunks.push( defines[ d ] );
  3776. }
  3777. for ( p in parameters ) {
  3778. chunks.push( p );
  3779. chunks.push( parameters[ p ] );
  3780. }
  3781. code = chunks.join();
  3782. // Check if code has been already compiled
  3783. for ( p = 0, pl = _programs.length; p < pl; p ++ ) {
  3784. var programInfo = _programs[ p ];
  3785. if ( programInfo.code === code ) {
  3786. //console.log( "Code already compiled." /*: \n\n" + code*/ );
  3787. programInfo.usedTimes ++;
  3788. return programInfo.program;
  3789. }
  3790. }
  3791. var shadowMapTypeDefine = "SHADOWMAP_TYPE_BASIC";
  3792. if ( parameters.shadowMapType === THREE.PCFShadowMap ) {
  3793. shadowMapTypeDefine = "SHADOWMAP_TYPE_PCF";
  3794. } else if ( parameters.shadowMapType === THREE.PCFSoftShadowMap ) {
  3795. shadowMapTypeDefine = "SHADOWMAP_TYPE_PCF_SOFT";
  3796. }
  3797. //console.log( "building new program " );
  3798. //
  3799. var customDefines = generateDefines( defines );
  3800. //
  3801. program = _gl.createProgram();
  3802. var prefix_vertex = [
  3803. "precision " + _precision + " float;",
  3804. customDefines,
  3805. _supportsVertexTextures ? "#define VERTEX_TEXTURES" : "",
  3806. _this.gammaInput ? "#define GAMMA_INPUT" : "",
  3807. _this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
  3808. _this.physicallyBasedShading ? "#define PHYSICALLY_BASED_SHADING" : "",
  3809. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  3810. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  3811. "#define MAX_SPOT_LIGHTS " + parameters.maxSpotLights,
  3812. "#define MAX_HEMI_LIGHTS " + parameters.maxHemiLights,
  3813. "#define MAX_SHADOWS " + parameters.maxShadows,
  3814. "#define MAX_BONES " + parameters.maxBones,
  3815. parameters.map ? "#define USE_MAP" : "",
  3816. parameters.envMap ? "#define USE_ENVMAP" : "",
  3817. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  3818. parameters.bumpMap ? "#define USE_BUMPMAP" : "",
  3819. parameters.normalMap ? "#define USE_NORMALMAP" : "",
  3820. parameters.specularMap ? "#define USE_SPECULARMAP" : "",
  3821. parameters.vertexColors ? "#define USE_COLOR" : "",
  3822. parameters.skinning ? "#define USE_SKINNING" : "",
  3823. parameters.useVertexTexture ? "#define BONE_TEXTURE" : "",
  3824. parameters.boneTextureWidth ? "#define N_BONE_PIXEL_X " + parameters.boneTextureWidth.toFixed( 1 ) : "",
  3825. parameters.boneTextureHeight ? "#define N_BONE_PIXEL_Y " + parameters.boneTextureHeight.toFixed( 1 ) : "",
  3826. parameters.morphTargets ? "#define USE_MORPHTARGETS" : "",
  3827. parameters.morphNormals ? "#define USE_MORPHNORMALS" : "",
  3828. parameters.perPixel ? "#define PHONG_PER_PIXEL" : "",
  3829. parameters.wrapAround ? "#define WRAP_AROUND" : "",
  3830. parameters.doubleSided ? "#define DOUBLE_SIDED" : "",
  3831. parameters.flipSided ? "#define FLIP_SIDED" : "",
  3832. parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "",
  3833. parameters.shadowMapEnabled ? "#define " + shadowMapTypeDefine : "",
  3834. parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "",
  3835. parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "",
  3836. parameters.sizeAttenuation ? "#define USE_SIZEATTENUATION" : "",
  3837. "uniform mat4 modelMatrix;",
  3838. "uniform mat4 modelViewMatrix;",
  3839. "uniform mat4 projectionMatrix;",
  3840. "uniform mat4 viewMatrix;",
  3841. "uniform mat3 normalMatrix;",
  3842. "uniform vec3 cameraPosition;",
  3843. "attribute vec3 position;",
  3844. "attribute vec3 normal;",
  3845. "attribute vec2 uv;",
  3846. "attribute vec2 uv2;",
  3847. "#ifdef USE_COLOR",
  3848. "attribute vec3 color;",
  3849. "#endif",
  3850. "#ifdef USE_MORPHTARGETS",
  3851. "attribute vec3 morphTarget0;",
  3852. "attribute vec3 morphTarget1;",
  3853. "attribute vec3 morphTarget2;",
  3854. "attribute vec3 morphTarget3;",
  3855. "#ifdef USE_MORPHNORMALS",
  3856. "attribute vec3 morphNormal0;",
  3857. "attribute vec3 morphNormal1;",
  3858. "attribute vec3 morphNormal2;",
  3859. "attribute vec3 morphNormal3;",
  3860. "#else",
  3861. "attribute vec3 morphTarget4;",
  3862. "attribute vec3 morphTarget5;",
  3863. "attribute vec3 morphTarget6;",
  3864. "attribute vec3 morphTarget7;",
  3865. "#endif",
  3866. "#endif",
  3867. "#ifdef USE_SKINNING",
  3868. "attribute vec4 skinIndex;",
  3869. "attribute vec4 skinWeight;",
  3870. "#endif",
  3871. ""
  3872. ].join("\n");
  3873. var prefix_fragment = [
  3874. "precision " + _precision + " float;",
  3875. ( parameters.bumpMap || parameters.normalMap ) ? "#extension GL_OES_standard_derivatives : enable" : "",
  3876. customDefines,
  3877. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  3878. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  3879. "#define MAX_SPOT_LIGHTS " + parameters.maxSpotLights,
  3880. "#define MAX_HEMI_LIGHTS " + parameters.maxHemiLights,
  3881. "#define MAX_SHADOWS " + parameters.maxShadows,
  3882. parameters.alphaTest ? "#define ALPHATEST " + parameters.alphaTest: "",
  3883. _this.gammaInput ? "#define GAMMA_INPUT" : "",
  3884. _this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
  3885. _this.physicallyBasedShading ? "#define PHYSICALLY_BASED_SHADING" : "",
  3886. ( parameters.useFog && parameters.fog ) ? "#define USE_FOG" : "",
  3887. ( parameters.useFog && parameters.fogExp ) ? "#define FOG_EXP2" : "",
  3888. parameters.map ? "#define USE_MAP" : "",
  3889. parameters.envMap ? "#define USE_ENVMAP" : "",
  3890. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  3891. parameters.bumpMap ? "#define USE_BUMPMAP" : "",
  3892. parameters.normalMap ? "#define USE_NORMALMAP" : "",
  3893. parameters.specularMap ? "#define USE_SPECULARMAP" : "",
  3894. parameters.vertexColors ? "#define USE_COLOR" : "",
  3895. parameters.metal ? "#define METAL" : "",
  3896. parameters.perPixel ? "#define PHONG_PER_PIXEL" : "",
  3897. parameters.wrapAround ? "#define WRAP_AROUND" : "",
  3898. parameters.doubleSided ? "#define DOUBLE_SIDED" : "",
  3899. parameters.flipSided ? "#define FLIP_SIDED" : "",
  3900. parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "",
  3901. parameters.shadowMapEnabled ? "#define " + shadowMapTypeDefine : "",
  3902. parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "",
  3903. parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "",
  3904. "uniform mat4 viewMatrix;",
  3905. "uniform vec3 cameraPosition;",
  3906. ""
  3907. ].join("\n");
  3908. var glFragmentShader = getShader( "fragment", prefix_fragment + fragmentShader );
  3909. var glVertexShader = getShader( "vertex", prefix_vertex + vertexShader );
  3910. _gl.attachShader( program, glVertexShader );
  3911. _gl.attachShader( program, glFragmentShader );
  3912. _gl.linkProgram( program );
  3913. if ( !_gl.getProgramParameter( program, _gl.LINK_STATUS ) ) {
  3914. console.error( "Could not initialise shader\n" + "VALIDATE_STATUS: " + _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) + ", gl error [" + _gl.getError() + "]" );
  3915. }
  3916. // clean up
  3917. _gl.deleteShader( glFragmentShader );
  3918. _gl.deleteShader( glVertexShader );
  3919. //console.log( prefix_fragment + fragmentShader );
  3920. //console.log( prefix_vertex + vertexShader );
  3921. program.uniforms = {};
  3922. program.attributes = {};
  3923. var identifiers, u, a, i;
  3924. // cache uniform locations
  3925. identifiers = [
  3926. 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'modelMatrix', 'cameraPosition',
  3927. 'morphTargetInfluences'
  3928. ];
  3929. if ( parameters.useVertexTexture ) {
  3930. identifiers.push( 'boneTexture' );
  3931. } else {
  3932. identifiers.push( 'boneGlobalMatrices' );
  3933. }
  3934. for ( u in uniforms ) {
  3935. identifiers.push( u );
  3936. }
  3937. cacheUniformLocations( program, identifiers );
  3938. // cache attributes locations
  3939. identifiers = [
  3940. "position", "normal", "uv", "uv2", "tangent", "color",
  3941. "skinIndex", "skinWeight", "lineDistance"
  3942. ];
  3943. for ( i = 0; i < parameters.maxMorphTargets; i ++ ) {
  3944. identifiers.push( "morphTarget" + i );
  3945. }
  3946. for ( i = 0; i < parameters.maxMorphNormals; i ++ ) {
  3947. identifiers.push( "morphNormal" + i );
  3948. }
  3949. for ( a in attributes ) {
  3950. identifiers.push( a );
  3951. }
  3952. cacheAttributeLocations( program, identifiers );
  3953. program.id = _programs_counter ++;
  3954. _programs.push( { program: program, code: code, usedTimes: 1 } );
  3955. _this.info.memory.programs = _programs.length;
  3956. return program;
  3957. };
  3958. // Shader parameters cache
  3959. function cacheUniformLocations ( program, identifiers ) {
  3960. var i, l, id;
  3961. for( i = 0, l = identifiers.length; i < l; i ++ ) {
  3962. id = identifiers[ i ];
  3963. program.uniforms[ id ] = _gl.getUniformLocation( program, id );
  3964. }
  3965. };
  3966. function cacheAttributeLocations ( program, identifiers ) {
  3967. var i, l, id;
  3968. for( i = 0, l = identifiers.length; i < l; i ++ ) {
  3969. id = identifiers[ i ];
  3970. program.attributes[ id ] = _gl.getAttribLocation( program, id );
  3971. }
  3972. };
  3973. function addLineNumbers ( string ) {
  3974. var chunks = string.split( "\n" );
  3975. for ( var i = 0, il = chunks.length; i < il; i ++ ) {
  3976. // Chrome reports shader errors on lines
  3977. // starting counting from 1
  3978. chunks[ i ] = ( i + 1 ) + ": " + chunks[ i ];
  3979. }
  3980. return chunks.join( "\n" );
  3981. };
  3982. function getShader ( type, string ) {
  3983. var shader;
  3984. if ( type === "fragment" ) {
  3985. shader = _gl.createShader( _gl.FRAGMENT_SHADER );
  3986. } else if ( type === "vertex" ) {
  3987. shader = _gl.createShader( _gl.VERTEX_SHADER );
  3988. }
  3989. _gl.shaderSource( shader, string );
  3990. _gl.compileShader( shader );
  3991. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  3992. console.error( _gl.getShaderInfoLog( shader ) );
  3993. console.error( addLineNumbers( string ) );
  3994. return null;
  3995. }
  3996. return shader;
  3997. };
  3998. // Textures
  3999. function isPowerOfTwo ( value ) {
  4000. return ( value & ( value - 1 ) ) === 0;
  4001. };
  4002. function setTextureParameters ( textureType, texture, isImagePowerOfTwo ) {
  4003. if ( isImagePowerOfTwo ) {
  4004. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) );
  4005. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) );
  4006. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.magFilter ) );
  4007. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.minFilter ) );
  4008. } else {
  4009. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  4010. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  4011. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
  4012. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
  4013. }
  4014. if ( _glExtensionTextureFilterAnisotropic && texture.type !== THREE.FloatType ) {
  4015. if ( texture.anisotropy > 1 || texture.__oldAnisotropy ) {
  4016. _gl.texParameterf( textureType, _glExtensionTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, _maxAnisotropy ) );
  4017. texture.__oldAnisotropy = texture.anisotropy;
  4018. }
  4019. }
  4020. };
  4021. this.setTexture = function ( texture, slot ) {
  4022. if ( texture.needsUpdate ) {
  4023. if ( ! texture.__webglInit ) {
  4024. texture.__webglInit = true;
  4025. texture.addEventListener( 'dispose', onTextureDispose );
  4026. texture.__webglTexture = _gl.createTexture();
  4027. _this.info.memory.textures ++;
  4028. }
  4029. _gl.activeTexture( _gl.TEXTURE0 + slot );
  4030. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  4031. _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  4032. _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  4033. _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
  4034. var image = texture.image,
  4035. isImagePowerOfTwo = isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ),
  4036. glFormat = paramThreeToGL( texture.format ),
  4037. glType = paramThreeToGL( texture.type );
  4038. setTextureParameters( _gl.TEXTURE_2D, texture, isImagePowerOfTwo );
  4039. var mipmap, mipmaps = texture.mipmaps;
  4040. if ( texture instanceof THREE.DataTexture ) {
  4041. // use manually created mipmaps if available
  4042. // if there are no manual mipmaps
  4043. // set 0 level mipmap and then use GL to generate other mipmap levels
  4044. if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
  4045. for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
  4046. mipmap = mipmaps[ i ];
  4047. _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  4048. }
  4049. texture.generateMipmaps = false;
  4050. } else {
  4051. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, image.width, image.height, 0, glFormat, glType, image.data );
  4052. }
  4053. } else if ( texture instanceof THREE.CompressedTexture ) {
  4054. // compressed textures can only use manually created mipmaps
  4055. // WebGL can't generate mipmaps for DDS textures
  4056. for( var i = 0, il = mipmaps.length; i < il; i ++ ) {
  4057. mipmap = mipmaps[ i ];
  4058. _gl.compressedTexImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
  4059. }
  4060. } else { // regular Texture (image, video, canvas)
  4061. // use manually created mipmaps if available
  4062. // if there are no manual mipmaps
  4063. // set 0 level mipmap and then use GL to generate other mipmap levels
  4064. if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
  4065. for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
  4066. mipmap = mipmaps[ i ];
  4067. _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, glFormat, glType, mipmap );
  4068. }
  4069. texture.generateMipmaps = false;
  4070. } else {
  4071. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, glFormat, glType, texture.image );
  4072. }
  4073. }
  4074. if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  4075. texture.needsUpdate = false;
  4076. if ( texture.onUpdate ) texture.onUpdate();
  4077. } else {
  4078. _gl.activeTexture( _gl.TEXTURE0 + slot );
  4079. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  4080. }
  4081. };
  4082. function clampToMaxSize ( image, maxSize ) {
  4083. if ( image.width <= maxSize && image.height <= maxSize ) {
  4084. return image;
  4085. }
  4086. // Warning: Scaling through the canvas will only work with images that use
  4087. // premultiplied alpha.
  4088. var maxDimension = Math.max( image.width, image.height );
  4089. var newWidth = Math.floor( image.width * maxSize / maxDimension );
  4090. var newHeight = Math.floor( image.height * maxSize / maxDimension );
  4091. var canvas = document.createElement( 'canvas' );
  4092. canvas.width = newWidth;
  4093. canvas.height = newHeight;
  4094. var ctx = canvas.getContext( "2d" );
  4095. ctx.drawImage( image, 0, 0, image.width, image.height, 0, 0, newWidth, newHeight );
  4096. return canvas;
  4097. }
  4098. function setCubeTexture ( texture, slot ) {
  4099. if ( texture.image.length === 6 ) {
  4100. if ( texture.needsUpdate ) {
  4101. if ( ! texture.image.__webglTextureCube ) {
  4102. texture.image.__webglTextureCube = _gl.createTexture();
  4103. _this.info.memory.textures ++;
  4104. }
  4105. _gl.activeTexture( _gl.TEXTURE0 + slot );
  4106. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  4107. _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  4108. var isCompressed = texture instanceof THREE.CompressedTexture;
  4109. var cubeImage = [];
  4110. for ( var i = 0; i < 6; i ++ ) {
  4111. if ( _this.autoScaleCubemaps && ! isCompressed ) {
  4112. cubeImage[ i ] = clampToMaxSize( texture.image[ i ], _maxCubemapSize );
  4113. } else {
  4114. cubeImage[ i ] = texture.image[ i ];
  4115. }
  4116. }
  4117. var image = cubeImage[ 0 ],
  4118. isImagePowerOfTwo = isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ),
  4119. glFormat = paramThreeToGL( texture.format ),
  4120. glType = paramThreeToGL( texture.type );
  4121. setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isImagePowerOfTwo );
  4122. for ( var i = 0; i < 6; i ++ ) {
  4123. if ( isCompressed ) {
  4124. var mipmap, mipmaps = cubeImage[ i ].mipmaps;
  4125. for( var j = 0, jl = mipmaps.length; j < jl; j ++ ) {
  4126. mipmap = mipmaps[ j ];
  4127. _gl.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
  4128. }
  4129. } else {
  4130. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] );
  4131. }
  4132. }
  4133. if ( texture.generateMipmaps && isImagePowerOfTwo ) {
  4134. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  4135. }
  4136. texture.needsUpdate = false;
  4137. if ( texture.onUpdate ) texture.onUpdate();
  4138. } else {
  4139. _gl.activeTexture( _gl.TEXTURE0 + slot );
  4140. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  4141. }
  4142. }
  4143. };
  4144. function setCubeTextureDynamic ( texture, slot ) {
  4145. _gl.activeTexture( _gl.TEXTURE0 + slot );
  4146. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.__webglTexture );
  4147. };
  4148. // Render targets
  4149. function setupFrameBuffer ( framebuffer, renderTarget, textureTarget ) {
  4150. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  4151. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureTarget, renderTarget.__webglTexture, 0 );
  4152. };
  4153. function setupRenderBuffer ( renderbuffer, renderTarget ) {
  4154. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );
  4155. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  4156. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );
  4157. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  4158. /* For some reason this is not working. Defaulting to RGBA4.
  4159. } else if( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  4160. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height );
  4161. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  4162. */
  4163. } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  4164. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
  4165. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  4166. } else {
  4167. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );
  4168. }
  4169. };
  4170. this.setRenderTarget = function ( renderTarget ) {
  4171. var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
  4172. if ( renderTarget && ! renderTarget.__webglFramebuffer ) {
  4173. if ( renderTarget.depthBuffer === undefined ) renderTarget.depthBuffer = true;
  4174. if ( renderTarget.stencilBuffer === undefined ) renderTarget.stencilBuffer = true;
  4175. renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
  4176. renderTarget.__webglTexture = _gl.createTexture();
  4177. _this.info.memory.textures ++;
  4178. // Setup texture, create render and frame buffers
  4179. var isTargetPowerOfTwo = isPowerOfTwo( renderTarget.width ) && isPowerOfTwo( renderTarget.height ),
  4180. glFormat = paramThreeToGL( renderTarget.format ),
  4181. glType = paramThreeToGL( renderTarget.type );
  4182. if ( isCube ) {
  4183. renderTarget.__webglFramebuffer = [];
  4184. renderTarget.__webglRenderbuffer = [];
  4185. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  4186. setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget, isTargetPowerOfTwo );
  4187. for ( var i = 0; i < 6; i ++ ) {
  4188. renderTarget.__webglFramebuffer[ i ] = _gl.createFramebuffer();
  4189. renderTarget.__webglRenderbuffer[ i ] = _gl.createRenderbuffer();
  4190. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  4191. setupFrameBuffer( renderTarget.__webglFramebuffer[ i ], renderTarget, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
  4192. setupRenderBuffer( renderTarget.__webglRenderbuffer[ i ], renderTarget );
  4193. }
  4194. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  4195. } else {
  4196. renderTarget.__webglFramebuffer = _gl.createFramebuffer();
  4197. if ( renderTarget.shareDepthFrom ) {
  4198. renderTarget.__webglRenderbuffer = renderTarget.shareDepthFrom.__webglRenderbuffer;
  4199. } else {
  4200. renderTarget.__webglRenderbuffer = _gl.createRenderbuffer();
  4201. }
  4202. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  4203. setTextureParameters( _gl.TEXTURE_2D, renderTarget, isTargetPowerOfTwo );
  4204. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  4205. setupFrameBuffer( renderTarget.__webglFramebuffer, renderTarget, _gl.TEXTURE_2D );
  4206. if ( renderTarget.shareDepthFrom ) {
  4207. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  4208. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer );
  4209. } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  4210. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer );
  4211. }
  4212. } else {
  4213. setupRenderBuffer( renderTarget.__webglRenderbuffer, renderTarget );
  4214. }
  4215. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  4216. }
  4217. // Release everything
  4218. if ( isCube ) {
  4219. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  4220. } else {
  4221. _gl.bindTexture( _gl.TEXTURE_2D, null );
  4222. }
  4223. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  4224. _gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
  4225. }
  4226. var framebuffer, width, height, vx, vy;
  4227. if ( renderTarget ) {
  4228. if ( isCube ) {
  4229. framebuffer = renderTarget.__webglFramebuffer[ renderTarget.activeCubeFace ];
  4230. } else {
  4231. framebuffer = renderTarget.__webglFramebuffer;
  4232. }
  4233. width = renderTarget.width;
  4234. height = renderTarget.height;
  4235. vx = 0;
  4236. vy = 0;
  4237. } else {
  4238. framebuffer = null;
  4239. width = _viewportWidth;
  4240. height = _viewportHeight;
  4241. vx = _viewportX;
  4242. vy = _viewportY;
  4243. }
  4244. if ( framebuffer !== _currentFramebuffer ) {
  4245. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  4246. _gl.viewport( vx, vy, width, height );
  4247. _currentFramebuffer = framebuffer;
  4248. }
  4249. _currentWidth = width;
  4250. _currentHeight = height;
  4251. };
  4252. function updateRenderTargetMipmap ( renderTarget ) {
  4253. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  4254. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  4255. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  4256. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  4257. } else {
  4258. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  4259. _gl.generateMipmap( _gl.TEXTURE_2D );
  4260. _gl.bindTexture( _gl.TEXTURE_2D, null );
  4261. }
  4262. };
  4263. // Fallback filters for non-power-of-2 textures
  4264. function filterFallback ( f ) {
  4265. if ( f === THREE.NearestFilter || f === THREE.NearestMipMapNearestFilter || f === THREE.NearestMipMapLinearFilter ) {
  4266. return _gl.NEAREST;
  4267. }
  4268. return _gl.LINEAR;
  4269. };
  4270. // Map three.js constants to WebGL constants
  4271. function paramThreeToGL ( p ) {
  4272. if ( p === THREE.RepeatWrapping ) return _gl.REPEAT;
  4273. if ( p === THREE.ClampToEdgeWrapping ) return _gl.CLAMP_TO_EDGE;
  4274. if ( p === THREE.MirroredRepeatWrapping ) return _gl.MIRRORED_REPEAT;
  4275. if ( p === THREE.NearestFilter ) return _gl.NEAREST;
  4276. if ( p === THREE.NearestMipMapNearestFilter ) return _gl.NEAREST_MIPMAP_NEAREST;
  4277. if ( p === THREE.NearestMipMapLinearFilter ) return _gl.NEAREST_MIPMAP_LINEAR;
  4278. if ( p === THREE.LinearFilter ) return _gl.LINEAR;
  4279. if ( p === THREE.LinearMipMapNearestFilter ) return _gl.LINEAR_MIPMAP_NEAREST;
  4280. if ( p === THREE.LinearMipMapLinearFilter ) return _gl.LINEAR_MIPMAP_LINEAR;
  4281. if ( p === THREE.UnsignedByteType ) return _gl.UNSIGNED_BYTE;
  4282. if ( p === THREE.UnsignedShort4444Type ) return _gl.UNSIGNED_SHORT_4_4_4_4;
  4283. if ( p === THREE.UnsignedShort5551Type ) return _gl.UNSIGNED_SHORT_5_5_5_1;
  4284. if ( p === THREE.UnsignedShort565Type ) return _gl.UNSIGNED_SHORT_5_6_5;
  4285. if ( p === THREE.ByteType ) return _gl.BYTE;
  4286. if ( p === THREE.ShortType ) return _gl.SHORT;
  4287. if ( p === THREE.UnsignedShortType ) return _gl.UNSIGNED_SHORT;
  4288. if ( p === THREE.IntType ) return _gl.INT;
  4289. if ( p === THREE.UnsignedIntType ) return _gl.UNSIGNED_INT;
  4290. if ( p === THREE.FloatType ) return _gl.FLOAT;
  4291. if ( p === THREE.AlphaFormat ) return _gl.ALPHA;
  4292. if ( p === THREE.RGBFormat ) return _gl.RGB;
  4293. if ( p === THREE.RGBAFormat ) return _gl.RGBA;
  4294. if ( p === THREE.LuminanceFormat ) return _gl.LUMINANCE;
  4295. if ( p === THREE.LuminanceAlphaFormat ) return _gl.LUMINANCE_ALPHA;
  4296. if ( p === THREE.AddEquation ) return _gl.FUNC_ADD;
  4297. if ( p === THREE.SubtractEquation ) return _gl.FUNC_SUBTRACT;
  4298. if ( p === THREE.ReverseSubtractEquation ) return _gl.FUNC_REVERSE_SUBTRACT;
  4299. if ( p === THREE.ZeroFactor ) return _gl.ZERO;
  4300. if ( p === THREE.OneFactor ) return _gl.ONE;
  4301. if ( p === THREE.SrcColorFactor ) return _gl.SRC_COLOR;
  4302. if ( p === THREE.OneMinusSrcColorFactor ) return _gl.ONE_MINUS_SRC_COLOR;
  4303. if ( p === THREE.SrcAlphaFactor ) return _gl.SRC_ALPHA;
  4304. if ( p === THREE.OneMinusSrcAlphaFactor ) return _gl.ONE_MINUS_SRC_ALPHA;
  4305. if ( p === THREE.DstAlphaFactor ) return _gl.DST_ALPHA;
  4306. if ( p === THREE.OneMinusDstAlphaFactor ) return _gl.ONE_MINUS_DST_ALPHA;
  4307. if ( p === THREE.DstColorFactor ) return _gl.DST_COLOR;
  4308. if ( p === THREE.OneMinusDstColorFactor ) return _gl.ONE_MINUS_DST_COLOR;
  4309. if ( p === THREE.SrcAlphaSaturateFactor ) return _gl.SRC_ALPHA_SATURATE;
  4310. if ( _glExtensionCompressedTextureS3TC !== undefined ) {
  4311. if ( p === THREE.RGB_S3TC_DXT1_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGB_S3TC_DXT1_EXT;
  4312. if ( p === THREE.RGBA_S3TC_DXT1_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT1_EXT;
  4313. if ( p === THREE.RGBA_S3TC_DXT3_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT3_EXT;
  4314. if ( p === THREE.RGBA_S3TC_DXT5_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT5_EXT;
  4315. }
  4316. return 0;
  4317. };
  4318. // Allocations
  4319. function allocateBones ( object ) {
  4320. if ( _supportsBoneTextures && object && object.useVertexTexture ) {
  4321. return 1024;
  4322. } else {
  4323. // default for when object is not specified
  4324. // ( for example when prebuilding shader
  4325. // to be used with multiple objects )
  4326. //
  4327. // - leave some extra space for other uniforms
  4328. // - limit here is ANGLE's 254 max uniform vectors
  4329. // (up to 54 should be safe)
  4330. var nVertexUniforms = _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS );
  4331. var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
  4332. var maxBones = nVertexMatrices;
  4333. if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
  4334. maxBones = Math.min( object.bones.length, maxBones );
  4335. if ( maxBones < object.bones.length ) {
  4336. console.warn( "WebGLRenderer: too many bones - " + object.bones.length + ", this GPU supports just " + maxBones + " (try OpenGL instead of ANGLE)" );
  4337. }
  4338. }
  4339. return maxBones;
  4340. }
  4341. };
  4342. function allocateLights ( lights ) {
  4343. var l, ll, light, dirLights, pointLights, spotLights, hemiLights;
  4344. dirLights = pointLights = spotLights = hemiLights = 0;
  4345. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  4346. light = lights[ l ];
  4347. if ( light.onlyShadow ) continue;
  4348. if ( light instanceof THREE.DirectionalLight ) dirLights ++;
  4349. if ( light instanceof THREE.PointLight ) pointLights ++;
  4350. if ( light instanceof THREE.SpotLight ) spotLights ++;
  4351. if ( light instanceof THREE.HemisphereLight ) hemiLights ++;
  4352. }
  4353. return { 'directional' : dirLights, 'point' : pointLights, 'spot': spotLights, 'hemi': hemiLights };
  4354. };
  4355. function allocateShadows ( lights ) {
  4356. var l, ll, light, maxShadows = 0;
  4357. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  4358. light = lights[ l ];
  4359. if ( ! light.castShadow ) continue;
  4360. if ( light instanceof THREE.SpotLight ) maxShadows ++;
  4361. if ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) maxShadows ++;
  4362. }
  4363. return maxShadows;
  4364. };
  4365. // Initialization
  4366. function initGL () {
  4367. try {
  4368. if ( ! ( _gl = _canvas.getContext( 'experimental-webgl', { alpha: _alpha, premultipliedAlpha: _premultipliedAlpha, antialias: _antialias, stencil: _stencil, preserveDrawingBuffer: _preserveDrawingBuffer } ) ) ) {
  4369. throw 'Error creating WebGL context.';
  4370. }
  4371. } catch ( error ) {
  4372. console.error( error );
  4373. }
  4374. _glExtensionTextureFloat = _gl.getExtension( 'OES_texture_float' );
  4375. _glExtensionStandardDerivatives = _gl.getExtension( 'OES_standard_derivatives' );
  4376. _glExtensionTextureFilterAnisotropic = _gl.getExtension( 'EXT_texture_filter_anisotropic' ) ||
  4377. _gl.getExtension( 'MOZ_EXT_texture_filter_anisotropic' ) ||
  4378. _gl.getExtension( 'WEBKIT_EXT_texture_filter_anisotropic' );
  4379. _glExtensionCompressedTextureS3TC = _gl.getExtension( 'WEBGL_compressed_texture_s3tc' ) ||
  4380. _gl.getExtension( 'MOZ_WEBGL_compressed_texture_s3tc' ) ||
  4381. _gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_s3tc' );
  4382. if ( ! _glExtensionTextureFloat ) {
  4383. console.log( 'THREE.WebGLRenderer: Float textures not supported.' );
  4384. }
  4385. if ( ! _glExtensionStandardDerivatives ) {
  4386. console.log( 'THREE.WebGLRenderer: Standard derivatives not supported.' );
  4387. }
  4388. if ( ! _glExtensionTextureFilterAnisotropic ) {
  4389. console.log( 'THREE.WebGLRenderer: Anisotropic texture filtering not supported.' );
  4390. }
  4391. if ( ! _glExtensionCompressedTextureS3TC ) {
  4392. console.log( 'THREE.WebGLRenderer: S3TC compressed textures not supported.' );
  4393. }
  4394. if ( _gl.getShaderPrecisionFormat === undefined ) {
  4395. _gl.getShaderPrecisionFormat = function() {
  4396. return {
  4397. "rangeMin" : 1,
  4398. "rangeMax" : 1,
  4399. "precision" : 1
  4400. };
  4401. }
  4402. }
  4403. };
  4404. function setDefaultGLState () {
  4405. _gl.clearColor( 0, 0, 0, 1 );
  4406. _gl.clearDepth( 1 );
  4407. _gl.clearStencil( 0 );
  4408. _gl.enable( _gl.DEPTH_TEST );
  4409. _gl.depthFunc( _gl.LEQUAL );
  4410. _gl.frontFace( _gl.CCW );
  4411. _gl.cullFace( _gl.BACK );
  4412. _gl.enable( _gl.CULL_FACE );
  4413. _gl.enable( _gl.BLEND );
  4414. _gl.blendEquation( _gl.FUNC_ADD );
  4415. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
  4416. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  4417. };
  4418. // default plugins (order is important)
  4419. this.shadowMapPlugin = new THREE.ShadowMapPlugin();
  4420. this.addPrePlugin( this.shadowMapPlugin );
  4421. this.addPostPlugin( new THREE.SpritePlugin() );
  4422. this.addPostPlugin( new THREE.LensFlarePlugin() );
  4423. };