WebGLRenderer.js 156 KB

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