WebGLRenderer.js 163 KB

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