WebGLRenderer.js 87 KB

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