WebGLRenderer.js 149 KB

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