WebGLRenderer.js 175 KB

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