WebGLDeferredRenderer.js 55 KB

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