WebGLRenderer.js 88 KB

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