WebGLDeferredRenderer.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author MPanknin / http://www.redplant.de/
  4. * @author takahiro / https://github.com/takahirox
  5. *
  6. * WebGLDeferredRenderer supports two types of Deferred Renderings.
  7. * One is Classic Deferred Rendering and the other one is
  8. * Light Pre-Pass (Deferred Lighting).
  9. * Classic Deferred Rendering is default. You can use Light Pre-Pass
  10. * by calling .enableLightPrePass( true ) method.
  11. *
  12. * Dependencies
  13. * - THREE.CopyShader
  14. * - THREE.RenderPass
  15. * - THREE.ShaderPass
  16. * - THREE.EffectComposer
  17. * - THREE.FXAAShader
  18. *
  19. * TODO
  20. * - reuse existing glsl
  21. * - shadow
  22. * - optimization
  23. * - MRT (when it's available on Three.js)
  24. * - AmbientLight
  25. * - HemisphereLight
  26. * - PointLight (distance < 0)
  27. * - morphNormals
  28. * - BumpMap
  29. * - ToneMap
  30. * - envMap
  31. * - wrapAround
  32. * - addEffect
  33. */
  34. THREE.WebGLDeferredRenderer = function ( parameters ) {
  35. parameters = parameters || {};
  36. // private properties
  37. var _this = this;
  38. var _gl;
  39. var _width, _height;
  40. // for Classic Deferred Rendering
  41. var _compColor;
  42. var _passColor, _passForward, _passCopy;
  43. // for Light Pre-Pass
  44. var _compReconstruction;
  45. var _passReconstruction;
  46. // for Common
  47. var _compNormalDepth, _compLight, _compFinal;
  48. var _passNormalDepth, _passLight, _passLightFullscreen, _passFinal, _passFXAA;
  49. var _depthTexture;
  50. var _currentCamera;
  51. var _lightScene, _lightFullscreenScene;
  52. var _antialias = false;
  53. var _hasTransparentObject = false;
  54. var _lightPrePass = false;
  55. var _cacheKeepAlive = false;
  56. var _tmpMaterial = new THREE.ShaderMaterial( { visible: false } );
  57. var _tmpVector3 = new THREE.Vector3();
  58. // scene/material/light cache for deferred rendering.
  59. // save them at the creation and release
  60. // if they're unused removeThresholdCount frames
  61. // unless _cacheKeepAlive is true.
  62. // scene.uuid -> lightScene, lightFullscreenScene
  63. var _lightScenesCache = {};
  64. var _lightFullscreenScenesCache = {};
  65. // object.material.uuid -> deferredMaterial or
  66. // object.material[ n ].uuid -> deferredMaterial
  67. var _normalDepthMaterialsCache = {};
  68. var _normalDepthShininessMaterialsCache = {};
  69. var _colorMaterialsCache = {};
  70. var _reconstructionMaterialsCache = {};
  71. // originalLight.uuid -> deferredLight
  72. var _deferredLightsCache = {};
  73. // deferredLight.uuid -> deferredLightMaterial
  74. var _classicDeferredLightMaterialsCache = {};
  75. var _lightPrePassMaterialsCache = {};
  76. var _removeThresholdCount = 60;
  77. // deferredMaterials.uuid -> object.material or
  78. // deferredMaterials.uuid -> object.material[ n ]
  79. // save before render and release after render.
  80. var _originalMaterialsTable = {};
  81. // object.uuid -> originalOnBeforeRender
  82. // save before render and release after render.
  83. var _originalOnBeforeRendersTable = {};
  84. // object.material.uuid -> object.material.visible or
  85. // object.material[ i ].uuid -> object.material[ i ].visible or
  86. // save before render and release after render.
  87. var _originalVisibleTable = {};
  88. // external properties
  89. this.renderer = undefined;
  90. this.domElement = undefined;
  91. this.forwardRendering = false; // for debug
  92. // private methods
  93. function init( parameters ) {
  94. _this.renderer = parameters.renderer !== undefined ? parameters.renderer : new THREE.WebGLRenderer();
  95. _this.domElement = _this.renderer.domElement;
  96. _gl = _this.renderer.context;
  97. _width = parameters.width !== undefined ? parameters.width : _this.renderer.getSize().width;
  98. _height = parameters.height !== undefined ? parameters.height : _this.renderer.getSize().height;
  99. var antialias = parameters.antialias !== undefined ? parameters.antialias : false;
  100. if ( parameters.cacheKeepAlive !== undefined ) _cacheKeepAlive = parameters.cacheKeepAlive;
  101. initDepthTexture();
  102. initPassNormalDepth();
  103. initPassColor();
  104. initPassLight();
  105. initPassReconstruction();
  106. initPassFinal();
  107. _this.setSize( _width, _height );
  108. _this.setAntialias( antialias );
  109. _this.enableLightPrePass( false );
  110. }
  111. function initDepthTexture() {
  112. _depthTexture = new THREE.DepthTexture(
  113. _width,
  114. _height,
  115. THREE.UnsignedInt248Type,
  116. undefined,
  117. undefined,
  118. undefined,
  119. undefined,
  120. undefined,
  121. undefined,
  122. THREE.DepthStencilFormat
  123. );
  124. }
  125. function initPassNormalDepth() {
  126. _passNormalDepth = new THREE.RenderPass();
  127. _passNormalDepth.clear = true;
  128. var rt = new THREE.WebGLRenderTarget( _width, _height, {
  129. minFilter: THREE.NearestFilter,
  130. magFilter: THREE.NearestFilter,
  131. format: THREE.RGBAFormat,
  132. type: THREE.FloatType,
  133. stencilBuffer: true,
  134. depthTexture: _depthTexture
  135. } );
  136. rt.texture.generateMipamps = false;
  137. _compNormalDepth = new THREE.EffectComposer( _this.renderer, rt );
  138. _compNormalDepth.addPass( _passNormalDepth );
  139. }
  140. function initPassColor() {
  141. _passColor = new THREE.RenderPass();
  142. _passColor.clear = true;
  143. var rt = new THREE.WebGLRenderTarget( _width, _height, {
  144. minFilter: THREE.NearestFilter,
  145. magFilter: THREE.NearestFilter,
  146. format: THREE.RGBAFormat,
  147. type: THREE.FloatType,
  148. depthTexture: _depthTexture
  149. } );
  150. rt.texture.generateMipamps = false;
  151. _compColor = new THREE.EffectComposer( _this.renderer, rt );
  152. _compColor.addPass( _passColor );
  153. }
  154. function initPassLight() {
  155. _passLightFullscreen = new THREE.RenderPass();
  156. _passLightFullscreen.clear = true;
  157. _passLightFullscreen.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  158. _passLight = new THREE.RenderPass();
  159. _passLight.clear = false;
  160. var rt = new THREE.WebGLRenderTarget( _width, _height, {
  161. minFilter: THREE.NearestFilter,
  162. magFilter: THREE.NearestFilter,
  163. format: THREE.RGBAFormat,
  164. type: THREE.FloatType,
  165. depthTexture: _depthTexture
  166. } );
  167. rt.texture.generateMipamps = false;
  168. _compLight = new THREE.EffectComposer( _this.renderer, rt );
  169. _compLight.addPass( _passLightFullscreen );
  170. _compLight.addPass( _passLight );
  171. }
  172. function initPassReconstruction() {
  173. _passReconstruction = new THREE.RenderPass();
  174. _passReconstruction.clear = true;
  175. var rt = new THREE.WebGLRenderTarget( _width, _height, {
  176. minFilter: THREE.NearestFilter,
  177. magFilter: THREE.NearestFilter,
  178. format: THREE.RGBAFormat,
  179. type: THREE.FloatType,
  180. depthTexture: _depthTexture
  181. } );
  182. rt.texture.generateMipamps = false;
  183. _compReconstruction = new THREE.EffectComposer( _this.renderer, rt );
  184. _compReconstruction.addPass( _passReconstruction );
  185. }
  186. function initPassFinal() {
  187. _passFinal = new THREE.ShaderPass( THREE.ShaderDeferred[ 'final' ] );
  188. _passFinal.clear = true;
  189. _passFinal.uniforms.samplerResult.value = _compLight.renderTarget2.texture;
  190. _passFinal.material.blending = THREE.NoBlending;
  191. _passFinal.material.depthWrite = false;
  192. _passFinal.material.depthTest = false;
  193. _passForward = new THREE.RenderPass();
  194. _passForward.clear = false;
  195. _passCopy = new THREE.ShaderPass( THREE.CopyShader );
  196. _passFXAA = new THREE.ShaderPass( THREE.FXAAShader );
  197. var rt = new THREE.WebGLRenderTarget( _width, _height, {
  198. minFilter: THREE.NearestFilter,
  199. magFilter: THREE.LinearFilter,
  200. format: THREE.RGBFormat,
  201. type: THREE.UnsignedByteType,
  202. depthTexture: _depthTexture
  203. } );
  204. rt.texture.generateMipamps = false;
  205. _compFinal = new THREE.EffectComposer( _this.renderer, rt );
  206. _compFinal.addPass( _passFinal );
  207. _compFinal.addPass( _passForward );
  208. _compFinal.addPass( _passCopy );
  209. _compFinal.addPass( _passFXAA );
  210. }
  211. function initLightScene( scene ) {
  212. var lightSceneData = _lightScenesCache[ scene.uuid ];
  213. var lightFullscreenSceneData = _lightFullscreenScenesCache[ scene.uuid ];
  214. if ( lightSceneData === undefined ) {
  215. var s = new THREE.Scene();
  216. s.userData.lights = {};
  217. lightSceneData = createCacheData();
  218. lightSceneData.scene = s;
  219. _lightScenesCache[ scene.uuid ] = lightSceneData;
  220. }
  221. if ( lightFullscreenSceneData === undefined ) {
  222. var s = new THREE.Scene();
  223. s.userData.lights = {};
  224. var emissiveLight = createDeferredEmissiveLight();
  225. s.userData.emissiveLight = emissiveLight;
  226. s.add( emissiveLight );
  227. lightFullscreenSceneData = createCacheData();
  228. lightFullscreenSceneData.scene = s;
  229. _lightFullscreenScenesCache[ scene.uuid ] = lightFullscreenSceneData;
  230. }
  231. lightSceneData.used = true;
  232. lightFullscreenSceneData.used = true;
  233. var lightScene = lightSceneData.scene;
  234. var lightFullscreenScene = lightFullscreenSceneData.scene;
  235. // emissiveLight is only for Classic Deferred Rendering
  236. lightFullscreenScene.userData.emissiveLight.visible = ! _lightPrePass;
  237. _lightScene = lightScene;
  238. _lightFullscreenScene = lightFullscreenScene;
  239. }
  240. function getMaterialFromCacheOrCreate( originalMaterial, cache, createFunc, updateFunc ) {
  241. var data = cache[ originalMaterial.uuid ];
  242. if ( data === undefined ) {
  243. data = createCacheData();
  244. data.material = createFunc( originalMaterial );
  245. cache[ originalMaterial.uuid ] = data;
  246. }
  247. data.used = true;
  248. updateFunc( data.material, originalMaterial );
  249. _originalMaterialsTable[ data.material.uuid ] = originalMaterial;
  250. return data.material;
  251. }
  252. function overrideMaterialAndOnBeforeRender( object, getMaterialFunc, onBeforeRender ) {
  253. if ( object.material === undefined ) return;
  254. if ( Array.isArray( object.material ) ) {
  255. for ( var i = 0, il = object.material.length; i < il; i ++ ) {
  256. object.material[ i ] = getMaterialFunc( object.material[ i ] );
  257. }
  258. } else {
  259. object.material = getMaterialFunc( object.material );
  260. }
  261. object.onBeforeRender = onBeforeRender;
  262. }
  263. function restoreOriginalMaterial( object ) {
  264. if ( object.material === undefined ) return;
  265. if ( Array.isArray( object.material ) ) {
  266. for ( var i = 0, il = object.material.length; i < il; i ++ ) {
  267. object.material[ i ] = _originalMaterialsTable[ object.material[ i ].uuid ];
  268. }
  269. } else {
  270. object.material = _originalMaterialsTable[ object.material.uuid ];
  271. }
  272. }
  273. function setMaterialNormalDepth( object ) {
  274. overrideMaterialAndOnBeforeRender( object, getNormalDepthMaterial, updateDeferredNormalDepthUniforms );
  275. }
  276. function getNormalDepthMaterial( originalMaterial ) {
  277. return getMaterialFromCacheOrCreate(
  278. originalMaterial,
  279. ( _lightPrePass ) ? _normalDepthShininessMaterialsCache : _normalDepthMaterialsCache,
  280. createDeferredNormalDepthMaterial,
  281. updateDeferredNormalDepthMaterial
  282. );
  283. }
  284. function createDeferredNormalDepthMaterial( originalMaterial ) {
  285. var shader = ( _lightPrePass ) ? THREE.ShaderDeferred[ 'normalDepthShininess' ] : THREE.ShaderDeferred[ 'normalDepth' ];
  286. return new THREE.ShaderMaterial( {
  287. uniforms: Object.assign( {}, shader.uniforms ),
  288. fragmentShader: shader.fragmentShader,
  289. vertexShader: shader.vertexShader,
  290. blending: THREE.NoBlending
  291. } );
  292. }
  293. function updateDeferredNormalDepthMaterial( material, originalMaterial ) {
  294. if ( originalMaterial.skinning !== undefined ) material.skinning = originalMaterial.skinning;
  295. if ( originalMaterial.morphTargets !== undefined ) material.morphTargets = originalMaterial.morphTargets;
  296. if ( originalMaterial.visible === true ) {
  297. material.visible = ! originalMaterial.transparent;
  298. } else {
  299. material.visible = false;
  300. }
  301. }
  302. function updateDeferredNormalDepthUniforms( renderer, scene, camera, geometry, material, group ) {
  303. if ( ! _lightPrePass ) return;
  304. var originalMaterial = _originalMaterialsTable[ material.uuid ];
  305. if ( originalMaterial === undefined || originalMaterial.shininess === undefined ) return;
  306. material.uniforms.shininess.value = originalMaterial.shininess;
  307. }
  308. function setMaterialColor( object ) {
  309. overrideMaterialAndOnBeforeRender( object, getColorMaterial, updateDeferredColorUniforms );
  310. }
  311. function getColorMaterial( originalMaterial ) {
  312. return getMaterialFromCacheOrCreate(
  313. originalMaterial,
  314. _colorMaterialsCache,
  315. createDeferredColorMaterial,
  316. updateDeferredColorMaterial
  317. );
  318. }
  319. function createDeferredColorMaterial( originalMaterial ) {
  320. var shader = THREE.ShaderDeferred[ 'color' ];
  321. var material = new THREE.ShaderMaterial( {
  322. uniforms: Object.assign( {}, shader.uniforms ),
  323. fragmentShader: shader.fragmentShader,
  324. vertexShader: shader.vertexShader,
  325. blending: THREE.NoBlending
  326. } );
  327. if ( originalMaterial.map !== undefined ) material.map = originalMaterial.map;
  328. return material;
  329. }
  330. function updateDeferredColorMaterial( material, originalMaterial ) {
  331. if ( originalMaterial.map !== undefined ) material.map = originalMaterial.map;
  332. if ( originalMaterial.skinning !== undefined ) material.skinning = originalMaterial.skinning;
  333. if ( originalMaterial.morphTargets !== undefined ) material.morphTargets = originalMaterial.morphTargets;
  334. if ( originalMaterial.visible === true ) {
  335. material.visible = ! originalMaterial.transparent;
  336. } else {
  337. material.visible = false;
  338. }
  339. }
  340. function updateDeferredColorUniforms( renderer, scene, camera, geometry, material, group ) {
  341. var originalMaterial = _originalMaterialsTable[ material.uuid ];
  342. var uniforms = material.uniforms;
  343. var diffuse, emissive;
  344. if ( originalMaterial.isMeshBasicMaterial === true ) {
  345. emissive = originalMaterial.color;
  346. } else {
  347. diffuse = originalMaterial.color;
  348. emissive = originalMaterial.emissive;
  349. }
  350. var specular = originalMaterial.specular;
  351. var shininess = originalMaterial.shininess;
  352. var map = originalMaterial.map;
  353. if ( diffuse !== undefined ) uniforms.diffuse.value.copy( diffuse );
  354. if ( emissive !== undefined ) uniforms.emissive.value.copy( emissive );
  355. if ( specular !== undefined ) uniforms.specular.value.copy( specular );
  356. if ( shininess !== undefined && uniforms.shininess !== undefined ) uniforms.shininess.value = shininess;
  357. if ( map !== undefined ) uniforms.map.value = map;
  358. }
  359. function setMaterialReconstruction( object ) {
  360. overrideMaterialAndOnBeforeRender( object, getReconstructionMaterial, updateDeferredReconstructionUniforms );
  361. }
  362. function getReconstructionMaterial( originalMaterial ) {
  363. if ( originalMaterial.transparent === true ) {
  364. _originalMaterialsTable[ originalMaterial.uuid ] = originalMaterial;
  365. return originalMaterial;
  366. }
  367. return getMaterialFromCacheOrCreate(
  368. originalMaterial,
  369. _reconstructionMaterialsCache,
  370. createDeferredReconstructionMaterial,
  371. updateDeferredReconstructionMaterial
  372. );
  373. }
  374. function createDeferredReconstructionMaterial( originalMaterial ) {
  375. var shader = THREE.ShaderDeferred[ 'reconstruction' ];
  376. var material = new THREE.ShaderMaterial( {
  377. uniforms: Object.assign( {}, shader.uniforms ),
  378. fragmentShader: shader.fragmentShader,
  379. vertexShader: shader.vertexShader,
  380. blending: THREE.NoBlending
  381. } );
  382. if ( originalMaterial.map !== undefined ) material.map = originalMaterial.map;
  383. return material;
  384. }
  385. function updateDeferredReconstructionMaterial( material, originalMaterial ) {
  386. updateDeferredColorMaterial( material, originalMaterial );
  387. }
  388. function updateDeferredReconstructionUniforms( renderer, scene, camera, geometry, material, group ) {
  389. if ( material.transparent === true ) {
  390. // 'this' is object here because this method is set as object.onBefore()
  391. var onBeforeRender = _originalOnBeforeRendersTable[ this.uuid ];
  392. if ( onBeforeRender ) {
  393. onBeforeRender.call( this, renderer, scene, camera, geometry, material, group );
  394. }
  395. return;
  396. }
  397. updateDeferredColorUniforms( renderer, scene, camera, geometry, material, group );
  398. material.uniforms.samplerLight.value = _compLight.renderTarget2.texture;
  399. }
  400. function setVisibleForForwardRendering( object ) {
  401. if ( object.material === undefined ) return;
  402. if ( Array.isArray( object.material ) ) {
  403. for ( var i = 0, il = object.material.length; i < il; i ++ ) {
  404. if ( _originalVisibleTable[ object.material[ i ].uuid ] === undefined ) {
  405. _originalVisibleTable[ object.material[ i ].uuid ] = object.material[ i ].visible;
  406. object.material[ i ].visible = object.material[ i ].transparent && object.material[ i ].visible;
  407. }
  408. }
  409. } else {
  410. if ( _originalVisibleTable[ object.material.uuid ] === undefined ) {
  411. _originalVisibleTable[ object.material.uuid ] = object.material.visible;
  412. object.material.visible = object.material.transparent && object.material.visible;
  413. }
  414. }
  415. }
  416. function restoreVisible( object ) {
  417. if ( object.material === undefined ) return;
  418. if ( Array.isArray( object.material ) ) {
  419. for ( var i = 0, il = object.material.length; i < il; i ++ ) {
  420. object.material[ i ].visible = _originalVisibleTable[ object.material[ i ].uuid ];
  421. }
  422. } else {
  423. object.material.visible = _originalVisibleTable[ object.material.uuid ];
  424. }
  425. }
  426. function createDeferredEmissiveLight() {
  427. var shader = THREE.ShaderDeferred[ 'emissiveLight' ];
  428. var material = new THREE.ShaderMaterial( {
  429. uniforms: Object.assign( {}, shader.uniforms ),
  430. vertexShader: shader.vertexShader,
  431. fragmentShader: shader.fragmentShader,
  432. blending: THREE.NoBlending,
  433. depthWrite: false
  434. } );
  435. var geometry = new THREE.PlaneBufferGeometry( 2, 2 );
  436. var mesh = new THREE.Mesh( geometry, material );
  437. mesh.onBeforeRender = function ( renderer, scene, camera, geometry, material, group ) {
  438. material.uniforms.samplerColor.value = _compColor.renderTarget2.texture;
  439. };
  440. return mesh;
  441. }
  442. function createDeferredLight( originalLight ) {
  443. if ( originalLight.isPointLight ) {
  444. return createDeferredPointLight( originalLight );
  445. } else if ( originalLight.isSpotLight ) {
  446. return createDeferredSpotLight( originalLight );
  447. } else if ( originalLight.isDirectionalLight ) {
  448. return createDeferredDirectionalLight( originalLight );
  449. }
  450. return null;
  451. }
  452. function createDeferredLightMaterial( originalLight ) {
  453. if ( originalLight.isPointLight ) {
  454. return createDeferredPointLightMaterial();
  455. } else if ( originalLight.isSpotLight ) {
  456. return createDeferredSpotLightMaterial();
  457. } else if ( originalLight.isDirectionalLight ) {
  458. return createDeferredDirectionalLightMaterial();
  459. }
  460. return null;
  461. }
  462. function getDeferredLightMaterial( light ) {
  463. var cache = ( _lightPrePass ) ? _lightPrePassMaterialsCache : _classicDeferredLightMaterialsCache;
  464. var data = cache[ light.uuid ];
  465. if ( data === undefined ) {
  466. data = createCacheData();
  467. data.material = createDeferredLightMaterial( light.userData.originalLight );
  468. cache[ light.uuid ] = data;
  469. }
  470. data.used = true;
  471. return data.material;
  472. }
  473. function updateDeferredLight( light ) {
  474. var originalLight = light.userData.originalLight;
  475. if ( originalLight.isPointLight ) {
  476. updateDeferredPointLight( light );
  477. }
  478. }
  479. function createDeferredLightMesh( light, geometry ) {
  480. var mesh = new THREE.Mesh( geometry, _tmpMaterial );
  481. mesh.userData.originalLight = light;
  482. return mesh;
  483. }
  484. function createDeferredLightShaderMaterial( shader ) {
  485. var material = new THREE.ShaderMaterial( {
  486. uniforms: Object.assign( {}, shader.uniforms ),
  487. vertexShader: shader.vertexShader,
  488. fragmentShader: shader.fragmentShader,
  489. transparent: true,
  490. blending: THREE.AdditiveBlending,
  491. depthWrite: false
  492. } );
  493. if ( _lightPrePass ) material.premultipliedAlpha = true;
  494. return material;
  495. }
  496. function updateDeferredLightCommonUniforms( uniforms ) {
  497. if ( _lightPrePass ) {
  498. uniforms.samplerNormalDepthShininess.value = _compNormalDepth.renderTarget2.texture;
  499. } else {
  500. uniforms.samplerNormalDepth.value = _compNormalDepth.renderTarget2.texture;
  501. uniforms.samplerColor.value = _compColor.renderTarget2.texture;
  502. }
  503. }
  504. function createDeferredPointLight( light ) {
  505. var mesh = createDeferredLightMesh( light, new THREE.SphereBufferGeometry( 1, 16, 8 ) );
  506. mesh.onBeforeRender = updateDeferredPointLightUniforms;
  507. return mesh;
  508. }
  509. /*
  510. * optimization:
  511. * Renders PointLight only back face with stencil test.
  512. */
  513. function createDeferredPointLightMaterial() {
  514. var shader = ( _lightPrePass ) ? THREE.ShaderDeferred[ 'pointLightPre' ] : THREE.ShaderDeferred[ 'pointLight' ];
  515. var material = createDeferredLightShaderMaterial( shader );
  516. material.side = THREE.BackSide;
  517. material.depthFunc = THREE.GreaterEqualDepth;
  518. return material;
  519. }
  520. function updateDeferredPointLight( light ) {
  521. var originalLight = light.userData.originalLight;
  522. var distance = originalLight.distance;
  523. if ( distance > 0 ) {
  524. light.scale.set( 1, 1, 1 ).multiplyScalar( distance );
  525. light.position.setFromMatrixPosition( originalLight.matrixWorld );
  526. }
  527. }
  528. function updateDeferredPointLightUniforms( renderer, scene, camera, geometry, material, group ) {
  529. var light = this;
  530. var originalLight = light.userData.originalLight;
  531. var distance = originalLight.distance;
  532. var uniforms = material.uniforms;
  533. uniforms.lightColor.value.copy( originalLight.color );
  534. if ( distance > 0 ) {
  535. uniforms.lightRadius.value = distance;
  536. uniforms.lightIntensity.value = originalLight.intensity;
  537. uniforms.lightPositionVS.value.setFromMatrixPosition( originalLight.matrixWorld ).applyMatrix4( _currentCamera.matrixWorldInverse );
  538. } else {
  539. uniforms.lightRadius.value = Infinity;
  540. }
  541. updateDeferredLightCommonUniforms( uniforms );
  542. }
  543. function createDeferredSpotLight( light ) {
  544. var mesh = createDeferredLightMesh( light, new THREE.PlaneBufferGeometry( 2, 2 ) );
  545. mesh.onBeforeRender = updateDeferredSpotLightUniforms;
  546. return mesh;
  547. }
  548. function createDeferredSpotLightMaterial() {
  549. var shader = ( _lightPrePass ) ? THREE.ShaderDeferred[ 'spotLightPre' ] : THREE.ShaderDeferred[ 'spotLight' ];
  550. var material = createDeferredLightShaderMaterial( shader );
  551. material.depthTest = false;
  552. return material;
  553. }
  554. function updateDeferredSpotLightUniforms( renderer, scene, camera, geometry, material, group ) {
  555. var light = this;
  556. var originalLight = light.userData.originalLight;
  557. var uniforms = light.material.uniforms;
  558. uniforms.lightAngle.value = originalLight.angle;
  559. uniforms.lightColor.value.copy( originalLight.color );
  560. uniforms.lightIntensity.value = originalLight.intensity;
  561. uniforms.lightPositionVS.value.setFromMatrixPosition( originalLight.matrixWorld ).applyMatrix4( _currentCamera.matrixWorldInverse );
  562. var vec = uniforms.lightDirectionVS.value;
  563. var vec2 = _tmpVector3;
  564. vec.setFromMatrixPosition( originalLight.matrixWorld );
  565. vec2.setFromMatrixPosition( originalLight.target.matrixWorld );
  566. vec.sub( vec2 ).normalize().transformDirection( _currentCamera.matrixWorldInverse );
  567. updateDeferredLightCommonUniforms( uniforms );
  568. }
  569. function createDeferredDirectionalLight( light ) {
  570. var mesh = createDeferredLightMesh( light, new THREE.PlaneBufferGeometry( 2, 2 ) );
  571. mesh.onBeforeRender = updateDeferredDirectionalLightUniforms;
  572. return mesh;
  573. }
  574. function createDeferredDirectionalLightMaterial() {
  575. var shader = ( _lightPrePass ) ? THREE.ShaderDeferred[ 'directionalLightPre' ] : THREE.ShaderDeferred[ 'directionalLight' ];
  576. var material = createDeferredLightShaderMaterial( shader );
  577. material.depthTest = false;
  578. return material;
  579. }
  580. function updateDeferredDirectionalLightUniforms( renderer, scene, camera, geometry, material, group ) {
  581. var light = this;
  582. var originalLight = light.userData.originalLight;
  583. var uniforms = light.material.uniforms;
  584. uniforms.lightColor.value.copy( originalLight.color );
  585. uniforms.lightIntensity.value = originalLight.intensity;
  586. var vec = uniforms.lightDirectionVS.value;
  587. var vec2 = _tmpVector3;
  588. vec.setFromMatrixPosition( originalLight.matrixWorld );
  589. vec2.setFromMatrixPosition( originalLight.target.matrixWorld );
  590. vec.sub( vec2 ).normalize().transformDirection( _currentCamera.matrixWorldInverse );
  591. updateDeferredLightCommonUniforms( uniforms );
  592. }
  593. function saveOriginalOnBeforeRenderAndCheckTransparency( object ) {
  594. if ( object.material === undefined ) return;
  595. _originalOnBeforeRendersTable[ object.uuid ] = object.onBeforeRender;
  596. // _hasTransparentObject is used only for Classic Deferred Rendering
  597. if ( _hasTransparentObject || _lightPrePass ) return;
  598. if ( ! object.visible ) return;
  599. if ( Array.isArray( object.material ) ) {
  600. for ( var i = 0, il = object.material.length; i < il; i ++ ) {
  601. if ( object.material[ i ].visible === true && object.material[ i ].transparent === true ) {
  602. _hasTransparentObject = true;
  603. break;
  604. }
  605. }
  606. } else {
  607. if ( object.material.visible === true && object.material.transparent === true ) _hasTransparentObject = true;
  608. }
  609. }
  610. function restoreOriginalOnBeforeRender( object ) {
  611. if ( object.material === undefined ) return;
  612. object.onBeforeRender = _originalOnBeforeRendersTable[ object.uuid ];
  613. }
  614. function addDeferredLightsToLightScene( object ) {
  615. if ( object.isLight !== true ) return;
  616. var data = _deferredLightsCache[ object.uuid ];
  617. if ( data === undefined ) {
  618. data = createCacheData();
  619. data.light = createDeferredLight( object );
  620. _deferredLightsCache[ object.uuid ] = data;
  621. }
  622. data.used = true;
  623. var light = data.light;
  624. if ( light === null ) return;
  625. var scene = ( object.isPointLight === true ) ? _lightScene : _lightFullscreenScene;
  626. var lights = scene.userData.lights;
  627. if ( lights[ light.uuid ] === undefined ) {
  628. scene.add( light );
  629. lights[ light.uuid ] = {
  630. light: light,
  631. found: true
  632. };
  633. }
  634. lights[ light.uuid ].found = true;
  635. }
  636. function updateDeferredLightsInLightScene( scene ) {
  637. var lights = scene.userData.lights;
  638. var keys = Object.keys( lights );
  639. for ( var i = 0, il = keys.length; i < il; i ++ ) {
  640. var key = keys[ i ];
  641. if ( lights[ key ].found === false ) {
  642. scene.remove( lights[ key ].light );
  643. delete lights[ key ];
  644. } else {
  645. var light = lights[ key ].light;
  646. light.material = getDeferredLightMaterial( light );
  647. updateDeferredLight( light );
  648. lights[ key ].found = false;
  649. }
  650. }
  651. }
  652. function updateDeferredCommonUniforms( camera ) {
  653. var uniforms = THREE.ShaderDeferredCommon[ 'commonUniforms' ];
  654. uniforms.viewWidth.value = _width;
  655. uniforms.viewHeight.value = _height;
  656. uniforms.matProjInverse.value.getInverse( camera.projectionMatrix );
  657. }
  658. function enableFinalPasses() {
  659. if ( _lightPrePass ) {
  660. _passForward.renderToScreen = false;
  661. _passForward.enabled = false;
  662. _passCopy.renderToScreen = false;
  663. _passCopy.enabled = false;
  664. if ( _antialias ) {
  665. _passFinal.renderToScreen = false;
  666. _passFXAA.renderToScreen = true;
  667. _passFXAA.enabled = true;
  668. } else {
  669. _passFinal.renderToScreen = true;
  670. _passFXAA.renderToScreen = false;
  671. _passFXAA.enabled = false;
  672. }
  673. } else {
  674. if ( _hasTransparentObject ) {
  675. if ( _antialias ) {
  676. _passFinal.renderToScreen = false;
  677. _passForward.renderToScreen = false;
  678. _passForward.enabled = true;
  679. _passCopy.renderToScreen = false;
  680. _passCopy.enabled = false;
  681. _passFXAA.renderToScreen = true;
  682. _passFXAA.enabled = true;
  683. } else {
  684. _passFinal.renderToScreen = false;
  685. _passForward.renderToScreen = false;
  686. _passForward.enabled = true;
  687. _passCopy.renderToScreen = true;
  688. _passCopy.enabled = true;
  689. _passFXAA.renderToScreen = false;
  690. _passFXAA.enabled = false;
  691. }
  692. } else {
  693. if ( _antialias ) {
  694. _passFinal.renderToScreen = false;
  695. _passForward.renderToScreen = false;
  696. _passForward.enabled = false;
  697. _passCopy.renderToScreen = false;
  698. _passCopy.enabled = false;
  699. _passFXAA.renderToScreen = true;
  700. _passFXAA.enabled = true;
  701. } else {
  702. _passFinal.renderToScreen = true;
  703. _passForward.renderToScreen = false;
  704. _passForward.enabled = false;
  705. _passCopy.renderToScreen = false;
  706. _passCopy.enabled = false;
  707. _passFXAA.renderToScreen = false;
  708. _passFXAA.enabled = false;
  709. }
  710. }
  711. }
  712. }
  713. function createCacheData() {
  714. return {
  715. used: true,
  716. keepAlive: _cacheKeepAlive,
  717. count: 0
  718. };
  719. }
  720. function cleanupCache( cache ) {
  721. var keys = Object.keys( cache );
  722. for ( var i = 0, il = keys.length; i < il; i ++ ) {
  723. var key = keys[ i ];
  724. if ( cache[ key ].used === false ) {
  725. cache[ key ].count++;
  726. if ( cache[ key ].keepAlive === false && cache[ key ].count > _removeThresholdCount ) {
  727. delete cache[ key ];
  728. }
  729. } else {
  730. cache[ key ].used = false;
  731. cache[ key ].count = 0;
  732. }
  733. }
  734. }
  735. function cleanupTable( table ) {
  736. var keys = Object.keys( table );
  737. for ( var i = 0, il = keys.length; i < il; i ++ ) {
  738. var key = keys[ i ];
  739. table[ key ] = undefined;
  740. }
  741. }
  742. function cleanupCaches() {
  743. cleanupCache( _lightScenesCache );
  744. cleanupCache( _lightFullscreenScenesCache );
  745. cleanupCache( _normalDepthMaterialsCache );
  746. cleanupCache( _normalDepthShininessMaterialsCache );
  747. cleanupCache( _colorMaterialsCache );
  748. cleanupCache( _reconstructionMaterialsCache );
  749. cleanupCache( _classicDeferredLightMaterialsCache );
  750. cleanupCache( _lightPrePassMaterialsCache );
  751. cleanupCache( _deferredLightsCache );
  752. cleanupTable( _originalMaterialsTable );
  753. cleanupTable( _originalOnBeforeRendersTable );
  754. cleanupTable( _originalVisibleTable );
  755. }
  756. /*
  757. * Classic Deferred Rendering
  758. *
  759. * 1) g-buffer normal + depth pass
  760. *
  761. * RGB: normal
  762. * A: depth
  763. *
  764. *
  765. * Light Pre-Pass Rendering
  766. *
  767. * 1') g-buffer normal + depth pass + shininess
  768. *
  769. * RG: normal
  770. * B: shininess
  771. * A: depth
  772. */
  773. function renderNormalDepth( scene, camera ) {
  774. scene.traverse( setMaterialNormalDepth );
  775. _passNormalDepth.scene = scene;
  776. _passNormalDepth.camera = camera;
  777. _this.renderer.autoClearDepth = true;
  778. _this.renderer.autoClearStencil = true;
  779. _gl.enable( _gl.STENCIL_TEST );
  780. _gl.stencilFunc( _gl.ALWAYS, 1, 0xffffffff );
  781. _gl.stencilOp( _gl.REPLACE, _gl.REPLACE, _gl.REPLACE );
  782. _compNormalDepth.render();
  783. scene.traverse( restoreOriginalMaterial );
  784. }
  785. /*
  786. * Classic Deferred Rendering
  787. *
  788. * 2) g-buffer color pass
  789. *
  790. * R: diffuse
  791. * G: emissive
  792. * B: specular
  793. * A: shininess
  794. */
  795. function renderColor( scene, camera ) {
  796. scene.traverse( setMaterialColor );
  797. _passColor.scene = scene;
  798. _passColor.camera = camera;
  799. _this.renderer.autoClearDepth = false;
  800. _this.renderer.autoClearStencil = false;
  801. _gl.stencilFunc( _gl.EQUAL, 1, 0xffffffff );
  802. _gl.stencilOp( _gl.KEEP, _gl.KEEP, _gl.KEEP );
  803. _compColor.render();
  804. scene.traverse( restoreOriginalMaterial );
  805. }
  806. /*
  807. * Classic Deferred Rendering
  808. *
  809. * 3) light pass
  810. */
  811. function renderLight( scene, camera ) {
  812. scene.traverse( addDeferredLightsToLightScene );
  813. updateDeferredLightsInLightScene( _lightScene );
  814. updateDeferredLightsInLightScene( _lightFullscreenScene );
  815. _passLight.scene = _lightScene;
  816. _passLight.camera = camera;
  817. _passLightFullscreen.scene = _lightFullscreenScene;
  818. _this.renderer.autoClearDepth = false;
  819. _this.renderer.autoClearStencil = false;
  820. _compLight.render();
  821. _gl.disable( _gl.STENCIL_TEST );
  822. }
  823. /*
  824. * Light Pre-Pass Rendering
  825. *
  826. * 2') Light pre pass
  827. */
  828. function renderLightPre( scene, camera ) {
  829. scene.traverse( addDeferredLightsToLightScene );
  830. updateDeferredLightsInLightScene( _lightScene );
  831. updateDeferredLightsInLightScene( _lightFullscreenScene );
  832. _passLight.scene = _lightScene;
  833. _passLight.camera = camera;
  834. _passLightFullscreen.scene = _lightFullscreenScene;
  835. _this.renderer.autoClearDepth = false;
  836. _this.renderer.autoClearStencil = false;
  837. _gl.stencilFunc( _gl.EQUAL, 1, 0xffffffff );
  838. _gl.stencilOp( _gl.KEEP, _gl.KEEP, _gl.KEEP );
  839. _compLight.render();
  840. }
  841. /*
  842. * Light Pre-Pass Rendering
  843. *
  844. * 3') Reconstruction pass
  845. *
  846. * Transprency handling:
  847. * Here renders transparent objects with normal forward rendering.
  848. */
  849. function renderReconstruction( scene, camera ) {
  850. scene.traverse( setMaterialReconstruction );
  851. _passReconstruction.scene = scene;
  852. _passReconstruction.camera = camera;
  853. _this.renderer.autoClearDepth = false;
  854. _this.renderer.autoClearStencil = false;
  855. _compReconstruction.render();
  856. _gl.disable( _gl.STENCIL_TEST );
  857. scene.traverse( restoreOriginalMaterial );
  858. }
  859. /*
  860. * Classic Deferred Rendering
  861. *
  862. * 4) Final pass
  863. *
  864. * transparency handling:
  865. * If there's any transparent objects, here renders them on the deferred rendering result
  866. * with normal forward rendering. This may be the easist way but heavy.
  867. * We should consider any better ways someday.
  868. *
  869. *
  870. * Light Pre-Pass Rendering
  871. *
  872. * 4') Final pass
  873. *
  874. *
  875. * Common
  876. *
  877. * antialias handling:
  878. * Here uses postprocessing FXAA for antialias.
  879. *
  880. */
  881. function renderFinal( scene, camera ) {
  882. if ( ! _lightPrePass && _hasTransparentObject ) {
  883. scene.traverse( setVisibleForForwardRendering );
  884. scene.traverse( restoreOriginalOnBeforeRender );
  885. _passForward.scene = scene;
  886. _passForward.camera = camera;
  887. }
  888. enableFinalPasses();
  889. _this.renderer.autoClearDepth = false;
  890. _this.renderer.autoClearStencil = false;
  891. _compFinal.render();
  892. if ( ! _lightPrePass && _hasTransparentObject ) {
  893. scene.traverse( restoreVisible );
  894. }
  895. }
  896. // external APIs
  897. this.setSize = function ( width, height ) {
  898. _width = width;
  899. _height = height;
  900. this.renderer.setSize( _width, _height );
  901. _compNormalDepth.setSize( _width, _height );
  902. _compColor.setSize( _width, _height );
  903. _compLight.setSize( _width, _height );
  904. _compReconstruction.setSize( _width, _height );
  905. _compFinal.setSize( _width, _height );
  906. _depthTexture.image.width = _width;
  907. _depthTexture.image.height = _height;
  908. _depthTexture.needsUpdate = true;
  909. _passFXAA.uniforms.resolution.value.set( 1 / _width, 1 / _height );
  910. };
  911. this.setAntialias = function ( enabled ) {
  912. _antialias = enabled;
  913. };
  914. this.enableLightPrePass = function ( enabled ) {
  915. _lightPrePass = enabled;
  916. _passFinal.uniforms.samplerResult.value = ( _lightPrePass ) ? _compReconstruction.renderTarget2.texture : _compLight.renderTarget2.texture;
  917. };
  918. this.render = function ( scene, camera ) {
  919. // for debug to compare with normal forward rendering
  920. if ( this.forwardRendering ) {
  921. this.renderer.render( scene, camera );
  922. return;
  923. }
  924. var currentSceneAutoUpdate = scene.autoUpdate;
  925. var currentAutoClearColor = this.renderer.autoClearColor;
  926. var currentAutoClearDepth = this.renderer.autoClearDepth;
  927. var currentAutoClearStencil = this.renderer.autoClearStencil;
  928. _currentCamera = camera;
  929. initLightScene( scene );
  930. scene.autoUpdate = false;
  931. scene.updateMatrixWorld();
  932. _hasTransparentObject = false;
  933. scene.traverse( saveOriginalOnBeforeRenderAndCheckTransparency );
  934. updateDeferredCommonUniforms( camera );
  935. renderNormalDepth( scene, camera );
  936. if ( _lightPrePass ) {
  937. renderLightPre( scene, camera );
  938. renderReconstruction( scene, camera );
  939. } else {
  940. renderColor( scene, camera );
  941. renderLight( scene, camera );
  942. }
  943. renderFinal( scene, camera );
  944. scene.traverse( restoreOriginalOnBeforeRender );
  945. cleanupCaches();
  946. scene.autoUpdate = currentSceneAutoUpdate;
  947. this.renderer.autoClearColor = currentAutoClearColor;
  948. this.renderer.autoClearDepth = currentAutoClearDepth;
  949. this.renderer.autoClearStencil = currentAutoClearStencil;
  950. };
  951. // initialize
  952. init( parameters );
  953. };
  954. THREE.DeferredShaderChunk = {
  955. packVector3: [
  956. "float vec3_to_float( vec3 data ) {",
  957. " const float unit = 255.0/256.0;",
  958. " highp float compressed = fract( data.x * unit ) + floor( data.y * unit * 255.0 ) + floor( data.z * unit * 255.0 ) * 255.0;",
  959. " return compressed;",
  960. "}"
  961. ].join( "\n" ),
  962. unpackFloat: [
  963. "vec3 float_to_vec3( float data ) {",
  964. " const float unit = 255.0;",
  965. " vec3 uncompressed;",
  966. " uncompressed.x = fract( data );",
  967. " float zInt = floor( data / unit );",
  968. " uncompressed.z = fract( zInt / unit );",
  969. " uncompressed.y = fract( floor( data - ( zInt * unit ) ) / unit );",
  970. " return uncompressed;",
  971. "}"
  972. ].join( "\n" ),
  973. // Refer to http://aras-p.info/texts/CompactNormalStorage.html
  974. packNormal: [
  975. "vec2 normal_to_vec2( vec3 normal ) {",
  976. " return normal.xy / sqrt( normal.z * 8.0 + 8.0 ) + 0.5;",
  977. "}"
  978. ].join( "\n" ),
  979. unpackVector2: [
  980. "vec3 vec2_to_normal( vec2 data ) {",
  981. " vec2 fenc = data * 4.0 - 2.0;",
  982. " float f = dot( fenc, fenc );",
  983. " float g = sqrt( 1.0 - f / 4.0 );",
  984. " vec3 normal;",
  985. " normal.xy = fenc * g;",
  986. " normal.z = 1.0 - f / 2.0;",
  987. " return normal;",
  988. "}"
  989. ].join( "\n" ),
  990. computeTextureCoord: [
  991. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );"
  992. ].join( "\n" ),
  993. packNormalDepth: [
  994. "vec4 packedNormalDepth;",
  995. "packedNormalDepth.xyz = normal * 0.5 + 0.5;",
  996. "packedNormalDepth.w = position.z / position.w;"
  997. ].join( "\n" ),
  998. unpackNormalDepth: [
  999. "vec4 normalDepthMap = texture2D( samplerNormalDepth, texCoord );",
  1000. "float depth = normalDepthMap.w;",
  1001. "if ( depth == 0.0 ) discard;",
  1002. "vec3 normal = normalDepthMap.xyz * 2.0 - 1.0;"
  1003. ].join( "\n" ),
  1004. packNormalDepthShininess: [
  1005. "vec4 packedNormalDepthShininess;",
  1006. "packedNormalDepthShininess.xy = normal_to_vec2( normal );",
  1007. "packedNormalDepthShininess.z = shininess;",
  1008. "packedNormalDepthShininess.w = position.z / position.w;"
  1009. ].join( "\n" ),
  1010. unpackNormalDepthShininess: [
  1011. "vec4 normalDepthMap = texture2D( samplerNormalDepthShininess, texCoord );",
  1012. "float depth = normalDepthMap.w;",
  1013. "if ( depth == 0.0 ) discard;",
  1014. "vec3 normal = vec2_to_normal( normalDepthMap.xy );",
  1015. "float shininess = normalDepthMap.z;"
  1016. ].join( "\n" ),
  1017. packColor: [
  1018. "vec4 packedColor;",
  1019. "packedColor.x = vec3_to_float( diffuseColor.rgb );",
  1020. "packedColor.y = vec3_to_float( emissiveColor );",
  1021. "packedColor.z = vec3_to_float( specularColor );",
  1022. "packedColor.w = shininess;"
  1023. ].join( "\n" ),
  1024. unpackColor: [
  1025. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  1026. "vec3 diffuseColor = float_to_vec3( colorMap.x );",
  1027. "vec3 emissiveColor = float_to_vec3( colorMap.y );",
  1028. "vec3 specularColor = float_to_vec3( colorMap.z );",
  1029. "float shininess = colorMap.w;"
  1030. ].join( "\n" ),
  1031. packLight: [
  1032. "vec4 packedLight;",
  1033. "packedLight.xyz = lightIntensity * lightColor * max( dot( lightVector, normal ), 0.0 ) * attenuation;",
  1034. "packedLight.w = lightIntensity * specular * max( dot( lightVector, normal ), 0.0 ) * attenuation;"
  1035. ].join( "\n" ),
  1036. computeVertexPositionVS: [
  1037. "vec2 xy = texCoord * 2.0 - 1.0;",
  1038. "vec4 vertexPositionProjected = vec4( xy, depth, 1.0 );",
  1039. "vec4 vertexPositionVS = matProjInverse * vertexPositionProjected;",
  1040. "vertexPositionVS.xyz /= vertexPositionVS.w;",
  1041. "vertexPositionVS.w = 1.0;"
  1042. ].join( "\n" ),
  1043. // TODO: calculate schlick
  1044. computeSpecular: [
  1045. "vec3 halfVector = normalize( lightVector - normalize( vertexPositionVS.xyz ) );",
  1046. "float dotNormalHalf = max( dot( normal, halfVector ), 0.0 );",
  1047. "float specular = 0.31830988618 * ( shininess * 0.5 + 1.0 ) * pow( dotNormalHalf, shininess );"
  1048. ].join( "\n" ),
  1049. combine: [
  1050. "gl_FragColor = vec4( lightIntensity * lightColor * max( dot( lightVector, normal ), 0.0 ) * ( diffuseColor + specular * specularColor ) * attenuation, 1.0 );"
  1051. ].join( "\n" )
  1052. };
  1053. THREE.ShaderDeferredCommon = {
  1054. commonUniforms: {
  1055. matProjInverse: new THREE.Uniform( new THREE.Matrix4() ),
  1056. viewWidth: new THREE.Uniform( 800 ),
  1057. viewHeight: new THREE.Uniform( 600 )
  1058. }
  1059. };
  1060. THREE.ShaderDeferred = {
  1061. normalDepth: {
  1062. uniforms: {},
  1063. vertexShader: [
  1064. "varying vec3 vNormal;",
  1065. "varying vec4 vPosition;",
  1066. "#include <morphtarget_pars_vertex>",
  1067. "#include <skinning_pars_vertex>",
  1068. "void main() {",
  1069. "#include <begin_vertex>",
  1070. "#include <beginnormal_vertex>",
  1071. "#include <skinbase_vertex>",
  1072. "#include <skinnormal_vertex>",
  1073. "#include <defaultnormal_vertex>",
  1074. "#include <morphtarget_vertex>",
  1075. "#include <skinning_vertex>",
  1076. "#include <project_vertex>",
  1077. " vNormal = normalize( transformedNormal );",
  1078. " vPosition = gl_Position;",
  1079. "}"
  1080. ].join( "\n" ),
  1081. fragmentShader: [
  1082. "varying vec3 vNormal;",
  1083. "varying vec4 vPosition;",
  1084. "void main() {",
  1085. " vec3 normal = vNormal;",
  1086. " vec4 position = vPosition;",
  1087. THREE.DeferredShaderChunk[ "packNormalDepth" ],
  1088. " gl_FragColor = packedNormalDepth;",
  1089. "}"
  1090. ].join( "\n" )
  1091. },
  1092. color: {
  1093. uniforms: {
  1094. map: new THREE.Uniform( null ),
  1095. offsetRepeat: new THREE.Uniform( new THREE.Vector4( 0, 0, 1, 1 ) ),
  1096. diffuse: new THREE.Uniform( new THREE.Color( 0x000000 ) ),
  1097. emissive: new THREE.Uniform( new THREE.Color( 0x000000 ) ),
  1098. specular: new THREE.Uniform( new THREE.Color( 0x000000 ) ),
  1099. shininess: new THREE.Uniform( 30.0 )
  1100. },
  1101. vertexShader: [
  1102. "#include <uv_pars_vertex>",
  1103. "#include <morphtarget_pars_vertex>",
  1104. "#include <skinning_pars_vertex>",
  1105. "void main() {",
  1106. "#include <uv_vertex>",
  1107. "#include <begin_vertex>",
  1108. "#include <beginnormal_vertex>",
  1109. "#include <skinbase_vertex>",
  1110. "#include <skinnormal_vertex>",
  1111. "#include <defaultnormal_vertex>",
  1112. "#include <morphtarget_vertex>",
  1113. "#include <skinning_vertex>",
  1114. "#include <project_vertex>",
  1115. "}"
  1116. ].join( "\n" ),
  1117. fragmentShader: [
  1118. "uniform vec3 diffuse;",
  1119. "uniform vec3 emissive;",
  1120. "uniform vec3 specular;",
  1121. "uniform float shininess;",
  1122. "#include <uv_pars_fragment>",
  1123. "#include <map_pars_fragment>",
  1124. THREE.DeferredShaderChunk[ "packVector3" ],
  1125. "void main() {",
  1126. " vec4 diffuseColor = vec4( diffuse, 1.0 );",
  1127. " vec3 emissiveColor = emissive;",
  1128. " vec3 specularColor = specular;",
  1129. "#include <map_fragment>",
  1130. THREE.DeferredShaderChunk[ "packColor" ],
  1131. " gl_FragColor = packedColor;",
  1132. "}"
  1133. ].join( "\n" )
  1134. },
  1135. emissiveLight: {
  1136. uniforms: Object.assign(
  1137. {
  1138. samplerColor: new THREE.Uniform( null )
  1139. },
  1140. THREE.ShaderDeferredCommon[ 'commonUniforms' ]
  1141. ),
  1142. vertexShader: [
  1143. "void main() { ",
  1144. " gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  1145. "}"
  1146. ].join( '\n' ),
  1147. fragmentShader: [
  1148. "uniform sampler2D samplerColor;",
  1149. "uniform float viewHeight;",
  1150. "uniform float viewWidth;",
  1151. THREE.DeferredShaderChunk[ "unpackFloat" ],
  1152. "void main() {",
  1153. THREE.DeferredShaderChunk[ "computeTextureCoord" ],
  1154. THREE.DeferredShaderChunk[ "unpackColor" ],
  1155. " gl_FragColor = vec4( emissiveColor, 1.0 );",
  1156. "}"
  1157. ].join( '\n' )
  1158. },
  1159. pointLight: {
  1160. uniforms: Object.assign(
  1161. {
  1162. samplerNormalDepth: new THREE.Uniform( null ),
  1163. samplerColor: new THREE.Uniform( null ),
  1164. lightColor: new THREE.Uniform( new THREE.Color( 0x000000 ) ),
  1165. lightPositionVS: new THREE.Uniform( new THREE.Vector3( 0, 1, 0 ) ),
  1166. lightIntensity: new THREE.Uniform( 1.0 ),
  1167. lightRadius: new THREE.Uniform( 1.0 )
  1168. },
  1169. THREE.ShaderDeferredCommon[ 'commonUniforms' ]
  1170. ),
  1171. vertexShader: [
  1172. "void main() {",
  1173. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  1174. "}"
  1175. ].join( "\n" ),
  1176. fragmentShader: [
  1177. "uniform sampler2D samplerNormalDepth;",
  1178. "uniform sampler2D samplerColor;",
  1179. "uniform float viewHeight;",
  1180. "uniform float viewWidth;",
  1181. "uniform vec3 lightColor;",
  1182. "uniform vec3 lightPositionVS;",
  1183. "uniform float lightIntensity;",
  1184. "uniform float lightRadius;",
  1185. "uniform mat4 matProjInverse;",
  1186. THREE.DeferredShaderChunk[ "unpackFloat" ],
  1187. "void main() {",
  1188. THREE.DeferredShaderChunk[ "computeTextureCoord" ],
  1189. THREE.DeferredShaderChunk[ "unpackNormalDepth" ],
  1190. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  1191. " vec3 lightVector = lightPositionVS - vertexPositionVS.xyz;",
  1192. " float distance = length( lightVector );",
  1193. " if ( distance > lightRadius ) discard;",
  1194. " lightVector = normalize( lightVector );",
  1195. THREE.DeferredShaderChunk[ "unpackColor" ],
  1196. THREE.DeferredShaderChunk[ "computeSpecular" ],
  1197. " //float cutoff = 0.3;",
  1198. " //float denom = distance / lightRadius + 1.0;",
  1199. " //float attenuation = 1.0 / ( denom * denom );",
  1200. " //attenuation = ( attenuation - cutoff ) / ( 1.0 - cutoff );",
  1201. " //attenuation = max( attenuation, 0.0 );",
  1202. " //attenuation *= attenuation;",
  1203. " //diffuseColor *= saturate( -distance / lightRadius + 1.0 );",
  1204. " //float attenuation = 1.0;",
  1205. " float attenuation = saturate( -distance / lightRadius + 1.0 );",
  1206. THREE.DeferredShaderChunk[ "combine" ],
  1207. "}"
  1208. ].join( "\n" )
  1209. },
  1210. spotLight: {
  1211. uniforms: Object.assign(
  1212. {
  1213. samplerNormalDepth: new THREE.Uniform( null ),
  1214. samplerColor: new THREE.Uniform( null ),
  1215. lightColor: new THREE.Uniform( new THREE.Color( 0x000000 ) ),
  1216. lightDirectionVS: new THREE.Uniform( new THREE.Vector3( 0, 1, 0 ) ),
  1217. lightPositionVS: new THREE.Uniform( new THREE.Vector3( 0, 1, 0 ) ),
  1218. lightAngle: new THREE.Uniform( 1.0 ),
  1219. lightIntensity: new THREE.Uniform( 1.0 )
  1220. },
  1221. THREE.ShaderDeferredCommon[ 'commonUniforms' ]
  1222. ),
  1223. vertexShader: [
  1224. "void main() { ",
  1225. " gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  1226. "}"
  1227. ].join( "\n" ),
  1228. fragmentShader: [
  1229. "uniform sampler2D samplerNormalDepth;",
  1230. "uniform sampler2D samplerColor;",
  1231. "uniform float viewHeight;",
  1232. "uniform float viewWidth;",
  1233. "uniform vec3 lightColor;",
  1234. "uniform vec3 lightPositionVS;",
  1235. "uniform vec3 lightDirectionVS;",
  1236. "uniform float lightAngle;",
  1237. "uniform float lightIntensity;",
  1238. "uniform mat4 matProjInverse;",
  1239. THREE.DeferredShaderChunk[ "unpackFloat" ],
  1240. "void main() {",
  1241. THREE.DeferredShaderChunk[ "computeTextureCoord" ],
  1242. THREE.DeferredShaderChunk[ "unpackNormalDepth" ],
  1243. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  1244. THREE.DeferredShaderChunk[ "unpackColor" ],
  1245. " vec3 lightVector = normalize( lightPositionVS.xyz - vertexPositionVS.xyz );",
  1246. " float rho = dot( lightDirectionVS, lightVector );",
  1247. " float rhoMax = cos( lightAngle );",
  1248. " if ( rho <= rhoMax ) discard;",
  1249. " float theta = rhoMax + 0.0001;",
  1250. " float phi = rhoMax + 0.05;",
  1251. " float falloff = 4.0;",
  1252. " float spot = 0.0;",
  1253. " if ( rho >= phi ) {",
  1254. " spot = 1.0;",
  1255. " } else if ( rho <= theta ) {",
  1256. " spot = 0.0;",
  1257. " } else { ",
  1258. " spot = pow( ( rho - theta ) / ( phi - theta ), falloff );",
  1259. " }",
  1260. " diffuseColor *= spot;",
  1261. THREE.DeferredShaderChunk[ "computeSpecular" ],
  1262. " const float attenuation = 1.0;",
  1263. THREE.DeferredShaderChunk[ "combine" ],
  1264. "}"
  1265. ].join( "\n" )
  1266. },
  1267. directionalLight: {
  1268. uniforms: Object.assign(
  1269. {
  1270. samplerNormalDepth: new THREE.Uniform( null ),
  1271. samplerColor: new THREE.Uniform( null ),
  1272. lightColor: new THREE.Uniform( new THREE.Color( 0x000000 ) ),
  1273. lightDirectionVS: new THREE.Uniform( new THREE.Vector3( 0, 1, 0 ) ),
  1274. lightIntensity: new THREE.Uniform( 1.0 )
  1275. },
  1276. THREE.ShaderDeferredCommon[ 'commonUniforms' ]
  1277. ),
  1278. vertexShader: [
  1279. "void main() { ",
  1280. " gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  1281. "}"
  1282. ].join( '\n' ),
  1283. fragmentShader: [
  1284. "uniform sampler2D samplerNormalDepth;",
  1285. "uniform sampler2D samplerColor;",
  1286. "uniform float viewHeight;",
  1287. "uniform float viewWidth;",
  1288. "uniform vec3 lightColor;",
  1289. "uniform vec3 lightDirectionVS;",
  1290. "uniform float lightIntensity;",
  1291. "uniform mat4 matProjInverse;",
  1292. THREE.DeferredShaderChunk[ "unpackFloat" ],
  1293. "void main() {",
  1294. THREE.DeferredShaderChunk[ "computeTextureCoord" ],
  1295. THREE.DeferredShaderChunk[ "unpackNormalDepth" ],
  1296. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  1297. THREE.DeferredShaderChunk[ "unpackColor" ],
  1298. " vec3 lightVector = normalize( lightDirectionVS );",
  1299. THREE.DeferredShaderChunk[ "computeSpecular" ],
  1300. " const float attenuation = 1.0;",
  1301. THREE.DeferredShaderChunk[ "combine" ],
  1302. "}"
  1303. ].join( '\n' )
  1304. },
  1305. normalDepthShininess: {
  1306. uniforms: {
  1307. shininess: new THREE.Uniform( 30.0 )
  1308. },
  1309. vertexShader: [
  1310. "varying vec3 vNormal;",
  1311. "varying vec4 vPosition;",
  1312. "#include <morphtarget_pars_vertex>",
  1313. "#include <skinning_pars_vertex>",
  1314. "void main() {",
  1315. "#include <begin_vertex>",
  1316. "#include <beginnormal_vertex>",
  1317. "#include <skinbase_vertex>",
  1318. "#include <skinnormal_vertex>",
  1319. "#include <defaultnormal_vertex>",
  1320. "#include <morphtarget_vertex>",
  1321. "#include <skinning_vertex>",
  1322. "#include <project_vertex>",
  1323. " vNormal = normalize( transformedNormal );",
  1324. " vPosition = gl_Position;",
  1325. "}"
  1326. ].join( "\n" ),
  1327. fragmentShader: [
  1328. "varying vec3 vNormal;",
  1329. "varying vec4 vPosition;",
  1330. "uniform float shininess;",
  1331. THREE.DeferredShaderChunk[ "packNormal" ],
  1332. "void main() {",
  1333. " vec3 normal = vNormal;",
  1334. " vec4 position = vPosition;",
  1335. THREE.DeferredShaderChunk[ "packNormalDepthShininess" ],
  1336. " gl_FragColor = packedNormalDepthShininess;",
  1337. "}"
  1338. ].join( "\n" )
  1339. },
  1340. pointLightPre: {
  1341. uniforms: Object.assign(
  1342. {
  1343. samplerNormalDepthShininess: new THREE.Uniform( null ),
  1344. lightColor: new THREE.Uniform( new THREE.Color( 0x000000 ) ),
  1345. lightPositionVS: new THREE.Uniform( new THREE.Vector3( 0, 1, 0 ) ),
  1346. lightIntensity: new THREE.Uniform( 1.0 ),
  1347. lightRadius: new THREE.Uniform( 1.0 )
  1348. },
  1349. THREE.ShaderDeferredCommon[ 'commonUniforms' ]
  1350. ),
  1351. vertexShader: [
  1352. "void main() {",
  1353. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  1354. "}"
  1355. ].join( "\n" ),
  1356. fragmentShader: [
  1357. "uniform sampler2D samplerNormalDepthShininess;",
  1358. "uniform float viewHeight;",
  1359. "uniform float viewWidth;",
  1360. "uniform vec3 lightColor;",
  1361. "uniform vec3 lightPositionVS;",
  1362. "uniform float lightIntensity;",
  1363. "uniform float lightRadius;",
  1364. "uniform mat4 matProjInverse;",
  1365. THREE.DeferredShaderChunk[ "unpackFloat" ],
  1366. THREE.DeferredShaderChunk[ "unpackVector2" ],
  1367. "void main() {",
  1368. THREE.DeferredShaderChunk[ "computeTextureCoord" ],
  1369. THREE.DeferredShaderChunk[ "unpackNormalDepthShininess" ],
  1370. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  1371. " vec3 lightVector = lightPositionVS - vertexPositionVS.xyz;",
  1372. " float distance = length( lightVector );",
  1373. " if ( distance > lightRadius ) discard;",
  1374. " lightVector = normalize( lightVector );",
  1375. THREE.DeferredShaderChunk[ "computeSpecular" ],
  1376. " float attenuation = saturate( -distance / lightRadius + 1.0 );",
  1377. THREE.DeferredShaderChunk[ "packLight" ],
  1378. " gl_FragColor = packedLight;",
  1379. "}"
  1380. ].join( "\n" )
  1381. },
  1382. spotLightPre: {
  1383. uniforms: Object.assign(
  1384. {
  1385. samplerNormalDepthShininess: new THREE.Uniform( null ),
  1386. lightColor: new THREE.Uniform( new THREE.Color( 0x000000 ) ),
  1387. lightDirectionVS: new THREE.Uniform( new THREE.Vector3( 0, 1, 0 ) ),
  1388. lightPositionVS: new THREE.Uniform( new THREE.Vector3( 0, 1, 0 ) ),
  1389. lightAngle: new THREE.Uniform( 1.0 ),
  1390. lightIntensity: new THREE.Uniform( 1.0 )
  1391. },
  1392. THREE.ShaderDeferredCommon[ 'commonUniforms' ]
  1393. ),
  1394. vertexShader: [
  1395. "void main() { ",
  1396. " gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  1397. "}"
  1398. ].join( "\n" ),
  1399. fragmentShader: [
  1400. "uniform sampler2D samplerNormalDepthShininess;",
  1401. "uniform float viewHeight;",
  1402. "uniform float viewWidth;",
  1403. "uniform vec3 lightColor;",
  1404. "uniform vec3 lightPositionVS;",
  1405. "uniform vec3 lightDirectionVS;",
  1406. "uniform float lightAngle;",
  1407. "uniform float lightIntensity;",
  1408. "uniform mat4 matProjInverse;",
  1409. THREE.DeferredShaderChunk[ "unpackFloat" ],
  1410. THREE.DeferredShaderChunk[ "unpackVector2" ],
  1411. "void main() {",
  1412. THREE.DeferredShaderChunk[ "computeTextureCoord" ],
  1413. THREE.DeferredShaderChunk[ "unpackNormalDepthShininess" ],
  1414. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  1415. " vec3 lightVector = normalize( lightPositionVS.xyz - vertexPositionVS.xyz );",
  1416. " float rho = dot( lightDirectionVS, lightVector );",
  1417. " float rhoMax = cos( lightAngle );",
  1418. " if ( rho <= rhoMax ) discard;",
  1419. " float theta = rhoMax + 0.0001;",
  1420. " float phi = rhoMax + 0.05;",
  1421. " float falloff = 4.0;",
  1422. " float spot = 0.0;",
  1423. " if ( rho >= phi ) {",
  1424. " spot = 1.0;",
  1425. " } else if ( rho <= theta ) {",
  1426. " spot = 0.0;",
  1427. " } else { ",
  1428. " spot = pow( ( rho - theta ) / ( phi - theta ), falloff );",
  1429. " }",
  1430. THREE.DeferredShaderChunk[ "computeSpecular" ],
  1431. " const float attenuation = 1.0;",
  1432. THREE.DeferredShaderChunk[ "packLight" ],
  1433. " gl_FragColor = spot * packedLight;",
  1434. "}"
  1435. ].join( "\n" )
  1436. },
  1437. directionalLightPre: {
  1438. uniforms: Object.assign(
  1439. {
  1440. samplerNormalDepthShininess: new THREE.Uniform( null ),
  1441. lightColor: new THREE.Uniform( new THREE.Color( 0x000000 ) ),
  1442. lightDirectionVS: new THREE.Uniform( new THREE.Vector3( 0, 1, 0 ) ),
  1443. lightIntensity: new THREE.Uniform( 1.0 )
  1444. },
  1445. THREE.ShaderDeferredCommon[ 'commonUniforms' ]
  1446. ),
  1447. vertexShader: [
  1448. "void main() { ",
  1449. " gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  1450. "}"
  1451. ].join( '\n' ),
  1452. fragmentShader: [
  1453. "uniform sampler2D samplerNormalDepthShininess;",
  1454. "uniform float viewHeight;",
  1455. "uniform float viewWidth;",
  1456. "uniform vec3 lightColor;",
  1457. "uniform vec3 lightDirectionVS;",
  1458. "uniform float lightIntensity;",
  1459. "uniform mat4 matProjInverse;",
  1460. THREE.DeferredShaderChunk[ "unpackFloat" ],
  1461. THREE.DeferredShaderChunk[ "unpackVector2" ],
  1462. "void main() {",
  1463. THREE.DeferredShaderChunk[ "computeTextureCoord" ],
  1464. THREE.DeferredShaderChunk[ "unpackNormalDepthShininess" ],
  1465. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  1466. " vec3 lightVector = normalize( lightDirectionVS );",
  1467. THREE.DeferredShaderChunk[ "computeSpecular" ],
  1468. " const float attenuation = 1.0;",
  1469. THREE.DeferredShaderChunk[ "packLight" ],
  1470. " gl_FragColor = packedLight;",
  1471. "}"
  1472. ].join( '\n' )
  1473. },
  1474. reconstruction: {
  1475. uniforms: Object.assign(
  1476. {
  1477. samplerLight: new THREE.Uniform( null ),
  1478. map: new THREE.Uniform( null ),
  1479. offsetRepeat: new THREE.Uniform( new THREE.Vector4( 0, 0, 1, 1 ) ),
  1480. diffuse: new THREE.Uniform( new THREE.Color( 0x000000 ) ),
  1481. emissive: new THREE.Uniform( new THREE.Color( 0x000000 ) ),
  1482. specular: new THREE.Uniform( new THREE.Color( 0x000000 ) ),
  1483. shininess: new THREE.Uniform( 30.0 )
  1484. },
  1485. THREE.ShaderDeferredCommon[ 'commonUniforms' ]
  1486. ),
  1487. vertexShader: [
  1488. "#include <uv_pars_vertex>",
  1489. "#include <morphtarget_pars_vertex>",
  1490. "#include <skinning_pars_vertex>",
  1491. "void main() {",
  1492. "#include <uv_vertex>",
  1493. "#include <begin_vertex>",
  1494. "#include <beginnormal_vertex>",
  1495. "#include <skinbase_vertex>",
  1496. "#include <skinnormal_vertex>",
  1497. "#include <defaultnormal_vertex>",
  1498. "#include <morphtarget_vertex>",
  1499. "#include <skinning_vertex>",
  1500. "#include <project_vertex>",
  1501. "}"
  1502. ].join( "\n" ),
  1503. fragmentShader: [
  1504. "uniform sampler2D samplerLight;",
  1505. "uniform vec3 diffuse;",
  1506. "uniform vec3 emissive;",
  1507. "uniform vec3 specular;",
  1508. "uniform float shininess;",
  1509. "uniform float viewHeight;",
  1510. "uniform float viewWidth;",
  1511. "#include <uv_pars_fragment>",
  1512. "#include <map_pars_fragment>",
  1513. THREE.DeferredShaderChunk[ "unpackFloat" ],
  1514. "void main() {",
  1515. " vec4 diffuseColor = vec4( diffuse, 1.0 );",
  1516. " vec3 emissiveColor = emissive;",
  1517. " vec3 specularColor = specular;",
  1518. THREE.DeferredShaderChunk[ "computeTextureCoord" ],
  1519. " vec4 light = texture2D( samplerLight, texCoord );",
  1520. "#include <map_fragment>",
  1521. " vec3 diffuseFinal = diffuseColor.rgb * light.rgb;",
  1522. " vec3 emissiveFinal = emissiveColor;",
  1523. " vec3 specularFinal = specularColor * light.rgb * light.a;",
  1524. " gl_FragColor = vec4( diffuseFinal + emissiveFinal + specularFinal, 1.0 );",
  1525. "}"
  1526. ].join( "\n" )
  1527. },
  1528. // TODO: implement tone mapping
  1529. final: {
  1530. uniforms: {
  1531. samplerResult: new THREE.Uniform( null )
  1532. },
  1533. vertexShader: [
  1534. "varying vec2 texCoord;",
  1535. "void main() {",
  1536. " vec4 pos = vec4( sign( position.xy ), 0.0, 1.0 );",
  1537. " texCoord = pos.xy * vec2( 0.5 ) + 0.5;",
  1538. " gl_Position = pos;",
  1539. "}"
  1540. ].join( "\n" ),
  1541. fragmentShader: [
  1542. "varying vec2 texCoord;",
  1543. "uniform sampler2D samplerResult;",
  1544. "void main() {",
  1545. " gl_FragColor = texture2D( samplerResult, texCoord );",
  1546. "}"
  1547. ].join( "\n" )
  1548. }
  1549. };