WebGLRenderer.js 87 KB

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