WebGLRenderer.js 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745
  1. /**
  2. * @author supereggbert / http://www.paulbrunt.co.uk/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author szimek / https://github.com/szimek/
  6. */
  7. THREE.WebGLRenderer = function ( parameters ) {
  8. console.log( 'THREE.WebGLRenderer', THREE.REVISION );
  9. parameters = parameters || {};
  10. var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
  11. _context = parameters.context !== undefined ? parameters.context : null,
  12. _width = _canvas.width,
  13. _height = _canvas.height,
  14. pixelRatio = 1,
  15. _precision = parameters.precision !== undefined ? parameters.precision : 'highp',
  16. _alpha = parameters.alpha !== undefined ? parameters.alpha : false,
  17. _depth = parameters.depth !== undefined ? parameters.depth : true,
  18. _stencil = parameters.stencil !== undefined ? parameters.stencil : true,
  19. _antialias = parameters.antialias !== undefined ? parameters.antialias : false,
  20. _premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
  21. _preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
  22. _logarithmicDepthBuffer = parameters.logarithmicDepthBuffer !== undefined ? parameters.logarithmicDepthBuffer : false,
  23. _clearColor = new THREE.Color( 0x000000 ),
  24. _clearAlpha = 0;
  25. var lights = [];
  26. var opaqueObjects = [];
  27. var transparentObjects = [];
  28. var opaqueImmediateObjects = [];
  29. var transparentImmediateObjects = [];
  30. var morphInfluences = new Float32Array( 8 );
  31. var sprites = [];
  32. var lensFlares = [];
  33. // public properties
  34. this.domElement = _canvas;
  35. this.context = null;
  36. // clearing
  37. this.autoClear = true;
  38. this.autoClearColor = true;
  39. this.autoClearDepth = true;
  40. this.autoClearStencil = true;
  41. // scene graph
  42. this.sortObjects = true;
  43. // physically based shading
  44. this.gammaFactor = 2.0; // for backwards compatibility
  45. this.gammaInput = false;
  46. this.gammaOutput = false;
  47. // morphs
  48. this.maxMorphTargets = 8;
  49. this.maxMorphNormals = 4;
  50. // flags
  51. this.autoScaleCubemaps = true;
  52. // internal properties
  53. var _this = this,
  54. _programs = [],
  55. // internal state cache
  56. _currentProgram = null,
  57. _currentFramebuffer = null,
  58. _currentMaterialId = - 1,
  59. _currentGeometryProgram = '',
  60. _currentCamera = null,
  61. _usedTextureUnits = 0,
  62. _viewportX = 0,
  63. _viewportY = 0,
  64. _viewportWidth = _canvas.width,
  65. _viewportHeight = _canvas.height,
  66. _currentWidth = 0,
  67. _currentHeight = 0,
  68. // frustum
  69. _frustum = new THREE.Frustum(),
  70. // camera matrices cache
  71. _projScreenMatrix = new THREE.Matrix4(),
  72. _vector3 = new THREE.Vector3(),
  73. // light arrays cache
  74. _direction = new THREE.Vector3(),
  75. _lightsNeedUpdate = true,
  76. _lights = {
  77. ambient: [ 0, 0, 0 ],
  78. directional: { length: 0, colors: [], positions: [] },
  79. point: { length: 0, colors: [], positions: [], distances: [], decays: [] },
  80. spot: { length: 0, colors: [], positions: [], distances: [], directions: [], anglesCos: [], exponents: [], decays: [] },
  81. hemi: { length: 0, skyColors: [], groundColors: [], positions: [] }
  82. },
  83. // info
  84. _infoMemory = {
  85. programs: 0,
  86. geometries: 0,
  87. textures: 0
  88. },
  89. _infoRender = {
  90. calls: 0,
  91. vertices: 0,
  92. faces: 0,
  93. points: 0
  94. };
  95. this.info = {
  96. render: _infoRender,
  97. memory: _infoMemory,
  98. programs: _programs
  99. };
  100. // initialize
  101. var _gl;
  102. try {
  103. var attributes = {
  104. alpha: _alpha,
  105. depth: _depth,
  106. stencil: _stencil,
  107. antialias: _antialias,
  108. premultipliedAlpha: _premultipliedAlpha,
  109. preserveDrawingBuffer: _preserveDrawingBuffer
  110. };
  111. _gl = _context || _canvas.getContext( 'webgl', attributes ) || _canvas.getContext( 'experimental-webgl', attributes );
  112. if ( _gl === null ) {
  113. if ( _canvas.getContext( 'webgl' ) !== null ) {
  114. throw 'Error creating WebGL context with your selected attributes.';
  115. } else {
  116. throw 'Error creating WebGL context.';
  117. }
  118. }
  119. _canvas.addEventListener( 'webglcontextlost', onContextLost, false );
  120. } catch ( error ) {
  121. console.error( 'THREE.WebGLRenderer: ' + error );
  122. }
  123. var extensions = new THREE.WebGLExtensions( _gl );
  124. extensions.get( 'OES_texture_float' );
  125. extensions.get( 'OES_texture_float_linear' );
  126. extensions.get( 'OES_texture_half_float' );
  127. extensions.get( 'OES_texture_half_float_linear' );
  128. extensions.get( 'OES_standard_derivatives' );
  129. extensions.get( 'ANGLE_instanced_arrays' );
  130. if ( extensions.get( 'OES_element_index_uint' ) ) {
  131. THREE.BufferGeometry.MaxIndex = 4294967296;
  132. }
  133. if ( _logarithmicDepthBuffer ) {
  134. extensions.get( 'EXT_frag_depth' );
  135. }
  136. var state = new THREE.WebGLState( _gl, extensions, paramThreeToGL );
  137. var properties = new THREE.WebGLProperties();
  138. var objects = new THREE.WebGLObjects( _gl, properties, this.info );
  139. var bufferRenderer = new THREE.WebGLBufferRenderer( _gl, extensions, _infoRender );
  140. var indexedBufferRenderer = new THREE.WebGLIndexedBufferRenderer( _gl, extensions, _infoRender );
  141. //
  142. function glClearColor( r, g, b, a ) {
  143. if ( _premultipliedAlpha === true ) {
  144. r *= a; g *= a; b *= a;
  145. }
  146. _gl.clearColor( r, g, b, a );
  147. }
  148. function setDefaultGLState() {
  149. state.init();
  150. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  151. glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  152. }
  153. function resetGLState() {
  154. _currentProgram = null;
  155. _currentCamera = null;
  156. _currentGeometryProgram = '';
  157. _currentMaterialId = - 1;
  158. _lightsNeedUpdate = true;
  159. state.reset();
  160. }
  161. setDefaultGLState();
  162. this.context = _gl;
  163. this.extensions = extensions;
  164. this.state = state;
  165. // shadow map
  166. var shadowMap = new THREE.WebGLShadowMap( this, lights, objects );
  167. this.shadowMap = shadowMap;
  168. // GPU capabilities
  169. var _maxTextures = _gl.getParameter( _gl.MAX_TEXTURE_IMAGE_UNITS );
  170. var _maxVertexTextures = _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
  171. var _maxTextureSize = _gl.getParameter( _gl.MAX_TEXTURE_SIZE );
  172. var _maxCubemapSize = _gl.getParameter( _gl.MAX_CUBE_MAP_TEXTURE_SIZE );
  173. var _supportsVertexTextures = _maxVertexTextures > 0;
  174. var _supportsBoneTextures = _supportsVertexTextures && extensions.get( 'OES_texture_float' );
  175. var _supportsInstancedArrays = extensions.get( 'ANGLE_instanced_arrays' );
  176. //
  177. //
  178. var _maxPrecision = state.getMaxPrecision( _precision );
  179. if ( _maxPrecision !== _precision ) {
  180. console.warn( 'THREE.WebGLRenderer:', _precision, 'not supported, using', _maxPrecision, 'instead.' );
  181. _precision = _maxPrecision;
  182. }
  183. // Plugins
  184. var spritePlugin = new THREE.SpritePlugin( this, sprites );
  185. var lensFlarePlugin = new THREE.LensFlarePlugin( this, lensFlares );
  186. // API
  187. this.getContext = function () {
  188. return _gl;
  189. };
  190. this.getContextAttributes = function () {
  191. return _gl.getContextAttributes();
  192. };
  193. this.forceContextLoss = function () {
  194. extensions.get( 'WEBGL_lose_context' ).loseContext();
  195. };
  196. this.supportsVertexTextures = function () {
  197. return _supportsVertexTextures;
  198. };
  199. this.supportsInstancedArrays = function () {
  200. return _supportsInstancedArrays;
  201. };
  202. this.getMaxAnisotropy = ( function () {
  203. var value;
  204. return function getMaxAnisotropy() {
  205. if ( value !== undefined ) return value;
  206. var extension = extensions.get( 'EXT_texture_filter_anisotropic' );
  207. if ( extension !== null ) {
  208. value = _gl.getParameter( extension.MAX_TEXTURE_MAX_ANISOTROPY_EXT );
  209. } else {
  210. value = 0;
  211. }
  212. return value;
  213. }
  214. } )();
  215. this.getPrecision = function () {
  216. return _precision;
  217. };
  218. this.getPixelRatio = function () {
  219. return pixelRatio;
  220. };
  221. this.setPixelRatio = function ( value ) {
  222. if ( value !== undefined ) pixelRatio = value;
  223. };
  224. this.getSize = function () {
  225. return {
  226. width: _width,
  227. height: _height
  228. };
  229. };
  230. this.setSize = function ( width, height, updateStyle ) {
  231. _width = width;
  232. _height = height;
  233. _canvas.width = width * pixelRatio;
  234. _canvas.height = height * pixelRatio;
  235. if ( updateStyle !== false ) {
  236. _canvas.style.width = width + 'px';
  237. _canvas.style.height = height + 'px';
  238. }
  239. this.setViewport( 0, 0, width, height );
  240. };
  241. this.setViewport = function ( x, y, width, height ) {
  242. _viewportX = x * pixelRatio;
  243. _viewportY = y * pixelRatio;
  244. _viewportWidth = width * pixelRatio;
  245. _viewportHeight = height * pixelRatio;
  246. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  247. };
  248. this.setScissor = function ( x, y, width, height ) {
  249. _gl.scissor(
  250. x * pixelRatio,
  251. y * pixelRatio,
  252. width * pixelRatio,
  253. height * pixelRatio
  254. );
  255. };
  256. this.enableScissorTest = function ( boolean ) {
  257. state.setScissorTest( boolean );
  258. };
  259. // Clearing
  260. this.getClearColor = function () {
  261. return _clearColor;
  262. };
  263. this.setClearColor = function ( color, alpha ) {
  264. _clearColor.set( color );
  265. _clearAlpha = alpha !== undefined ? alpha : 1;
  266. glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  267. };
  268. this.getClearAlpha = function () {
  269. return _clearAlpha;
  270. };
  271. this.setClearAlpha = function ( alpha ) {
  272. _clearAlpha = alpha;
  273. glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  274. };
  275. this.clear = function ( color, depth, stencil ) {
  276. var bits = 0;
  277. if ( color === undefined || color ) bits |= _gl.COLOR_BUFFER_BIT;
  278. if ( depth === undefined || depth ) bits |= _gl.DEPTH_BUFFER_BIT;
  279. if ( stencil === undefined || stencil ) bits |= _gl.STENCIL_BUFFER_BIT;
  280. _gl.clear( bits );
  281. };
  282. this.clearColor = function () {
  283. _gl.clear( _gl.COLOR_BUFFER_BIT );
  284. };
  285. this.clearDepth = function () {
  286. _gl.clear( _gl.DEPTH_BUFFER_BIT );
  287. };
  288. this.clearStencil = function () {
  289. _gl.clear( _gl.STENCIL_BUFFER_BIT );
  290. };
  291. this.clearTarget = function ( renderTarget, color, depth, stencil ) {
  292. this.setRenderTarget( renderTarget );
  293. this.clear( color, depth, stencil );
  294. };
  295. // Reset
  296. this.resetGLState = resetGLState;
  297. this.dispose = function() {
  298. _canvas.removeEventListener( 'webglcontextlost', onContextLost, false );
  299. };
  300. // Events
  301. function onContextLost( event ) {
  302. event.preventDefault();
  303. resetGLState();
  304. setDefaultGLState();
  305. objects.clear();
  306. properties.clear();
  307. };
  308. function onTextureDispose( event ) {
  309. var texture = event.target;
  310. texture.removeEventListener( 'dispose', onTextureDispose );
  311. deallocateTexture( texture );
  312. _infoMemory.textures --;
  313. }
  314. function onRenderTargetDispose( event ) {
  315. var renderTarget = event.target;
  316. renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );
  317. deallocateRenderTarget( renderTarget );
  318. _infoMemory.textures --;
  319. }
  320. function onMaterialDispose( event ) {
  321. var material = event.target;
  322. material.removeEventListener( 'dispose', onMaterialDispose );
  323. deallocateMaterial( material );
  324. }
  325. // Buffer deallocation
  326. function deallocateTexture( texture ) {
  327. var textureProperties = properties.get( texture );
  328. if ( texture.image && textureProperties.__image__webglTextureCube ) {
  329. // cube texture
  330. _gl.deleteTexture( textureProperties.__image__webglTextureCube );
  331. } else {
  332. // 2D texture
  333. if ( textureProperties.__webglInit === undefined ) return;
  334. _gl.deleteTexture( textureProperties.__webglTexture );
  335. }
  336. // remove all webgl properties
  337. properties.delete( texture );
  338. }
  339. function deallocateRenderTarget( renderTarget ) {
  340. var renderTargetProperties = properties.get( renderTarget );
  341. if ( ! renderTarget || renderTargetProperties.__webglTexture === undefined ) return;
  342. _gl.deleteTexture( renderTargetProperties.__webglTexture );
  343. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  344. for ( var i = 0; i < 6; i ++ ) {
  345. _gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ] );
  346. _gl.deleteRenderbuffer( renderTargetProperties.__webglRenderbuffer[ i ] );
  347. }
  348. } else {
  349. _gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer );
  350. _gl.deleteRenderbuffer( renderTargetProperties.__webglRenderbuffer );
  351. }
  352. properties.delete( renderTarget );
  353. }
  354. function deallocateMaterial( material ) {
  355. releaseMaterialProgramReference( material );
  356. properties.delete( material );
  357. }
  358. function releaseMaterialProgramReference( material ) {
  359. var program = properties.get( material ).program.program;
  360. if ( program === undefined ) return;
  361. material.program = undefined;
  362. for ( var i = 0, n = _programs.length; i !== n; ++ i ) {
  363. var programInfo = _programs[ i ];
  364. if ( programInfo.program === program ) {
  365. var newReferenceCount = -- programInfo.usedTimes;
  366. if ( newReferenceCount === 0 ) {
  367. // the last meterial that has been using the program let
  368. // go of it, so remove it from the (unordered) _programs
  369. // set and deallocate the GL resource
  370. var newLength = n - 1;
  371. _programs[ i ] = _programs[ newLength ];
  372. _programs.pop();
  373. _gl.deleteProgram( program );
  374. _infoMemory.programs = newLength;
  375. }
  376. break;
  377. }
  378. }
  379. }
  380. // Buffer rendering
  381. this.renderBufferImmediate = function ( object, program, material ) {
  382. state.initAttributes();
  383. var buffers = properties.get( object );
  384. if ( object.hasPositions && ! buffers.position ) buffers.position = _gl.createBuffer();
  385. if ( object.hasNormals && ! buffers.normal ) buffers.normal = _gl.createBuffer();
  386. if ( object.hasUvs && ! buffers.uv ) buffers.uv = _gl.createBuffer();
  387. if ( object.hasColors && ! buffers.color ) buffers.color = _gl.createBuffer();
  388. var attributes = program.getAttributes();
  389. if ( object.hasPositions ) {
  390. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.position );
  391. _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
  392. state.enableAttribute( attributes.position );
  393. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  394. }
  395. if ( object.hasNormals ) {
  396. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.normal );
  397. if ( material.type !== 'MeshPhongMaterial' && material.shading === THREE.FlatShading ) {
  398. for ( var i = 0, l = object.count * 3; i < l; i += 9 ) {
  399. var array = object.normalArray;
  400. var nx = ( array[ i + 0 ] + array[ i + 3 ] + array[ i + 6 ] ) / 3;
  401. var ny = ( array[ i + 1 ] + array[ i + 4 ] + array[ i + 7 ] ) / 3;
  402. var nz = ( array[ i + 2 ] + array[ i + 5 ] + array[ i + 8 ] ) / 3;
  403. array[ i + 0 ] = nx;
  404. array[ i + 1 ] = ny;
  405. array[ i + 2 ] = nz;
  406. array[ i + 3 ] = nx;
  407. array[ i + 4 ] = ny;
  408. array[ i + 5 ] = nz;
  409. array[ i + 6 ] = nx;
  410. array[ i + 7 ] = ny;
  411. array[ i + 8 ] = nz;
  412. }
  413. }
  414. _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
  415. state.enableAttribute( attributes.normal );
  416. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  417. }
  418. if ( object.hasUvs && material.map ) {
  419. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv );
  420. _gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
  421. state.enableAttribute( attributes.uv );
  422. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  423. }
  424. if ( object.hasColors && material.vertexColors !== THREE.NoColors ) {
  425. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color );
  426. _gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
  427. state.enableAttribute( attributes.color );
  428. _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  429. }
  430. state.disableUnusedAttributes();
  431. _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
  432. object.count = 0;
  433. };
  434. this.renderBufferDirect = function ( camera, lights, fog, geometry, material, object ) {
  435. setMaterial( material );
  436. var program = setProgram( camera, lights, fog, material, object );
  437. var updateBuffers = false;
  438. var geometryProgram = geometry.id + '_' + program.id + '_' + material.wireframe;
  439. if ( geometryProgram !== _currentGeometryProgram ) {
  440. _currentGeometryProgram = geometryProgram;
  441. updateBuffers = true;
  442. }
  443. // morph targets
  444. var morphTargetInfluences = object.morphTargetInfluences;
  445. if ( morphTargetInfluences !== undefined ) {
  446. var activeInfluences = [];
  447. for ( var i = 0, l = morphTargetInfluences.length; i < l; i ++ ) {
  448. var influence = morphTargetInfluences[ i ];
  449. activeInfluences.push( [ influence, i ] );
  450. }
  451. activeInfluences.sort( numericalSort );
  452. if ( activeInfluences.length > 8 ) {
  453. activeInfluences.length = 8;
  454. }
  455. var morphAttributes = geometry.morphAttributes;
  456. for ( var i = 0, l = activeInfluences.length; i < l; i ++ ) {
  457. var influence = activeInfluences[ i ];
  458. morphInfluences[ i ] = influence[ 0 ];
  459. if ( influence[ 0 ] !== 0 ) {
  460. var index = influence[ 1 ];
  461. if ( material.morphTargets === true ) geometry.addAttribute( 'morphTarget' + i, morphAttributes.position[ index ] );
  462. if ( material.morphNormals === true ) geometry.addAttribute( 'morphNormal' + i, morphAttributes.normal[ index ] );
  463. } else {
  464. if ( material.morphTargets === true ) geometry.removeAttribute( 'morphTarget' + i );
  465. if ( material.morphNormals === true ) geometry.removeAttribute( 'morphNormal' + i );
  466. }
  467. }
  468. var uniforms = program.getUniforms();
  469. if ( uniforms.morphTargetInfluences !== null ) {
  470. _gl.uniform1fv( uniforms.morphTargetInfluences, morphInfluences );
  471. }
  472. updateBuffers = true;
  473. }
  474. //
  475. var index = geometry.attributes.index;
  476. var position = geometry.attributes.position;
  477. if ( material.wireframe === true ) {
  478. index = objects.getWireframeAttribute( geometry );
  479. }
  480. var groups = geometry.drawcalls;
  481. var renderer;
  482. if ( index !== undefined ) {
  483. renderer = indexedBufferRenderer;
  484. renderer.setIndex( index );
  485. } else {
  486. renderer = bufferRenderer;
  487. }
  488. if ( updateBuffers ) {
  489. setupVertexAttributes( material, program, geometry );
  490. if ( index !== undefined ) {
  491. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, objects.getAttributeBuffer( index ) );
  492. }
  493. }
  494. if ( groups.length === 0 ) {
  495. var count;
  496. if ( index !== undefined ) {
  497. count = index.array.length;
  498. } else if ( position instanceof THREE.InterleavedBufferAttribute ) {
  499. count = position.data.array.length / 3;
  500. } else {
  501. count = position.array.length / 3;
  502. }
  503. groups = [ {
  504. start: 0,
  505. count: count
  506. } ];
  507. }
  508. if ( object instanceof THREE.Mesh ) {
  509. if ( material.wireframe === true ) {
  510. state.setLineWidth( material.wireframeLinewidth * pixelRatio );
  511. renderer.setMode( _gl.LINES );
  512. } else {
  513. renderer.setMode( _gl.TRIANGLES );
  514. }
  515. if ( geometry instanceof THREE.InstancedBufferGeometry && geometry.maxInstancedCount > 0 ) {
  516. renderer.renderInstances( geometry );
  517. } else if ( position instanceof THREE.InterleavedBufferAttribute ) {
  518. renderer.render( 0, position.data.count );
  519. } else {
  520. renderer.renderGroups( groups );
  521. }
  522. } else if ( object instanceof THREE.Line ) {
  523. var lineWidth = material.linewidth;
  524. if ( lineWidth === undefined ) lineWidth = 1; // Not using Line*Material
  525. state.setLineWidth( lineWidth * pixelRatio );
  526. if ( object instanceof THREE.LineSegments ) {
  527. renderer.setMode( _gl.LINES );
  528. } else {
  529. renderer.setMode( _gl.LINE_STRIP );
  530. }
  531. renderer.renderGroups( groups );
  532. } else if ( object instanceof THREE.PointCloud ) {
  533. renderer.setMode( _gl.POINTS );
  534. renderer.renderGroups( groups );
  535. }
  536. };
  537. function setupVertexAttributes( material, program, geometry, startIndex ) {
  538. var extension;
  539. if ( geometry instanceof THREE.InstancedBufferGeometry ) {
  540. extension = extensions.get( 'ANGLE_instanced_arrays' );
  541. if ( extension === null ) {
  542. console.error( 'THREE.WebGLRenderer.setupVertexAttributes: using THREE.InstancedBufferGeometry but hardware does not support extension ANGLE_instanced_arrays.' );
  543. return;
  544. }
  545. }
  546. if ( startIndex === undefined ) startIndex = 0;
  547. state.initAttributes();
  548. var geometryAttributes = geometry.attributes;
  549. var programAttributes = program.getAttributes();
  550. var materialDefaultAttributeValues = material.defaultAttributeValues;
  551. for ( var name in programAttributes ) {
  552. var programAttribute = programAttributes[ name ];
  553. if ( programAttribute >= 0 ) {
  554. var geometryAttribute = geometryAttributes[ name ];
  555. if ( geometryAttribute !== undefined ) {
  556. state.enableAttribute( programAttribute );
  557. var size = geometryAttribute.itemSize;
  558. var buffer = objects.getAttributeBuffer( geometryAttribute );
  559. if ( geometryAttribute instanceof THREE.InterleavedBufferAttribute ) {
  560. var data = geometryAttribute.data;
  561. var stride = data.stride;
  562. var offset = geometryAttribute.offset;
  563. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
  564. _gl.vertexAttribPointer( programAttribute, size, _gl.FLOAT, false, stride * data.array.BYTES_PER_ELEMENT, ( startIndex * stride + offset ) * data.array.BYTES_PER_ELEMENT );
  565. if ( data instanceof THREE.InstancedInterleavedBuffer ) {
  566. if ( extension === null ) {
  567. console.error( 'THREE.WebGLRenderer.setupVertexAttributes: using THREE.InstancedBufferAttribute but hardware does not support extension ANGLE_instanced_arrays.' );
  568. return;
  569. }
  570. extension.vertexAttribDivisorANGLE( programAttribute, data.meshPerAttribute );
  571. if ( geometry.maxInstancedCount === undefined ) {
  572. geometry.maxInstancedCount = data.meshPerAttribute * data.count;
  573. }
  574. }
  575. } else {
  576. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
  577. _gl.vertexAttribPointer( programAttribute, size, _gl.FLOAT, false, 0, startIndex * size * 4 ); // 4 bytes per Float32
  578. if ( geometryAttribute instanceof THREE.InstancedBufferAttribute ) {
  579. if ( extension === null ) {
  580. console.error( 'THREE.WebGLRenderer.setupVertexAttributes: using THREE.InstancedBufferAttribute but hardware does not support extension ANGLE_instanced_arrays.' );
  581. return;
  582. }
  583. extension.vertexAttribDivisorANGLE( programAttribute, geometryAttribute.meshPerAttribute );
  584. if ( geometry.maxInstancedCount === undefined ) {
  585. geometry.maxInstancedCount = geometryAttribute.meshPerAttribute * geometryAttribute.count;
  586. }
  587. }
  588. }
  589. } else if ( materialDefaultAttributeValues !== undefined ) {
  590. var value = materialDefaultAttributeValues[ name ];
  591. if ( value !== undefined ) {
  592. switch ( value.length ) {
  593. case 2:
  594. _gl.vertexAttrib2fv( programAttribute, value );
  595. break;
  596. case 3:
  597. _gl.vertexAttrib3fv( programAttribute, value );
  598. break;
  599. case 4:
  600. _gl.vertexAttrib4fv( programAttribute, value );
  601. break;
  602. default:
  603. _gl.vertexAttrib1fv( programAttribute, value );
  604. }
  605. }
  606. }
  607. }
  608. }
  609. state.disableUnusedAttributes();
  610. }
  611. // Sorting
  612. function numericalSort ( a, b ) {
  613. return b[ 0 ] - a[ 0 ];
  614. }
  615. function painterSortStable ( a, b ) {
  616. if ( a.object.renderOrder !== b.object.renderOrder ) {
  617. return a.object.renderOrder - b.object.renderOrder;
  618. } else if ( a.object.material.id !== b.object.material.id ) {
  619. return a.object.material.id - b.object.material.id;
  620. } else if ( a.z !== b.z ) {
  621. return a.z - b.z;
  622. } else {
  623. return a.id - b.id;
  624. }
  625. }
  626. function reversePainterSortStable ( a, b ) {
  627. if ( a.object.renderOrder !== b.object.renderOrder ) {
  628. return a.object.renderOrder - b.object.renderOrder;
  629. } if ( a.z !== b.z ) {
  630. return b.z - a.z;
  631. } else {
  632. return a.id - b.id;
  633. }
  634. }
  635. // Rendering
  636. this.render = function ( scene, camera, renderTarget, forceClear ) {
  637. if ( camera instanceof THREE.Camera === false ) {
  638. console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
  639. return;
  640. }
  641. var fog = scene.fog;
  642. // reset caching for this frame
  643. _currentGeometryProgram = '';
  644. _currentMaterialId = - 1;
  645. _currentCamera = null;
  646. _lightsNeedUpdate = true;
  647. // update scene graph
  648. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  649. // update camera matrices and frustum
  650. if ( camera.parent === null ) camera.updateMatrixWorld();
  651. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  652. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  653. _frustum.setFromMatrix( _projScreenMatrix );
  654. lights.length = 0;
  655. opaqueObjects.length = 0;
  656. transparentObjects.length = 0;
  657. opaqueImmediateObjects.length = 0;
  658. transparentImmediateObjects.length = 0;
  659. sprites.length = 0;
  660. lensFlares.length = 0;
  661. projectObject( scene );
  662. if ( _this.sortObjects === true ) {
  663. opaqueObjects.sort( painterSortStable );
  664. transparentObjects.sort( reversePainterSortStable );
  665. }
  666. //
  667. shadowMap.render( scene, camera );
  668. //
  669. _infoRender.calls = 0;
  670. _infoRender.vertices = 0;
  671. _infoRender.faces = 0;
  672. _infoRender.points = 0;
  673. this.setRenderTarget( renderTarget );
  674. if ( this.autoClear || forceClear ) {
  675. this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );
  676. }
  677. //
  678. if ( scene.overrideMaterial ) {
  679. var overrideMaterial = scene.overrideMaterial;
  680. renderObjects( opaqueObjects, camera, lights, fog, overrideMaterial );
  681. renderObjects( transparentObjects, camera, lights, fog, overrideMaterial );
  682. renderObjectsImmediate( opaqueImmediateObjects, camera, lights, fog, overrideMaterial );
  683. renderObjectsImmediate( transparentImmediateObjects, camera, lights, fog, overrideMaterial );
  684. } else {
  685. // opaque pass (front-to-back order)
  686. state.setBlending( THREE.NoBlending );
  687. renderObjects( opaqueObjects, camera, lights, fog );
  688. renderObjectsImmediate( opaqueImmediateObjects, camera, lights, fog );
  689. // transparent pass (back-to-front order)
  690. renderObjects( transparentObjects, camera, lights, fog );
  691. renderObjectsImmediate( transparentImmediateObjects, camera, lights, fog );
  692. }
  693. // custom render plugins (post pass)
  694. spritePlugin.render( scene, camera );
  695. lensFlarePlugin.render( scene, camera, _currentWidth, _currentHeight );
  696. // Generate mipmap if we're using any kind of mipmap filtering
  697. if ( renderTarget && renderTarget.generateMipmaps && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
  698. updateRenderTargetMipmap( renderTarget );
  699. }
  700. // Ensure depth buffer writing is enabled so it can be cleared on next render
  701. state.setDepthTest( true );
  702. state.setDepthWrite( true );
  703. state.setColorWrite( true );
  704. // _gl.finish();
  705. };
  706. function projectObject( object ) {
  707. if ( object.visible === false ) return;
  708. if ( object instanceof THREE.Scene || object instanceof THREE.Group ) {
  709. // skip
  710. } else {
  711. // update Skeleton objects
  712. if ( object instanceof THREE.SkinnedMesh ) {
  713. object.skeleton.update();
  714. }
  715. objects.init( object );
  716. if ( object instanceof THREE.Light ) {
  717. lights.push( object );
  718. } else if ( object instanceof THREE.Sprite ) {
  719. sprites.push( object );
  720. } else if ( object instanceof THREE.LensFlare ) {
  721. lensFlares.push( object );
  722. } else if ( object instanceof THREE.ImmediateRenderObject ) {
  723. var material = object.material;
  724. if ( material.transparent ) {
  725. transparentImmediateObjects.push( object );
  726. } else {
  727. opaqueImmediateObjects.push( object );
  728. }
  729. } else {
  730. var webglObject = objects.objects[ object.id ];
  731. if ( webglObject && ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) ) {
  732. var material = object.material;
  733. if ( material !== null && material.visible === true ) {
  734. if ( material instanceof THREE.MeshFaceMaterial ) {
  735. for ( var i = 0, l = material.materials.length; i < l; i ++ ) {
  736. if ( properties.get( material.materials[ i ] ) ) {
  737. material.materials[ i ].program = properties.get( material.materials[ i ] ).program;
  738. }
  739. }
  740. }
  741. else {
  742. if ( properties.get( material ) ) {
  743. material.program = properties.get( material ).program;
  744. }
  745. }
  746. if ( material.transparent ) {
  747. transparentObjects.push( webglObject );
  748. } else {
  749. opaqueObjects.push( webglObject );
  750. }
  751. if ( _this.sortObjects === true ) {
  752. _vector3.setFromMatrixPosition( object.matrixWorld );
  753. _vector3.applyProjection( _projScreenMatrix );
  754. webglObject.z = _vector3.z;
  755. }
  756. }
  757. }
  758. }
  759. }
  760. var children = object.children;
  761. for ( var i = 0, l = children.length; i < l; i ++ ) {
  762. projectObject( children[ i ] );
  763. }
  764. }
  765. function renderObjects( renderList, camera, lights, fog, overrideMaterial ) {
  766. var material = overrideMaterial;
  767. for ( var i = 0, l = renderList.length; i < l; i ++ ) {
  768. var webglObject = renderList[ i ];
  769. var object = webglObject.object;
  770. var geometry = objects.update( object );
  771. object._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  772. object._normalMatrix.getNormalMatrix( object._modelViewMatrix );
  773. if ( overrideMaterial === undefined ) material = object.material;
  774. if ( material instanceof THREE.MeshFaceMaterial ) {
  775. var materials = material.materials;
  776. for ( var j = 0, jl = materials.length; j < jl; j ++ ) {
  777. material = materials[ j ];
  778. if ( material.visible ) {
  779. _this.renderBufferDirect( camera, lights, fog, geometry, material, object );
  780. }
  781. }
  782. } else {
  783. _this.renderBufferDirect( camera, lights, fog, geometry, material, object );
  784. }
  785. }
  786. }
  787. function renderObjectsImmediate( renderList, camera, lights, fog, overrideMaterial ) {
  788. var material = overrideMaterial;
  789. for ( var i = 0, l = renderList.length; i < l; i ++ ) {
  790. var object = renderList[ i ];
  791. object._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  792. object._normalMatrix.getNormalMatrix( object._modelViewMatrix );
  793. if ( overrideMaterial === undefined ) material = object.material;
  794. setMaterial( material );
  795. var program = setProgram( camera, lights, fog, material, object );
  796. _currentGeometryProgram = '';
  797. object.render( function ( object ) {
  798. _this.renderBufferImmediate( object, program, material );
  799. } );
  800. }
  801. }
  802. // Materials
  803. var shaderIDs = {
  804. MeshDepthMaterial: 'depth',
  805. MeshNormalMaterial: 'normal',
  806. MeshBasicMaterial: 'basic',
  807. MeshLambertMaterial: 'lambert',
  808. MeshPhongMaterial: 'phong',
  809. LineBasicMaterial: 'basic',
  810. LineDashedMaterial: 'dashed',
  811. PointCloudMaterial: 'particle_basic'
  812. };
  813. function initMaterial( material, lights, fog, object ) {
  814. var materialProperties = properties.get( material );
  815. var shaderID = shaderIDs[ material.type ];
  816. // heuristics to create shader parameters according to lights in the scene
  817. // (not to blow over maxLights budget)
  818. var maxLightCount = allocateLights( lights );
  819. var maxShadows = allocateShadows( lights );
  820. var maxBones = allocateBones( object );
  821. var precision = _precision;
  822. if ( material.precision !== null ) {
  823. precision = state.getMaxPrecision( material.precision );
  824. if ( precision !== material.precision ) {
  825. console.warn( 'THREE.WebGLRenderer.initMaterial:', material.precision, 'not supported, using', precision, 'instead.' );
  826. }
  827. }
  828. var parameters = {
  829. precision: precision,
  830. supportsVertexTextures: _supportsVertexTextures,
  831. map: !! material.map,
  832. envMap: !! material.envMap,
  833. envMapMode: material.envMap && material.envMap.mapping,
  834. lightMap: !! material.lightMap,
  835. aoMap: !! material.aoMap,
  836. emissiveMap: !! material.emissiveMap,
  837. bumpMap: !! material.bumpMap,
  838. normalMap: !! material.normalMap,
  839. specularMap: !! material.specularMap,
  840. alphaMap: !! material.alphaMap,
  841. combine: material.combine,
  842. vertexColors: material.vertexColors,
  843. fog: fog,
  844. useFog: material.fog,
  845. fogExp: fog instanceof THREE.FogExp2,
  846. flatShading: material.shading === THREE.FlatShading,
  847. sizeAttenuation: material.sizeAttenuation,
  848. logarithmicDepthBuffer: _logarithmicDepthBuffer,
  849. skinning: material.skinning,
  850. maxBones: maxBones,
  851. useVertexTexture: _supportsBoneTextures && object && object.skeleton && object.skeleton.useVertexTexture,
  852. morphTargets: material.morphTargets,
  853. morphNormals: material.morphNormals,
  854. maxMorphTargets: _this.maxMorphTargets,
  855. maxMorphNormals: _this.maxMorphNormals,
  856. maxDirLights: maxLightCount.directional,
  857. maxPointLights: maxLightCount.point,
  858. maxSpotLights: maxLightCount.spot,
  859. maxHemiLights: maxLightCount.hemi,
  860. maxShadows: maxShadows,
  861. shadowMapEnabled: shadowMap.enabled && object.receiveShadow && maxShadows > 0,
  862. shadowMapType: shadowMap.type,
  863. shadowMapDebug: shadowMap.debug,
  864. alphaTest: material.alphaTest,
  865. metal: material.metal,
  866. doubleSided: material.side === THREE.DoubleSide,
  867. flipSided: material.side === THREE.BackSide
  868. };
  869. // Generate code
  870. var chunks = [];
  871. if ( shaderID ) {
  872. chunks.push( shaderID );
  873. } else {
  874. chunks.push( material.fragmentShader );
  875. chunks.push( material.vertexShader );
  876. }
  877. if ( material.defines !== undefined ) {
  878. for ( var name in material.defines ) {
  879. chunks.push( name );
  880. chunks.push( material.defines[ name ] );
  881. }
  882. }
  883. for ( var name in parameters ) {
  884. chunks.push( name );
  885. chunks.push( parameters[ name ] );
  886. }
  887. var code = chunks.join();
  888. var programChange = true;
  889. if ( ! materialProperties.program ) {
  890. // new material
  891. material.addEventListener( 'dispose', onMaterialDispose );
  892. } else if ( materialProperties.program.code !== code ) {
  893. // changed glsl or parameters
  894. releaseMaterialProgramReference( material );
  895. } else if ( shaderID !== undefined ) {
  896. // same glsl and uniform list
  897. return;
  898. } else {
  899. // only rebuild uniform list
  900. programChange = false;
  901. }
  902. if ( shaderID ) {
  903. var shader = THREE.ShaderLib[ shaderID ];
  904. materialProperties.__webglShader = {
  905. name: material.type,
  906. uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
  907. vertexShader: shader.vertexShader,
  908. fragmentShader: shader.fragmentShader
  909. };
  910. } else {
  911. materialProperties.__webglShader = {
  912. name: material.type,
  913. uniforms: material.uniforms,
  914. vertexShader: material.vertexShader,
  915. fragmentShader: material.fragmentShader
  916. };
  917. }
  918. var program;
  919. // Check if code has been already compiled
  920. for ( var p = 0, pl = _programs.length; p < pl; p ++ ) {
  921. var programInfo = _programs[ p ];
  922. if ( programInfo.code === code ) {
  923. program = programInfo;
  924. if ( programChange ) {
  925. program.usedTimes ++;
  926. }
  927. break;
  928. }
  929. }
  930. if ( program === undefined ) {
  931. material.__webglShader = materialProperties.__webglShader;
  932. program = new THREE.WebGLProgram( _this, code, material, parameters );
  933. _programs.push( program );
  934. _infoMemory.programs = _programs.length;
  935. }
  936. materialProperties.program = program;
  937. var attributes = program.getAttributes();
  938. if ( material.morphTargets ) {
  939. material.numSupportedMorphTargets = 0;
  940. for ( var i = 0; i < _this.maxMorphTargets; i ++ ) {
  941. if ( attributes[ 'morphTarget' + i ] >= 0 ) {
  942. material.numSupportedMorphTargets ++;
  943. }
  944. }
  945. }
  946. if ( material.morphNormals ) {
  947. material.numSupportedMorphNormals = 0;
  948. for ( i = 0; i < _this.maxMorphNormals; i ++ ) {
  949. if ( attributes[ 'morphNormal' + i ] >= 0 ) {
  950. material.numSupportedMorphNormals ++;
  951. }
  952. }
  953. }
  954. materialProperties.uniformsList = [];
  955. var uniformLocations = materialProperties.program.getUniforms();
  956. for ( var u in materialProperties.__webglShader.uniforms ) {
  957. var location = uniformLocations[ u ];
  958. if ( location ) {
  959. materialProperties.uniformsList.push( [ materialProperties.__webglShader.uniforms[ u ], location ] );
  960. }
  961. }
  962. }
  963. function setMaterial( material ) {
  964. setMaterialFaces( material );
  965. if ( material.transparent === true ) {
  966. state.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst, material.blendEquationAlpha, material.blendSrcAlpha, material.blendDstAlpha );
  967. } else {
  968. state.setBlending( THREE.NoBlending );
  969. }
  970. state.setDepthFunc( material.depthFunc );
  971. state.setDepthTest( material.depthTest );
  972. state.setDepthWrite( material.depthWrite );
  973. state.setColorWrite( material.colorWrite );
  974. state.setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  975. }
  976. function setMaterialFaces( material ) {
  977. material.side !== THREE.DoubleSide ? state.enable( _gl.CULL_FACE ) : state.disable( _gl.CULL_FACE );
  978. state.setFlipSided( material.side === THREE.BackSide );
  979. }
  980. function setProgram( camera, lights, fog, material, object ) {
  981. _usedTextureUnits = 0;
  982. var materialProperties = properties.get( material );
  983. if ( material.needsUpdate || ! materialProperties.program ) {
  984. initMaterial( material, lights, fog, object );
  985. material.needsUpdate = false;
  986. }
  987. var refreshProgram = false;
  988. var refreshMaterial = false;
  989. var refreshLights = false;
  990. var program = materialProperties.program,
  991. p_uniforms = program.getUniforms(),
  992. m_uniforms = materialProperties.__webglShader.uniforms;
  993. if ( program.id !== _currentProgram ) {
  994. _gl.useProgram( program.program );
  995. _currentProgram = program.id;
  996. refreshProgram = true;
  997. refreshMaterial = true;
  998. refreshLights = true;
  999. }
  1000. if ( material.id !== _currentMaterialId ) {
  1001. if ( _currentMaterialId === - 1 ) refreshLights = true;
  1002. _currentMaterialId = material.id;
  1003. refreshMaterial = true;
  1004. }
  1005. if ( refreshProgram || camera !== _currentCamera ) {
  1006. _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, camera.projectionMatrix.elements );
  1007. if ( _logarithmicDepthBuffer ) {
  1008. _gl.uniform1f( p_uniforms.logDepthBufFC, 2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
  1009. }
  1010. if ( camera !== _currentCamera ) _currentCamera = camera;
  1011. // load material specific uniforms
  1012. // (shader material also gets them for the sake of genericity)
  1013. if ( material instanceof THREE.ShaderMaterial ||
  1014. material instanceof THREE.MeshPhongMaterial ||
  1015. material.envMap ) {
  1016. if ( p_uniforms.cameraPosition !== undefined ) {
  1017. _vector3.setFromMatrixPosition( camera.matrixWorld );
  1018. _gl.uniform3f( p_uniforms.cameraPosition, _vector3.x, _vector3.y, _vector3.z );
  1019. }
  1020. }
  1021. if ( material instanceof THREE.MeshPhongMaterial ||
  1022. material instanceof THREE.MeshLambertMaterial ||
  1023. material instanceof THREE.MeshBasicMaterial ||
  1024. material instanceof THREE.ShaderMaterial ||
  1025. material.skinning ) {
  1026. if ( p_uniforms.viewMatrix !== undefined ) {
  1027. _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, camera.matrixWorldInverse.elements );
  1028. }
  1029. }
  1030. }
  1031. // skinning uniforms must be set even if material didn't change
  1032. // auto-setting of texture unit for bone texture must go before other textures
  1033. // not sure why, but otherwise weird things happen
  1034. if ( material.skinning ) {
  1035. if ( object.bindMatrix && p_uniforms.bindMatrix !== undefined ) {
  1036. _gl.uniformMatrix4fv( p_uniforms.bindMatrix, false, object.bindMatrix.elements );
  1037. }
  1038. if ( object.bindMatrixInverse && p_uniforms.bindMatrixInverse !== undefined ) {
  1039. _gl.uniformMatrix4fv( p_uniforms.bindMatrixInverse, false, object.bindMatrixInverse.elements );
  1040. }
  1041. if ( _supportsBoneTextures && object.skeleton && object.skeleton.useVertexTexture ) {
  1042. if ( p_uniforms.boneTexture !== undefined ) {
  1043. var textureUnit = getTextureUnit();
  1044. _gl.uniform1i( p_uniforms.boneTexture, textureUnit );
  1045. _this.setTexture( object.skeleton.boneTexture, textureUnit );
  1046. }
  1047. if ( p_uniforms.boneTextureWidth !== undefined ) {
  1048. _gl.uniform1i( p_uniforms.boneTextureWidth, object.skeleton.boneTextureWidth );
  1049. }
  1050. if ( p_uniforms.boneTextureHeight !== undefined ) {
  1051. _gl.uniform1i( p_uniforms.boneTextureHeight, object.skeleton.boneTextureHeight );
  1052. }
  1053. } else if ( object.skeleton && object.skeleton.boneMatrices ) {
  1054. if ( p_uniforms.boneGlobalMatrices !== undefined ) {
  1055. _gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.skeleton.boneMatrices );
  1056. }
  1057. }
  1058. }
  1059. if ( refreshMaterial ) {
  1060. // refresh uniforms common to several materials
  1061. if ( fog && material.fog ) {
  1062. refreshUniformsFog( m_uniforms, fog );
  1063. }
  1064. if ( material instanceof THREE.MeshPhongMaterial ||
  1065. material instanceof THREE.MeshLambertMaterial ||
  1066. material.lights ) {
  1067. if ( _lightsNeedUpdate ) {
  1068. refreshLights = true;
  1069. setupLights( lights );
  1070. _lightsNeedUpdate = false;
  1071. }
  1072. if ( refreshLights ) {
  1073. refreshUniformsLights( m_uniforms, _lights );
  1074. markUniformsLightsNeedsUpdate( m_uniforms, true );
  1075. } else {
  1076. markUniformsLightsNeedsUpdate( m_uniforms, false );
  1077. }
  1078. }
  1079. if ( material instanceof THREE.MeshBasicMaterial ||
  1080. material instanceof THREE.MeshLambertMaterial ||
  1081. material instanceof THREE.MeshPhongMaterial ) {
  1082. refreshUniformsCommon( m_uniforms, material );
  1083. }
  1084. // refresh single material specific uniforms
  1085. if ( material instanceof THREE.LineBasicMaterial ) {
  1086. refreshUniformsLine( m_uniforms, material );
  1087. } else if ( material instanceof THREE.LineDashedMaterial ) {
  1088. refreshUniformsLine( m_uniforms, material );
  1089. refreshUniformsDash( m_uniforms, material );
  1090. } else if ( material instanceof THREE.PointCloudMaterial ) {
  1091. refreshUniformsParticle( m_uniforms, material );
  1092. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  1093. refreshUniformsPhong( m_uniforms, material );
  1094. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  1095. refreshUniformsLambert( m_uniforms, material );
  1096. } else if ( material instanceof THREE.MeshBasicMaterial ) {
  1097. refreshUniformsBasic( m_uniforms, material );
  1098. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  1099. m_uniforms.mNear.value = camera.near;
  1100. m_uniforms.mFar.value = camera.far;
  1101. m_uniforms.opacity.value = material.opacity;
  1102. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  1103. m_uniforms.opacity.value = material.opacity;
  1104. }
  1105. if ( object.receiveShadow && ! material._shadowPass ) {
  1106. refreshUniformsShadow( m_uniforms, lights );
  1107. }
  1108. // load common uniforms
  1109. loadUniformsGeneric( materialProperties.uniformsList );
  1110. }
  1111. loadUniformsMatrices( p_uniforms, object );
  1112. if ( p_uniforms.modelMatrix !== undefined ) {
  1113. _gl.uniformMatrix4fv( p_uniforms.modelMatrix, false, object.matrixWorld.elements );
  1114. }
  1115. return program;
  1116. }
  1117. // Uniforms (refresh uniforms objects)
  1118. function refreshUniformsCommon ( uniforms, material ) {
  1119. uniforms.opacity.value = material.opacity;
  1120. uniforms.diffuse.value = material.color;
  1121. uniforms.map.value = material.map;
  1122. uniforms.specularMap.value = material.specularMap;
  1123. uniforms.alphaMap.value = material.alphaMap;
  1124. if ( material.bumpMap ) {
  1125. uniforms.bumpMap.value = material.bumpMap;
  1126. uniforms.bumpScale.value = material.bumpScale;
  1127. }
  1128. if ( material.normalMap ) {
  1129. uniforms.normalMap.value = material.normalMap;
  1130. uniforms.normalScale.value.copy( material.normalScale );
  1131. }
  1132. // uv repeat and offset setting priorities
  1133. // 1. color map
  1134. // 2. specular map
  1135. // 3. normal map
  1136. // 4. bump map
  1137. // 5. alpha map
  1138. // 6. emissive map
  1139. var uvScaleMap;
  1140. if ( material.map ) {
  1141. uvScaleMap = material.map;
  1142. } else if ( material.specularMap ) {
  1143. uvScaleMap = material.specularMap;
  1144. } else if ( material.normalMap ) {
  1145. uvScaleMap = material.normalMap;
  1146. } else if ( material.bumpMap ) {
  1147. uvScaleMap = material.bumpMap;
  1148. } else if ( material.alphaMap ) {
  1149. uvScaleMap = material.alphaMap;
  1150. } else if ( material.emissiveMap ) {
  1151. uvScaleMap = material.emissiveMap;
  1152. }
  1153. if ( uvScaleMap !== undefined ) {
  1154. var offset = uvScaleMap.offset;
  1155. var repeat = uvScaleMap.repeat;
  1156. uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
  1157. }
  1158. uniforms.envMap.value = material.envMap;
  1159. uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : - 1;
  1160. uniforms.reflectivity.value = material.reflectivity;
  1161. uniforms.refractionRatio.value = material.refractionRatio;
  1162. }
  1163. function refreshUniformsLine ( uniforms, material ) {
  1164. uniforms.diffuse.value = material.color;
  1165. uniforms.opacity.value = material.opacity;
  1166. }
  1167. function refreshUniformsDash ( uniforms, material ) {
  1168. uniforms.dashSize.value = material.dashSize;
  1169. uniforms.totalSize.value = material.dashSize + material.gapSize;
  1170. uniforms.scale.value = material.scale;
  1171. }
  1172. function refreshUniformsParticle ( uniforms, material ) {
  1173. uniforms.psColor.value = material.color;
  1174. uniforms.opacity.value = material.opacity;
  1175. uniforms.size.value = material.size;
  1176. uniforms.scale.value = _canvas.height / 2.0; // TODO: Cache this.
  1177. uniforms.map.value = material.map;
  1178. if ( material.map !== null ) {
  1179. var offset = material.map.offset;
  1180. var repeat = material.map.repeat;
  1181. uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
  1182. }
  1183. }
  1184. function refreshUniformsFog ( uniforms, fog ) {
  1185. uniforms.fogColor.value = fog.color;
  1186. if ( fog instanceof THREE.Fog ) {
  1187. uniforms.fogNear.value = fog.near;
  1188. uniforms.fogFar.value = fog.far;
  1189. } else if ( fog instanceof THREE.FogExp2 ) {
  1190. uniforms.fogDensity.value = fog.density;
  1191. }
  1192. }
  1193. function refreshUniformsPhong ( uniforms, material ) {
  1194. uniforms.shininess.value = material.shininess;
  1195. uniforms.emissive.value = material.emissive;
  1196. uniforms.specular.value = material.specular;
  1197. uniforms.lightMap.value = material.lightMap;
  1198. uniforms.lightMapIntensity.value = material.lightMapIntensity;
  1199. uniforms.aoMap.value = material.aoMap;
  1200. uniforms.aoMapIntensity.value = material.aoMapIntensity;
  1201. uniforms.emissiveMap.value = material.emissiveMap;
  1202. }
  1203. function refreshUniformsLambert ( uniforms, material ) {
  1204. uniforms.emissive.value = material.emissive;
  1205. }
  1206. function refreshUniformsBasic ( uniforms, material ) {
  1207. uniforms.aoMap.value = material.aoMap;
  1208. uniforms.aoMapIntensity.value = material.aoMapIntensity;
  1209. }
  1210. function refreshUniformsLights ( uniforms, lights ) {
  1211. uniforms.ambientLightColor.value = lights.ambient;
  1212. uniforms.directionalLightColor.value = lights.directional.colors;
  1213. uniforms.directionalLightDirection.value = lights.directional.positions;
  1214. uniforms.pointLightColor.value = lights.point.colors;
  1215. uniforms.pointLightPosition.value = lights.point.positions;
  1216. uniforms.pointLightDistance.value = lights.point.distances;
  1217. uniforms.pointLightDecay.value = lights.point.decays;
  1218. uniforms.spotLightColor.value = lights.spot.colors;
  1219. uniforms.spotLightPosition.value = lights.spot.positions;
  1220. uniforms.spotLightDistance.value = lights.spot.distances;
  1221. uniforms.spotLightDirection.value = lights.spot.directions;
  1222. uniforms.spotLightAngleCos.value = lights.spot.anglesCos;
  1223. uniforms.spotLightExponent.value = lights.spot.exponents;
  1224. uniforms.spotLightDecay.value = lights.spot.decays;
  1225. uniforms.hemisphereLightSkyColor.value = lights.hemi.skyColors;
  1226. uniforms.hemisphereLightGroundColor.value = lights.hemi.groundColors;
  1227. uniforms.hemisphereLightDirection.value = lights.hemi.positions;
  1228. }
  1229. // If uniforms are marked as clean, they don't need to be loaded to the GPU.
  1230. function markUniformsLightsNeedsUpdate ( uniforms, value ) {
  1231. uniforms.ambientLightColor.needsUpdate = value;
  1232. uniforms.directionalLightColor.needsUpdate = value;
  1233. uniforms.directionalLightDirection.needsUpdate = value;
  1234. uniforms.pointLightColor.needsUpdate = value;
  1235. uniforms.pointLightPosition.needsUpdate = value;
  1236. uniforms.pointLightDistance.needsUpdate = value;
  1237. uniforms.pointLightDecay.needsUpdate = value;
  1238. uniforms.spotLightColor.needsUpdate = value;
  1239. uniforms.spotLightPosition.needsUpdate = value;
  1240. uniforms.spotLightDistance.needsUpdate = value;
  1241. uniforms.spotLightDirection.needsUpdate = value;
  1242. uniforms.spotLightAngleCos.needsUpdate = value;
  1243. uniforms.spotLightExponent.needsUpdate = value;
  1244. uniforms.spotLightDecay.needsUpdate = value;
  1245. uniforms.hemisphereLightSkyColor.needsUpdate = value;
  1246. uniforms.hemisphereLightGroundColor.needsUpdate = value;
  1247. uniforms.hemisphereLightDirection.needsUpdate = value;
  1248. }
  1249. function refreshUniformsShadow ( uniforms, lights ) {
  1250. if ( uniforms.shadowMatrix ) {
  1251. var j = 0;
  1252. for ( var i = 0, il = lights.length; i < il; i ++ ) {
  1253. var light = lights[ i ];
  1254. if ( ! light.castShadow ) continue;
  1255. if ( light instanceof THREE.SpotLight || ( light instanceof THREE.DirectionalLight ) ) {
  1256. uniforms.shadowMap.value[ j ] = light.shadowMap;
  1257. uniforms.shadowMapSize.value[ j ] = light.shadowMapSize;
  1258. uniforms.shadowMatrix.value[ j ] = light.shadowMatrix;
  1259. uniforms.shadowDarkness.value[ j ] = light.shadowDarkness;
  1260. uniforms.shadowBias.value[ j ] = light.shadowBias;
  1261. j ++;
  1262. }
  1263. }
  1264. }
  1265. }
  1266. // Uniforms (load to GPU)
  1267. function loadUniformsMatrices ( uniforms, object ) {
  1268. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrix.elements );
  1269. if ( uniforms.normalMatrix ) {
  1270. _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrix.elements );
  1271. }
  1272. }
  1273. function getTextureUnit() {
  1274. var textureUnit = _usedTextureUnits;
  1275. if ( textureUnit >= _maxTextures ) {
  1276. console.warn( 'WebGLRenderer: trying to use ' + textureUnit + ' texture units while this GPU supports only ' + _maxTextures );
  1277. }
  1278. _usedTextureUnits += 1;
  1279. return textureUnit;
  1280. }
  1281. function loadUniformsGeneric ( uniforms ) {
  1282. var texture, textureUnit, offset;
  1283. for ( var j = 0, jl = uniforms.length; j < jl; j ++ ) {
  1284. var uniform = uniforms[ j ][ 0 ];
  1285. // needsUpdate property is not added to all uniforms.
  1286. if ( uniform.needsUpdate === false ) continue;
  1287. var type = uniform.type;
  1288. var value = uniform.value;
  1289. var location = uniforms[ j ][ 1 ];
  1290. switch ( type ) {
  1291. case '1i':
  1292. _gl.uniform1i( location, value );
  1293. break;
  1294. case '1f':
  1295. _gl.uniform1f( location, value );
  1296. break;
  1297. case '2f':
  1298. _gl.uniform2f( location, value[ 0 ], value[ 1 ] );
  1299. break;
  1300. case '3f':
  1301. _gl.uniform3f( location, value[ 0 ], value[ 1 ], value[ 2 ] );
  1302. break;
  1303. case '4f':
  1304. _gl.uniform4f( location, value[ 0 ], value[ 1 ], value[ 2 ], value[ 3 ] );
  1305. break;
  1306. case '1iv':
  1307. _gl.uniform1iv( location, value );
  1308. break;
  1309. case '3iv':
  1310. _gl.uniform3iv( location, value );
  1311. break;
  1312. case '1fv':
  1313. _gl.uniform1fv( location, value );
  1314. break;
  1315. case '2fv':
  1316. _gl.uniform2fv( location, value );
  1317. break;
  1318. case '3fv':
  1319. _gl.uniform3fv( location, value );
  1320. break;
  1321. case '4fv':
  1322. _gl.uniform4fv( location, value );
  1323. break;
  1324. case 'Matrix3fv':
  1325. _gl.uniformMatrix3fv( location, false, value );
  1326. break;
  1327. case 'Matrix4fv':
  1328. _gl.uniformMatrix4fv( location, false, value );
  1329. break;
  1330. //
  1331. case 'i':
  1332. // single integer
  1333. _gl.uniform1i( location, value );
  1334. break;
  1335. case 'f':
  1336. // single float
  1337. _gl.uniform1f( location, value );
  1338. break;
  1339. case 'v2':
  1340. // single THREE.Vector2
  1341. _gl.uniform2f( location, value.x, value.y );
  1342. break;
  1343. case 'v3':
  1344. // single THREE.Vector3
  1345. _gl.uniform3f( location, value.x, value.y, value.z );
  1346. break;
  1347. case 'v4':
  1348. // single THREE.Vector4
  1349. _gl.uniform4f( location, value.x, value.y, value.z, value.w );
  1350. break;
  1351. case 'c':
  1352. // single THREE.Color
  1353. _gl.uniform3f( location, value.r, value.g, value.b );
  1354. break;
  1355. case 'iv1':
  1356. // flat array of integers (JS or typed array)
  1357. _gl.uniform1iv( location, value );
  1358. break;
  1359. case 'iv':
  1360. // flat array of integers with 3 x N size (JS or typed array)
  1361. _gl.uniform3iv( location, value );
  1362. break;
  1363. case 'fv1':
  1364. // flat array of floats (JS or typed array)
  1365. _gl.uniform1fv( location, value );
  1366. break;
  1367. case 'fv':
  1368. // flat array of floats with 3 x N size (JS or typed array)
  1369. _gl.uniform3fv( location, value );
  1370. break;
  1371. case 'v2v':
  1372. // array of THREE.Vector2
  1373. if ( uniform._array === undefined ) {
  1374. uniform._array = new Float32Array( 2 * value.length );
  1375. }
  1376. for ( var i = 0, il = value.length; i < il; i ++ ) {
  1377. offset = i * 2;
  1378. uniform._array[ offset + 0 ] = value[ i ].x;
  1379. uniform._array[ offset + 1 ] = value[ i ].y;
  1380. }
  1381. _gl.uniform2fv( location, uniform._array );
  1382. break;
  1383. case 'v3v':
  1384. // array of THREE.Vector3
  1385. if ( uniform._array === undefined ) {
  1386. uniform._array = new Float32Array( 3 * value.length );
  1387. }
  1388. for ( var i = 0, il = value.length; i < il; i ++ ) {
  1389. offset = i * 3;
  1390. uniform._array[ offset + 0 ] = value[ i ].x;
  1391. uniform._array[ offset + 1 ] = value[ i ].y;
  1392. uniform._array[ offset + 2 ] = value[ i ].z;
  1393. }
  1394. _gl.uniform3fv( location, uniform._array );
  1395. break;
  1396. case 'v4v':
  1397. // array of THREE.Vector4
  1398. if ( uniform._array === undefined ) {
  1399. uniform._array = new Float32Array( 4 * value.length );
  1400. }
  1401. for ( var i = 0, il = value.length; i < il; i ++ ) {
  1402. offset = i * 4;
  1403. uniform._array[ offset + 0 ] = value[ i ].x;
  1404. uniform._array[ offset + 1 ] = value[ i ].y;
  1405. uniform._array[ offset + 2 ] = value[ i ].z;
  1406. uniform._array[ offset + 3 ] = value[ i ].w;
  1407. }
  1408. _gl.uniform4fv( location, uniform._array );
  1409. break;
  1410. case 'm3':
  1411. // single THREE.Matrix3
  1412. _gl.uniformMatrix3fv( location, false, value.elements );
  1413. break;
  1414. case 'm3v':
  1415. // array of THREE.Matrix3
  1416. if ( uniform._array === undefined ) {
  1417. uniform._array = new Float32Array( 9 * value.length );
  1418. }
  1419. for ( var i = 0, il = value.length; i < il; i ++ ) {
  1420. value[ i ].flattenToArrayOffset( uniform._array, i * 9 );
  1421. }
  1422. _gl.uniformMatrix3fv( location, false, uniform._array );
  1423. break;
  1424. case 'm4':
  1425. // single THREE.Matrix4
  1426. _gl.uniformMatrix4fv( location, false, value.elements );
  1427. break;
  1428. case 'm4v':
  1429. // array of THREE.Matrix4
  1430. if ( uniform._array === undefined ) {
  1431. uniform._array = new Float32Array( 16 * value.length );
  1432. }
  1433. for ( var i = 0, il = value.length; i < il; i ++ ) {
  1434. value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
  1435. }
  1436. _gl.uniformMatrix4fv( location, false, uniform._array );
  1437. break;
  1438. case 't':
  1439. // single THREE.Texture (2d or cube)
  1440. texture = value;
  1441. textureUnit = getTextureUnit();
  1442. _gl.uniform1i( location, textureUnit );
  1443. if ( ! texture ) continue;
  1444. if ( texture instanceof THREE.CubeTexture ||
  1445. ( Array.isArray( texture.image ) && texture.image.length === 6 ) ) {
  1446. // CompressedTexture can have Array in image :/
  1447. setCubeTexture( texture, textureUnit );
  1448. } else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
  1449. setCubeTextureDynamic( texture, textureUnit );
  1450. } else {
  1451. _this.setTexture( texture, textureUnit );
  1452. }
  1453. break;
  1454. case 'tv':
  1455. // array of THREE.Texture (2d)
  1456. if ( uniform._array === undefined ) {
  1457. uniform._array = [];
  1458. }
  1459. for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
  1460. uniform._array[ i ] = getTextureUnit();
  1461. }
  1462. _gl.uniform1iv( location, uniform._array );
  1463. for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
  1464. texture = uniform.value[ i ];
  1465. textureUnit = uniform._array[ i ];
  1466. if ( ! texture ) continue;
  1467. _this.setTexture( texture, textureUnit );
  1468. }
  1469. break;
  1470. default:
  1471. console.warn( 'THREE.WebGLRenderer: Unknown uniform type: ' + type );
  1472. }
  1473. }
  1474. }
  1475. function setColorLinear( array, offset, color, intensity ) {
  1476. array[ offset + 0 ] = color.r * intensity;
  1477. array[ offset + 1 ] = color.g * intensity;
  1478. array[ offset + 2 ] = color.b * intensity;
  1479. }
  1480. function setupLights ( lights ) {
  1481. var l, ll, light,
  1482. r = 0, g = 0, b = 0,
  1483. color, skyColor, groundColor,
  1484. intensity,
  1485. distance,
  1486. zlights = _lights,
  1487. dirColors = zlights.directional.colors,
  1488. dirPositions = zlights.directional.positions,
  1489. pointColors = zlights.point.colors,
  1490. pointPositions = zlights.point.positions,
  1491. pointDistances = zlights.point.distances,
  1492. pointDecays = zlights.point.decays,
  1493. spotColors = zlights.spot.colors,
  1494. spotPositions = zlights.spot.positions,
  1495. spotDistances = zlights.spot.distances,
  1496. spotDirections = zlights.spot.directions,
  1497. spotAnglesCos = zlights.spot.anglesCos,
  1498. spotExponents = zlights.spot.exponents,
  1499. spotDecays = zlights.spot.decays,
  1500. hemiSkyColors = zlights.hemi.skyColors,
  1501. hemiGroundColors = zlights.hemi.groundColors,
  1502. hemiPositions = zlights.hemi.positions,
  1503. dirLength = 0,
  1504. pointLength = 0,
  1505. spotLength = 0,
  1506. hemiLength = 0,
  1507. dirCount = 0,
  1508. pointCount = 0,
  1509. spotCount = 0,
  1510. hemiCount = 0,
  1511. dirOffset = 0,
  1512. pointOffset = 0,
  1513. spotOffset = 0,
  1514. hemiOffset = 0;
  1515. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  1516. light = lights[ l ];
  1517. if ( light.onlyShadow ) continue;
  1518. color = light.color;
  1519. intensity = light.intensity;
  1520. distance = light.distance;
  1521. if ( light instanceof THREE.AmbientLight ) {
  1522. if ( ! light.visible ) continue;
  1523. r += color.r;
  1524. g += color.g;
  1525. b += color.b;
  1526. } else if ( light instanceof THREE.DirectionalLight ) {
  1527. dirCount += 1;
  1528. if ( ! light.visible ) continue;
  1529. _direction.setFromMatrixPosition( light.matrixWorld );
  1530. _vector3.setFromMatrixPosition( light.target.matrixWorld );
  1531. _direction.sub( _vector3 );
  1532. _direction.normalize();
  1533. dirOffset = dirLength * 3;
  1534. dirPositions[ dirOffset + 0 ] = _direction.x;
  1535. dirPositions[ dirOffset + 1 ] = _direction.y;
  1536. dirPositions[ dirOffset + 2 ] = _direction.z;
  1537. setColorLinear( dirColors, dirOffset, color, intensity );
  1538. dirLength += 1;
  1539. } else if ( light instanceof THREE.PointLight ) {
  1540. pointCount += 1;
  1541. if ( ! light.visible ) continue;
  1542. pointOffset = pointLength * 3;
  1543. setColorLinear( pointColors, pointOffset, color, intensity );
  1544. _vector3.setFromMatrixPosition( light.matrixWorld );
  1545. pointPositions[ pointOffset + 0 ] = _vector3.x;
  1546. pointPositions[ pointOffset + 1 ] = _vector3.y;
  1547. pointPositions[ pointOffset + 2 ] = _vector3.z;
  1548. // distance is 0 if decay is 0, because there is no attenuation at all.
  1549. pointDistances[ pointLength ] = distance;
  1550. pointDecays[ pointLength ] = ( light.distance === 0 ) ? 0.0 : light.decay;
  1551. pointLength += 1;
  1552. } else if ( light instanceof THREE.SpotLight ) {
  1553. spotCount += 1;
  1554. if ( ! light.visible ) continue;
  1555. spotOffset = spotLength * 3;
  1556. setColorLinear( spotColors, spotOffset, color, intensity );
  1557. _direction.setFromMatrixPosition( light.matrixWorld );
  1558. spotPositions[ spotOffset + 0 ] = _direction.x;
  1559. spotPositions[ spotOffset + 1 ] = _direction.y;
  1560. spotPositions[ spotOffset + 2 ] = _direction.z;
  1561. spotDistances[ spotLength ] = distance;
  1562. _vector3.setFromMatrixPosition( light.target.matrixWorld );
  1563. _direction.sub( _vector3 );
  1564. _direction.normalize();
  1565. spotDirections[ spotOffset + 0 ] = _direction.x;
  1566. spotDirections[ spotOffset + 1 ] = _direction.y;
  1567. spotDirections[ spotOffset + 2 ] = _direction.z;
  1568. spotAnglesCos[ spotLength ] = Math.cos( light.angle );
  1569. spotExponents[ spotLength ] = light.exponent;
  1570. spotDecays[ spotLength ] = ( light.distance === 0 ) ? 0.0 : light.decay;
  1571. spotLength += 1;
  1572. } else if ( light instanceof THREE.HemisphereLight ) {
  1573. hemiCount += 1;
  1574. if ( ! light.visible ) continue;
  1575. _direction.setFromMatrixPosition( light.matrixWorld );
  1576. _direction.normalize();
  1577. hemiOffset = hemiLength * 3;
  1578. hemiPositions[ hemiOffset + 0 ] = _direction.x;
  1579. hemiPositions[ hemiOffset + 1 ] = _direction.y;
  1580. hemiPositions[ hemiOffset + 2 ] = _direction.z;
  1581. skyColor = light.color;
  1582. groundColor = light.groundColor;
  1583. setColorLinear( hemiSkyColors, hemiOffset, skyColor, intensity );
  1584. setColorLinear( hemiGroundColors, hemiOffset, groundColor, intensity );
  1585. hemiLength += 1;
  1586. }
  1587. }
  1588. // null eventual remains from removed lights
  1589. // (this is to avoid if in shader)
  1590. for ( l = dirLength * 3, ll = Math.max( dirColors.length, dirCount * 3 ); l < ll; l ++ ) dirColors[ l ] = 0.0;
  1591. for ( l = pointLength * 3, ll = Math.max( pointColors.length, pointCount * 3 ); l < ll; l ++ ) pointColors[ l ] = 0.0;
  1592. for ( l = spotLength * 3, ll = Math.max( spotColors.length, spotCount * 3 ); l < ll; l ++ ) spotColors[ l ] = 0.0;
  1593. for ( l = hemiLength * 3, ll = Math.max( hemiSkyColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiSkyColors[ l ] = 0.0;
  1594. for ( l = hemiLength * 3, ll = Math.max( hemiGroundColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiGroundColors[ l ] = 0.0;
  1595. zlights.directional.length = dirLength;
  1596. zlights.point.length = pointLength;
  1597. zlights.spot.length = spotLength;
  1598. zlights.hemi.length = hemiLength;
  1599. zlights.ambient[ 0 ] = r;
  1600. zlights.ambient[ 1 ] = g;
  1601. zlights.ambient[ 2 ] = b;
  1602. }
  1603. // GL state setting
  1604. this.setFaceCulling = function ( cullFace, frontFaceDirection ) {
  1605. if ( cullFace === THREE.CullFaceNone ) {
  1606. state.disable( _gl.CULL_FACE );
  1607. } else {
  1608. if ( frontFaceDirection === THREE.FrontFaceDirectionCW ) {
  1609. _gl.frontFace( _gl.CW );
  1610. } else {
  1611. _gl.frontFace( _gl.CCW );
  1612. }
  1613. if ( cullFace === THREE.CullFaceBack ) {
  1614. _gl.cullFace( _gl.BACK );
  1615. } else if ( cullFace === THREE.CullFaceFront ) {
  1616. _gl.cullFace( _gl.FRONT );
  1617. } else {
  1618. _gl.cullFace( _gl.FRONT_AND_BACK );
  1619. }
  1620. state.enable( _gl.CULL_FACE );
  1621. }
  1622. };
  1623. // Textures
  1624. function setTextureParameters ( textureType, texture, isImagePowerOfTwo ) {
  1625. var extension;
  1626. if ( isImagePowerOfTwo ) {
  1627. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) );
  1628. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) );
  1629. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.magFilter ) );
  1630. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.minFilter ) );
  1631. } else {
  1632. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  1633. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  1634. if ( texture.wrapS !== THREE.ClampToEdgeWrapping || texture.wrapT !== THREE.ClampToEdgeWrapping ) {
  1635. console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.wrapS and Texture.wrapT should be set to THREE.ClampToEdgeWrapping. ( ' + texture.sourceFile + ' )' );
  1636. }
  1637. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
  1638. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
  1639. if ( texture.minFilter !== THREE.NearestFilter && texture.minFilter !== THREE.LinearFilter ) {
  1640. console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter should be set to THREE.NearestFilter or THREE.LinearFilter. ( ' + texture.sourceFile + ' )' );
  1641. }
  1642. }
  1643. extension = extensions.get( 'EXT_texture_filter_anisotropic' );
  1644. if ( extension && texture.type !== THREE.FloatType && texture.type !== THREE.HalfFloatType ) {
  1645. if ( texture.anisotropy > 1 || properties.get( texture ).__currentAnisotropy ) {
  1646. _gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, _this.getMaxAnisotropy() ) );
  1647. properties.get( texture ).__currentAnisotropy = texture.anisotropy;
  1648. }
  1649. }
  1650. }
  1651. function uploadTexture( textureProperties, texture, slot ) {
  1652. if ( textureProperties.__webglInit === undefined ) {
  1653. textureProperties.__webglInit = true;
  1654. texture.__webglInit = true;
  1655. texture.addEventListener( 'dispose', onTextureDispose );
  1656. textureProperties.__webglTexture = _gl.createTexture();
  1657. _infoMemory.textures ++;
  1658. }
  1659. state.activeTexture( _gl.TEXTURE0 + slot );
  1660. state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );
  1661. _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  1662. _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  1663. _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
  1664. texture.image = clampToMaxSize( texture.image, _maxTextureSize );
  1665. var image = texture.image,
  1666. isImagePowerOfTwo = THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height ),
  1667. glFormat = paramThreeToGL( texture.format ),
  1668. glType = paramThreeToGL( texture.type );
  1669. setTextureParameters( _gl.TEXTURE_2D, texture, isImagePowerOfTwo );
  1670. var mipmap, mipmaps = texture.mipmaps;
  1671. if ( texture instanceof THREE.DataTexture ) {
  1672. // use manually created mipmaps if available
  1673. // if there are no manual mipmaps
  1674. // set 0 level mipmap and then use GL to generate other mipmap levels
  1675. if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
  1676. for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
  1677. mipmap = mipmaps[ i ];
  1678. state.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  1679. }
  1680. texture.generateMipmaps = false;
  1681. } else {
  1682. state.texImage2D( _gl.TEXTURE_2D, 0, glFormat, image.width, image.height, 0, glFormat, glType, image.data );
  1683. }
  1684. } else if ( texture instanceof THREE.CompressedTexture ) {
  1685. for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
  1686. mipmap = mipmaps[ i ];
  1687. if ( texture.format !== THREE.RGBAFormat && texture.format !== THREE.RGBFormat ) {
  1688. if ( state.getCompressedTextureFormats().indexOf( glFormat ) > - 1 ) {
  1689. state.compressedTexImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
  1690. } else {
  1691. console.warn( "THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()" );
  1692. }
  1693. } else {
  1694. state.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  1695. }
  1696. }
  1697. } else {
  1698. // regular Texture (image, video, canvas)
  1699. // use manually created mipmaps if available
  1700. // if there are no manual mipmaps
  1701. // set 0 level mipmap and then use GL to generate other mipmap levels
  1702. if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
  1703. for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
  1704. mipmap = mipmaps[ i ];
  1705. state.texImage2D( _gl.TEXTURE_2D, i, glFormat, glFormat, glType, mipmap );
  1706. }
  1707. texture.generateMipmaps = false;
  1708. } else {
  1709. state.texImage2D( _gl.TEXTURE_2D, 0, glFormat, glFormat, glType, texture.image );
  1710. }
  1711. }
  1712. if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  1713. textureProperties.__version = texture.version;
  1714. if ( texture.onUpdate ) texture.onUpdate( texture );
  1715. }
  1716. this.setTexture = function ( texture, slot ) {
  1717. var textureProperties = properties.get( texture );
  1718. if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
  1719. var image = texture.image;
  1720. if ( image === undefined ) {
  1721. console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is undefined', texture );
  1722. return;
  1723. }
  1724. if ( image.complete === false ) {
  1725. console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete', texture );
  1726. return;
  1727. }
  1728. uploadTexture( textureProperties, texture, slot );
  1729. return;
  1730. }
  1731. state.activeTexture( _gl.TEXTURE0 + slot );
  1732. state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );
  1733. };
  1734. function clampToMaxSize ( image, maxSize ) {
  1735. if ( image.width > maxSize || image.height > maxSize ) {
  1736. // Warning: Scaling through the canvas will only work with images that use
  1737. // premultiplied alpha.
  1738. var scale = maxSize / Math.max( image.width, image.height );
  1739. var canvas = document.createElement( 'canvas' );
  1740. canvas.width = Math.floor( image.width * scale );
  1741. canvas.height = Math.floor( image.height * scale );
  1742. var context = canvas.getContext( '2d' );
  1743. context.drawImage( image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height );
  1744. console.warn( 'THREE.WebGLRenderer: image is too big (' + image.width + 'x' + image.height + '). Resized to ' + canvas.width + 'x' + canvas.height, image );
  1745. return canvas;
  1746. }
  1747. return image;
  1748. }
  1749. function setCubeTexture ( texture, slot ) {
  1750. var textureProperties = properties.get( texture );
  1751. if ( texture.image.length === 6 ) {
  1752. if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
  1753. if ( ! textureProperties.__image__webglTextureCube ) {
  1754. texture.addEventListener( 'dispose', onTextureDispose );
  1755. textureProperties.__image__webglTextureCube = _gl.createTexture();
  1756. _infoMemory.textures ++;
  1757. }
  1758. state.activeTexture( _gl.TEXTURE0 + slot );
  1759. state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__image__webglTextureCube );
  1760. _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  1761. var isCompressed = texture instanceof THREE.CompressedTexture;
  1762. var isDataTexture = texture.image[ 0 ] instanceof THREE.DataTexture;
  1763. var cubeImage = [];
  1764. for ( var i = 0; i < 6; i ++ ) {
  1765. if ( _this.autoScaleCubemaps && ! isCompressed && ! isDataTexture ) {
  1766. cubeImage[ i ] = clampToMaxSize( texture.image[ i ], _maxCubemapSize );
  1767. } else {
  1768. cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
  1769. }
  1770. }
  1771. var image = cubeImage[ 0 ],
  1772. isImagePowerOfTwo = THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height ),
  1773. glFormat = paramThreeToGL( texture.format ),
  1774. glType = paramThreeToGL( texture.type );
  1775. setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isImagePowerOfTwo );
  1776. for ( var i = 0; i < 6; i ++ ) {
  1777. if ( ! isCompressed ) {
  1778. if ( isDataTexture ) {
  1779. state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, cubeImage[ i ].width, cubeImage[ i ].height, 0, glFormat, glType, cubeImage[ i ].data );
  1780. } else {
  1781. state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] );
  1782. }
  1783. } else {
  1784. var mipmap, mipmaps = cubeImage[ i ].mipmaps;
  1785. for ( var j = 0, jl = mipmaps.length; j < jl; j ++ ) {
  1786. mipmap = mipmaps[ j ];
  1787. if ( texture.format !== THREE.RGBAFormat && texture.format !== THREE.RGBFormat ) {
  1788. if ( state.getCompressedTextureFormats().indexOf( glFormat ) > - 1 ) {
  1789. state.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
  1790. } else {
  1791. console.warn( "THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .setCubeTexture()" );
  1792. }
  1793. } else {
  1794. state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  1795. }
  1796. }
  1797. }
  1798. }
  1799. if ( texture.generateMipmaps && isImagePowerOfTwo ) {
  1800. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  1801. }
  1802. textureProperties.__version = texture.version;
  1803. if ( texture.onUpdate ) texture.onUpdate( texture );
  1804. } else {
  1805. state.activeTexture( _gl.TEXTURE0 + slot );
  1806. state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__image__webglTextureCube );
  1807. }
  1808. }
  1809. }
  1810. function setCubeTextureDynamic ( texture, slot ) {
  1811. state.activeTexture( _gl.TEXTURE0 + slot );
  1812. state.bindTexture( _gl.TEXTURE_CUBE_MAP, properties.get( texture ).__webglTexture );
  1813. }
  1814. // Render targets
  1815. function setupFrameBuffer ( framebuffer, renderTarget, textureTarget ) {
  1816. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  1817. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureTarget, properties.get( renderTarget ).__webglTexture, 0 );
  1818. }
  1819. function setupRenderBuffer ( renderbuffer, renderTarget ) {
  1820. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );
  1821. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  1822. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );
  1823. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  1824. /* For some reason this is not working. Defaulting to RGBA4.
  1825. } else if ( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  1826. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height );
  1827. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  1828. */
  1829. } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  1830. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
  1831. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  1832. } else {
  1833. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );
  1834. }
  1835. }
  1836. this.setRenderTarget = function ( renderTarget ) {
  1837. var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
  1838. if ( renderTarget && properties.get( renderTarget ).__webglFramebuffer === undefined ) {
  1839. var renderTargetProperties = properties.get( renderTarget );
  1840. if ( renderTarget.depthBuffer === undefined ) renderTarget.depthBuffer = true;
  1841. if ( renderTarget.stencilBuffer === undefined ) renderTarget.stencilBuffer = true;
  1842. renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
  1843. renderTargetProperties.__webglTexture = _gl.createTexture();
  1844. _infoMemory.textures ++;
  1845. // Setup texture, create render and frame buffers
  1846. var isTargetPowerOfTwo = THREE.Math.isPowerOfTwo( renderTarget.width ) && THREE.Math.isPowerOfTwo( renderTarget.height ),
  1847. glFormat = paramThreeToGL( renderTarget.format ),
  1848. glType = paramThreeToGL( renderTarget.type );
  1849. if ( isCube ) {
  1850. renderTargetProperties.__webglFramebuffer = [];
  1851. renderTargetProperties.__webglRenderbuffer = [];
  1852. state.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTargetProperties.__webglTexture );
  1853. setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget, isTargetPowerOfTwo );
  1854. for ( var i = 0; i < 6; i ++ ) {
  1855. renderTargetProperties.__webglFramebuffer[ i ] = _gl.createFramebuffer();
  1856. renderTargetProperties.__webglRenderbuffer[ i ] = _gl.createRenderbuffer();
  1857. state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  1858. setupFrameBuffer( renderTargetProperties.__webglFramebuffer[ i ], renderTarget, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
  1859. setupRenderBuffer( renderTargetProperties.__webglRenderbuffer[ i ], renderTarget );
  1860. }
  1861. if ( renderTarget.generateMipmaps && isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  1862. } else {
  1863. renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer();
  1864. if ( renderTarget.shareDepthFrom ) {
  1865. renderTargetProperties.__webglRenderbuffer = renderTarget.shareDepthFrom.__webglRenderbuffer;
  1866. } else {
  1867. renderTargetProperties.__webglRenderbuffer = _gl.createRenderbuffer();
  1868. }
  1869. state.bindTexture( _gl.TEXTURE_2D, renderTargetProperties.__webglTexture );
  1870. setTextureParameters( _gl.TEXTURE_2D, renderTarget, isTargetPowerOfTwo );
  1871. state.texImage2D( _gl.TEXTURE_2D, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  1872. setupFrameBuffer( renderTargetProperties.__webglFramebuffer, renderTarget, _gl.TEXTURE_2D );
  1873. if ( renderTarget.shareDepthFrom ) {
  1874. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  1875. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTargetProperties.__webglRenderbuffer );
  1876. } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  1877. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderTargetProperties.__webglRenderbuffer );
  1878. }
  1879. } else {
  1880. setupRenderBuffer( renderTargetProperties.__webglRenderbuffer, renderTarget );
  1881. }
  1882. if ( renderTarget.generateMipmaps && isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  1883. }
  1884. // Release everything
  1885. if ( isCube ) {
  1886. state.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  1887. } else {
  1888. state.bindTexture( _gl.TEXTURE_2D, null );
  1889. }
  1890. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  1891. _gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
  1892. }
  1893. var framebuffer, width, height, vx, vy;
  1894. if ( renderTarget ) {
  1895. var renderTargetProperties = properties.get( renderTarget );
  1896. if ( isCube ) {
  1897. framebuffer = renderTargetProperties.__webglFramebuffer[ renderTarget.activeCubeFace ];
  1898. } else {
  1899. framebuffer = renderTargetProperties.__webglFramebuffer;
  1900. }
  1901. width = renderTarget.width;
  1902. height = renderTarget.height;
  1903. vx = 0;
  1904. vy = 0;
  1905. } else {
  1906. framebuffer = null;
  1907. width = _viewportWidth;
  1908. height = _viewportHeight;
  1909. vx = _viewportX;
  1910. vy = _viewportY;
  1911. }
  1912. if ( framebuffer !== _currentFramebuffer ) {
  1913. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  1914. _gl.viewport( vx, vy, width, height );
  1915. _currentFramebuffer = framebuffer;
  1916. }
  1917. _currentWidth = width;
  1918. _currentHeight = height;
  1919. };
  1920. this.readRenderTargetPixels = function( renderTarget, x, y, width, height, buffer ) {
  1921. if ( ! ( renderTarget instanceof THREE.WebGLRenderTarget ) ) {
  1922. console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
  1923. return;
  1924. }
  1925. if ( properties.get( renderTarget ).__webglFramebuffer ) {
  1926. if ( renderTarget.format !== THREE.RGBAFormat ) {
  1927. console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in RGBA format. readPixels can read only RGBA format.' );
  1928. return;
  1929. }
  1930. var restore = false;
  1931. if ( properties.get( renderTarget ).__webglFramebuffer !== _currentFramebuffer ) {
  1932. _gl.bindFramebuffer( _gl.FRAMEBUFFER, properties.get( renderTarget ).__webglFramebuffer );
  1933. restore = true;
  1934. }
  1935. if ( _gl.checkFramebufferStatus( _gl.FRAMEBUFFER ) === _gl.FRAMEBUFFER_COMPLETE ) {
  1936. _gl.readPixels( x, y, width, height, _gl.RGBA, _gl.UNSIGNED_BYTE, buffer );
  1937. } else {
  1938. console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: readPixels from renderTarget failed. Framebuffer not complete.' );
  1939. }
  1940. if ( restore ) {
  1941. _gl.bindFramebuffer( _gl.FRAMEBUFFER, _currentFramebuffer );
  1942. }
  1943. }
  1944. };
  1945. function updateRenderTargetMipmap ( renderTarget ) {
  1946. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  1947. state.bindTexture( _gl.TEXTURE_CUBE_MAP, properties.get( renderTarget ).__webglTexture );
  1948. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  1949. state.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  1950. } else {
  1951. state.bindTexture( _gl.TEXTURE_2D, properties.get( renderTarget ).__webglTexture );
  1952. _gl.generateMipmap( _gl.TEXTURE_2D );
  1953. state.bindTexture( _gl.TEXTURE_2D, null );
  1954. }
  1955. }
  1956. // Fallback filters for non-power-of-2 textures
  1957. function filterFallback ( f ) {
  1958. if ( f === THREE.NearestFilter || f === THREE.NearestMipMapNearestFilter || f === THREE.NearestMipMapLinearFilter ) {
  1959. return _gl.NEAREST;
  1960. }
  1961. return _gl.LINEAR;
  1962. }
  1963. // Map three.js constants to WebGL constants
  1964. function paramThreeToGL ( p ) {
  1965. var extension;
  1966. if ( p === THREE.RepeatWrapping ) return _gl.REPEAT;
  1967. if ( p === THREE.ClampToEdgeWrapping ) return _gl.CLAMP_TO_EDGE;
  1968. if ( p === THREE.MirroredRepeatWrapping ) return _gl.MIRRORED_REPEAT;
  1969. if ( p === THREE.NearestFilter ) return _gl.NEAREST;
  1970. if ( p === THREE.NearestMipMapNearestFilter ) return _gl.NEAREST_MIPMAP_NEAREST;
  1971. if ( p === THREE.NearestMipMapLinearFilter ) return _gl.NEAREST_MIPMAP_LINEAR;
  1972. if ( p === THREE.LinearFilter ) return _gl.LINEAR;
  1973. if ( p === THREE.LinearMipMapNearestFilter ) return _gl.LINEAR_MIPMAP_NEAREST;
  1974. if ( p === THREE.LinearMipMapLinearFilter ) return _gl.LINEAR_MIPMAP_LINEAR;
  1975. if ( p === THREE.UnsignedByteType ) return _gl.UNSIGNED_BYTE;
  1976. if ( p === THREE.UnsignedShort4444Type ) return _gl.UNSIGNED_SHORT_4_4_4_4;
  1977. if ( p === THREE.UnsignedShort5551Type ) return _gl.UNSIGNED_SHORT_5_5_5_1;
  1978. if ( p === THREE.UnsignedShort565Type ) return _gl.UNSIGNED_SHORT_5_6_5;
  1979. if ( p === THREE.ByteType ) return _gl.BYTE;
  1980. if ( p === THREE.ShortType ) return _gl.SHORT;
  1981. if ( p === THREE.UnsignedShortType ) return _gl.UNSIGNED_SHORT;
  1982. if ( p === THREE.IntType ) return _gl.INT;
  1983. if ( p === THREE.UnsignedIntType ) return _gl.UNSIGNED_INT;
  1984. if ( p === THREE.FloatType ) return _gl.FLOAT;
  1985. extension = extensions.get( 'OES_texture_half_float' );
  1986. if ( extension !== null ) {
  1987. if ( p === THREE.HalfFloatType ) return extension.HALF_FLOAT_OES;
  1988. }
  1989. if ( p === THREE.AlphaFormat ) return _gl.ALPHA;
  1990. if ( p === THREE.RGBFormat ) return _gl.RGB;
  1991. if ( p === THREE.RGBAFormat ) return _gl.RGBA;
  1992. if ( p === THREE.LuminanceFormat ) return _gl.LUMINANCE;
  1993. if ( p === THREE.LuminanceAlphaFormat ) return _gl.LUMINANCE_ALPHA;
  1994. if ( p === THREE.AddEquation ) return _gl.FUNC_ADD;
  1995. if ( p === THREE.SubtractEquation ) return _gl.FUNC_SUBTRACT;
  1996. if ( p === THREE.ReverseSubtractEquation ) return _gl.FUNC_REVERSE_SUBTRACT;
  1997. if ( p === THREE.ZeroFactor ) return _gl.ZERO;
  1998. if ( p === THREE.OneFactor ) return _gl.ONE;
  1999. if ( p === THREE.SrcColorFactor ) return _gl.SRC_COLOR;
  2000. if ( p === THREE.OneMinusSrcColorFactor ) return _gl.ONE_MINUS_SRC_COLOR;
  2001. if ( p === THREE.SrcAlphaFactor ) return _gl.SRC_ALPHA;
  2002. if ( p === THREE.OneMinusSrcAlphaFactor ) return _gl.ONE_MINUS_SRC_ALPHA;
  2003. if ( p === THREE.DstAlphaFactor ) return _gl.DST_ALPHA;
  2004. if ( p === THREE.OneMinusDstAlphaFactor ) return _gl.ONE_MINUS_DST_ALPHA;
  2005. if ( p === THREE.DstColorFactor ) return _gl.DST_COLOR;
  2006. if ( p === THREE.OneMinusDstColorFactor ) return _gl.ONE_MINUS_DST_COLOR;
  2007. if ( p === THREE.SrcAlphaSaturateFactor ) return _gl.SRC_ALPHA_SATURATE;
  2008. extension = extensions.get( 'WEBGL_compressed_texture_s3tc' );
  2009. if ( extension !== null ) {
  2010. if ( p === THREE.RGB_S3TC_DXT1_Format ) return extension.COMPRESSED_RGB_S3TC_DXT1_EXT;
  2011. if ( p === THREE.RGBA_S3TC_DXT1_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT;
  2012. if ( p === THREE.RGBA_S3TC_DXT3_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT;
  2013. if ( p === THREE.RGBA_S3TC_DXT5_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT;
  2014. }
  2015. extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );
  2016. if ( extension !== null ) {
  2017. if ( p === THREE.RGB_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  2018. if ( p === THREE.RGB_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  2019. if ( p === THREE.RGBA_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  2020. if ( p === THREE.RGBA_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  2021. }
  2022. extension = extensions.get( 'EXT_blend_minmax' );
  2023. if ( extension !== null ) {
  2024. if ( p === THREE.MinEquation ) return extension.MIN_EXT;
  2025. if ( p === THREE.MaxEquation ) return extension.MAX_EXT;
  2026. }
  2027. return 0;
  2028. }
  2029. // Allocations
  2030. function allocateBones ( object ) {
  2031. if ( _supportsBoneTextures && object && object.skeleton && object.skeleton.useVertexTexture ) {
  2032. return 1024;
  2033. } else {
  2034. // default for when object is not specified
  2035. // ( for example when prebuilding shader to be used with multiple objects )
  2036. //
  2037. // - leave some extra space for other uniforms
  2038. // - limit here is ANGLE's 254 max uniform vectors
  2039. // (up to 54 should be safe)
  2040. var nVertexUniforms = _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS );
  2041. var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
  2042. var maxBones = nVertexMatrices;
  2043. if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
  2044. maxBones = Math.min( object.skeleton.bones.length, maxBones );
  2045. if ( maxBones < object.skeleton.bones.length ) {
  2046. console.warn( 'WebGLRenderer: too many bones - ' + object.skeleton.bones.length + ', this GPU supports just ' + maxBones + ' (try OpenGL instead of ANGLE)' );
  2047. }
  2048. }
  2049. return maxBones;
  2050. }
  2051. }
  2052. function allocateLights( lights ) {
  2053. var dirLights = 0;
  2054. var pointLights = 0;
  2055. var spotLights = 0;
  2056. var hemiLights = 0;
  2057. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  2058. var light = lights[ l ];
  2059. if ( light.onlyShadow || light.visible === false ) continue;
  2060. if ( light instanceof THREE.DirectionalLight ) dirLights ++;
  2061. if ( light instanceof THREE.PointLight ) pointLights ++;
  2062. if ( light instanceof THREE.SpotLight ) spotLights ++;
  2063. if ( light instanceof THREE.HemisphereLight ) hemiLights ++;
  2064. }
  2065. return { 'directional': dirLights, 'point': pointLights, 'spot': spotLights, 'hemi': hemiLights };
  2066. }
  2067. function allocateShadows( lights ) {
  2068. var maxShadows = 0;
  2069. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  2070. var light = lights[ l ];
  2071. if ( ! light.castShadow ) continue;
  2072. if ( light instanceof THREE.SpotLight ) maxShadows ++;
  2073. if ( light instanceof THREE.DirectionalLight ) maxShadows ++;
  2074. }
  2075. return maxShadows;
  2076. }
  2077. // DEPRECATED
  2078. this.supportsFloatTextures = function () {
  2079. console.warn( 'THREE.WebGLRenderer: .supportsFloatTextures() is now .extensions.get( \'OES_texture_float\' ).' );
  2080. return extensions.get( 'OES_texture_float' );
  2081. };
  2082. this.supportsHalfFloatTextures = function () {
  2083. console.warn( 'THREE.WebGLRenderer: .supportsHalfFloatTextures() is now .extensions.get( \'OES_texture_half_float\' ).' );
  2084. return extensions.get( 'OES_texture_half_float' );
  2085. };
  2086. this.supportsStandardDerivatives = function () {
  2087. console.warn( 'THREE.WebGLRenderer: .supportsStandardDerivatives() is now .extensions.get( \'OES_standard_derivatives\' ).' );
  2088. return extensions.get( 'OES_standard_derivatives' );
  2089. };
  2090. this.supportsCompressedTextureS3TC = function () {
  2091. console.warn( 'THREE.WebGLRenderer: .supportsCompressedTextureS3TC() is now .extensions.get( \'WEBGL_compressed_texture_s3tc\' ).' );
  2092. return extensions.get( 'WEBGL_compressed_texture_s3tc' );
  2093. };
  2094. this.supportsCompressedTexturePVRTC = function () {
  2095. console.warn( 'THREE.WebGLRenderer: .supportsCompressedTexturePVRTC() is now .extensions.get( \'WEBGL_compressed_texture_pvrtc\' ).' );
  2096. return extensions.get( 'WEBGL_compressed_texture_pvrtc' );
  2097. };
  2098. this.supportsBlendMinMax = function () {
  2099. console.warn( 'THREE.WebGLRenderer: .supportsBlendMinMax() is now .extensions.get( \'EXT_blend_minmax\' ).' );
  2100. return extensions.get( 'EXT_blend_minmax' );
  2101. };
  2102. this.initMaterial = function () {
  2103. console.warn( 'THREE.WebGLRenderer: .initMaterial() has been removed.' );
  2104. };
  2105. this.addPrePlugin = function () {
  2106. console.warn( 'THREE.WebGLRenderer: .addPrePlugin() has been removed.' );
  2107. };
  2108. this.addPostPlugin = function () {
  2109. console.warn( 'THREE.WebGLRenderer: .addPostPlugin() has been removed.' );
  2110. };
  2111. this.updateShadowMap = function () {
  2112. console.warn( 'THREE.WebGLRenderer: .updateShadowMap() has been removed.' );
  2113. };
  2114. Object.defineProperties( this, {
  2115. shadowMapEnabled: {
  2116. get: function () {
  2117. return shadowMap.enabled;
  2118. },
  2119. set: function ( value ) {
  2120. console.warn( 'THREE.WebGLRenderer: .shadowMapEnabled is now .shadowMap.enabled.' );
  2121. shadowMap.enabled = value;
  2122. }
  2123. },
  2124. shadowMapType: {
  2125. get: function () {
  2126. return shadowMap.type;
  2127. },
  2128. set: function ( value ) {
  2129. console.warn( 'THREE.WebGLRenderer: .shadowMapType is now .shadowMap.type.' );
  2130. shadowMap.type = value;
  2131. }
  2132. },
  2133. shadowMapCullFace: {
  2134. get: function () {
  2135. return shadowMap.cullFace;
  2136. },
  2137. set: function ( value ) {
  2138. console.warn( 'THREE.WebGLRenderer: .shadowMapCullFace is now .shadowMap.cullFace.' );
  2139. shadowMap.cullFace = value;
  2140. }
  2141. },
  2142. shadowMapDebug: {
  2143. get: function () {
  2144. return shadowMap.debug;
  2145. },
  2146. set: function ( value ) {
  2147. console.warn( 'THREE.WebGLRenderer: .shadowMapDebug is now .shadowMap.debug.' );
  2148. shadowMap.debug = value;
  2149. }
  2150. }
  2151. } );
  2152. };