WebGLRenderer.js 174 KB

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