WebGLRenderer.js 88 KB

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