WebGLDeferredRenderer.js 54 KB

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