WebGLRenderer.js 135 KB

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