WebGLDeferredRenderer.js 54 KB

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