WebGLDeferredRenderer.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author MPanknin / http://www.redplant.de/
  4. */
  5. THREE.WebGLDeferredRenderer = function ( parameters ) {
  6. var _this = this;
  7. var fullWidth = parameters.width !== undefined ? parameters.width : 800;
  8. var fullHeight = parameters.height !== undefined ? parameters.height : 600;
  9. var currentScale = parameters.scale !== undefined ? parameters.scale : 1;
  10. var scaledWidth = Math.floor( currentScale * fullWidth );
  11. var scaledHeight = Math.floor( currentScale * fullHeight );
  12. var brightness = parameters.brightness !== undefined ? parameters.brightness : 1;
  13. var antialias = parameters.antialias !== undefined ? parameters.antialias : false;
  14. this.renderer = parameters.renderer;
  15. if ( this.renderer === undefined ) {
  16. this.renderer = new THREE.WebGLRenderer( { alpha: false, antialias: false } );
  17. this.renderer.setSize( fullWidth, fullHeight );
  18. this.renderer.setClearColorHex( 0x000000, 0 );
  19. this.renderer.autoClear = false;
  20. }
  21. this.domElement = this.renderer.domElement;
  22. //
  23. var gl = this.renderer.context;
  24. //
  25. var positionVS = new THREE.Vector3();
  26. var directionVS = new THREE.Vector3();
  27. var right = new THREE.Vector3();
  28. var normal = new THREE.Vector3();
  29. //
  30. var geometryLightSphere = new THREE.SphereGeometry( 1, 16, 8 );
  31. var geometryLightPlane = new THREE.PlaneGeometry( 2, 2 );
  32. var black = new THREE.Color( 0x000000 );
  33. var colorShader = THREE.ShaderDeferred[ "color" ];
  34. var normalDepthShader = THREE.ShaderDeferred[ "normalDepth" ];
  35. //
  36. var emissiveLightShader = THREE.ShaderDeferred[ "emissiveLight" ];
  37. var pointLightShader = THREE.ShaderDeferred[ "pointLight" ];
  38. var spotLightShader = THREE.ShaderDeferred[ "spotLight" ];
  39. var directionalLightShader = THREE.ShaderDeferred[ "directionalLight" ];
  40. var hemisphereLightShader = THREE.ShaderDeferred[ "hemisphereLight" ];
  41. var areaLightShader = THREE.ShaderDeferred[ "areaLight" ];
  42. var compositeShader = THREE.ShaderDeferred[ "composite" ];
  43. //
  44. var compColor, compNormal, compDepth, compLight, compFinal;
  45. var passColor, passNormal, passDepth, passLightFullscreen, passLightProxy, compositePass;
  46. var effectFXAA;
  47. //
  48. var lightSceneFullscreen, lightSceneProxy;
  49. //
  50. var resizableMaterials = [];
  51. //
  52. var invisibleMaterial = new THREE.ShaderMaterial();
  53. invisibleMaterial.visible = false;
  54. var defaultNormalDepthMaterial = new THREE.ShaderMaterial( {
  55. uniforms: THREE.UniformsUtils.clone( normalDepthShader.uniforms ),
  56. vertexShader: normalDepthShader.vertexShader,
  57. fragmentShader: normalDepthShader.fragmentShader,
  58. blending: THREE.NoBlending
  59. } );
  60. //
  61. var initDeferredMaterials = function ( object ) {
  62. if ( object.material instanceof THREE.MeshFaceMaterial ) {
  63. var colorMaterials = [];
  64. var normalDepthMaterials = [];
  65. var materials = object.material.materials;
  66. for ( var i = 0, il = materials.length; i < il; i ++ ) {
  67. var deferredMaterials = createDeferredMaterials( materials[ i ] );
  68. if ( deferredMaterials.transparent ) {
  69. colorMaterials.push( invisibleMaterial );
  70. normalDepthMaterials.push( invisibleMaterial );
  71. } else {
  72. colorMaterials.push( deferredMaterials.colorMaterial );
  73. normalDepthMaterials.push( deferredMaterials.normalDepthMaterial );
  74. }
  75. }
  76. object.properties.colorMaterial = new THREE.MeshFaceMaterial( colorMaterials );
  77. object.properties.normalDepthMaterial = new THREE.MeshFaceMaterial( normalDepthMaterials );
  78. } else {
  79. var deferredMaterials = createDeferredMaterials( object.material );
  80. object.properties.colorMaterial = deferredMaterials.colorMaterial;
  81. object.properties.normalDepthMaterial = deferredMaterials.normalDepthMaterial;
  82. object.properties.transparent = deferredMaterials.transparent;
  83. }
  84. };
  85. var createDeferredMaterials = function ( originalMaterial ) {
  86. var deferredMaterials = {};
  87. // color material
  88. // -----------------
  89. // diffuse color
  90. // specular color
  91. // shininess
  92. // diffuse map
  93. // vertex colors
  94. // alphaTest
  95. // morphs
  96. var uniforms = THREE.UniformsUtils.clone( colorShader.uniforms );
  97. var defines = { "USE_MAP": !! originalMaterial.map, "USE_ENVMAP": !! originalMaterial.envMap, "GAMMA_INPUT": true };
  98. var material = new THREE.ShaderMaterial( {
  99. fragmentShader: colorShader.fragmentShader,
  100. vertexShader: colorShader.vertexShader,
  101. uniforms: uniforms,
  102. defines: defines,
  103. shading: originalMaterial.shading
  104. } );
  105. if ( originalMaterial instanceof THREE.MeshBasicMaterial ) {
  106. var diffuse = black;
  107. var emissive = originalMaterial.color;
  108. } else {
  109. var diffuse = originalMaterial.color;
  110. var emissive = originalMaterial.emissive !== undefined ? originalMaterial.emissive : black;
  111. }
  112. var specular = originalMaterial.specular !== undefined ? originalMaterial.specular : black;
  113. var shininess = originalMaterial.shininess !== undefined ? originalMaterial.shininess : 1;
  114. var wrapAround = originalMaterial.wrapAround !== undefined ? ( originalMaterial.wrapAround ? -1 : 1 ) : 1;
  115. var additiveSpecular = originalMaterial.metal !== undefined ? ( originalMaterial.metal ? 1 : -1 ) : -1;
  116. uniforms.emissive.value.copyGammaToLinear( emissive );
  117. uniforms.diffuse.value.copyGammaToLinear( diffuse );
  118. uniforms.specular.value.copyGammaToLinear( specular );
  119. uniforms.shininess.value = shininess;
  120. uniforms.wrapAround.value = wrapAround;
  121. uniforms.additiveSpecular.value = additiveSpecular;
  122. uniforms.map.value = originalMaterial.map;
  123. if ( originalMaterial.envMap ) {
  124. uniforms.envMap.value = originalMaterial.envMap;
  125. uniforms.useRefract.value = originalMaterial.envMap.mapping instanceof THREE.CubeRefractionMapping;
  126. uniforms.refractionRatio.value = originalMaterial.refractionRatio;
  127. uniforms.combine.value = originalMaterial.combine;
  128. uniforms.reflectivity.value = originalMaterial.reflectivity;
  129. uniforms.flipEnvMap.value = ( originalMaterial.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : -1;
  130. uniforms.samplerNormalDepth.value = compNormalDepth.renderTarget2;
  131. uniforms.viewWidth.value = scaledWidth;
  132. uniforms.viewHeight.value = scaledHeight;
  133. resizableMaterials.push( material );
  134. }
  135. material.vertexColors = originalMaterial.vertexColors;
  136. material.morphTargets = originalMaterial.morphTargets;
  137. material.morphNormals = originalMaterial.morphNormals;
  138. material.skinning = originalMaterial.skinning;
  139. material.alphaTest = originalMaterial.alphaTest;
  140. material.wireframe = originalMaterial.wireframe;
  141. // uv repeat and offset setting priorities
  142. // 1. color map
  143. // 2. specular map
  144. // 3. normal map
  145. // 4. bump map
  146. var uvScaleMap;
  147. if ( originalMaterial.map ) {
  148. uvScaleMap = originalMaterial.map;
  149. } else if ( originalMaterial.specularMap ) {
  150. uvScaleMap = originalMaterial.specularMap;
  151. } else if ( originalMaterial.normalMap ) {
  152. uvScaleMap = originalMaterial.normalMap;
  153. } else if ( originalMaterial.bumpMap ) {
  154. uvScaleMap = originalMaterial.bumpMap;
  155. }
  156. if ( uvScaleMap !== undefined ) {
  157. var offset = uvScaleMap.offset;
  158. var repeat = uvScaleMap.repeat;
  159. uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
  160. }
  161. deferredMaterials.colorMaterial = material;
  162. // normal + depth material
  163. // -----------------
  164. // vertex normals
  165. // morph normals
  166. // bump map
  167. // bump scale
  168. // clip depth
  169. if ( originalMaterial.morphTargets || originalMaterial.skinning || originalMaterial.bumpMap ) {
  170. var uniforms = THREE.UniformsUtils.clone( normalDepthShader.uniforms );
  171. var defines = { "USE_BUMPMAP": !!originalMaterial.bumpMap };
  172. var normalDepthMaterial = new THREE.ShaderMaterial( {
  173. uniforms: uniforms,
  174. vertexShader: normalDepthShader.vertexShader,
  175. fragmentShader: normalDepthShader.fragmentShader,
  176. shading: originalMaterial.shading,
  177. defines: defines,
  178. blending: THREE.NoBlending
  179. } );
  180. normalDepthMaterial.morphTargets = originalMaterial.morphTargets;
  181. normalDepthMaterial.morphNormals = originalMaterial.morphNormals;
  182. normalDepthMaterial.skinning = originalMaterial.skinning;
  183. if ( originalMaterial.bumpMap ) {
  184. uniforms.bumpMap.value = originalMaterial.bumpMap;
  185. uniforms.bumpScale.value = originalMaterial.bumpScale;
  186. var offset = originalMaterial.bumpMap.offset;
  187. var repeat = originalMaterial.bumpMap.repeat;
  188. uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
  189. }
  190. } else {
  191. var normalDepthMaterial = defaultNormalDepthMaterial.clone();
  192. }
  193. normalDepthMaterial.wireframe = originalMaterial.wireframe;
  194. normalDepthMaterial.vertexColors = originalMaterial.vertexColors;
  195. deferredMaterials.normalDepthMaterial = normalDepthMaterial;
  196. //
  197. deferredMaterials.transparent = originalMaterial.transparent;
  198. return deferredMaterials;
  199. };
  200. var updatePointLightProxy = function ( lightProxy ) {
  201. var light = lightProxy.properties.originalLight;
  202. var uniforms = lightProxy.material.uniforms;
  203. // skip infinite pointlights
  204. // right now you can't switch between infinite and finite pointlights
  205. // it's just too messy as they use different proxies
  206. var distance = light.distance;
  207. if ( distance > 0 ) {
  208. lightProxy.scale.set( 1, 1, 1 ).multiplyScalar( distance );
  209. uniforms[ "lightRadius" ].value = distance;
  210. positionVS.copy( light.matrixWorld.getPosition() );
  211. camera.matrixWorldInverse.multiplyVector3( positionVS );
  212. uniforms[ "lightPositionVS" ].value.copy( positionVS );
  213. lightProxy.position.copy( light.matrixWorld.getPosition() );
  214. } else {
  215. uniforms[ "lightRadius" ].value = Infinity;
  216. }
  217. // linear space colors
  218. var intensity = light.intensity * light.intensity;
  219. uniforms[ "lightIntensity" ].value = intensity;
  220. uniforms[ "lightColor" ].value.copyGammaToLinear( light.color );
  221. };
  222. var createDeferredPointLight = function ( light ) {
  223. // setup light material
  224. var materialLight = new THREE.ShaderMaterial( {
  225. uniforms: THREE.UniformsUtils.clone( pointLightShader.uniforms ),
  226. vertexShader: pointLightShader.vertexShader,
  227. fragmentShader: pointLightShader.fragmentShader,
  228. blending: THREE.AdditiveBlending,
  229. depthWrite: false,
  230. transparent: true,
  231. side: THREE.BackSide
  232. } );
  233. // infinite pointlights use full-screen quad proxy
  234. // regular pointlights use sphere proxy
  235. var geometry;
  236. if ( light.distance > 0 ) {
  237. geometry = geometryLightSphere;
  238. } else {
  239. geometry = geometryLightPlane;
  240. materialLight.depthTest = false;
  241. materialLight.side = THREE.FrontSide;
  242. }
  243. materialLight.uniforms[ "viewWidth" ].value = scaledWidth;
  244. materialLight.uniforms[ "viewHeight" ].value = scaledHeight;
  245. materialLight.uniforms[ 'samplerColor' ].value = compColor.renderTarget2;
  246. materialLight.uniforms[ 'samplerNormalDepth' ].value = compNormalDepth.renderTarget2;
  247. // create light proxy mesh
  248. var meshLight = new THREE.Mesh( geometry, materialLight );
  249. // keep reference for color and intensity updates
  250. meshLight.properties.originalLight = light;
  251. // keep reference for size reset
  252. resizableMaterials.push( materialLight );
  253. // sync proxy uniforms to the original light
  254. updatePointLightProxy( meshLight );
  255. return meshLight;
  256. };
  257. var updateSpotLightProxy = function ( lightProxy ) {
  258. var light = lightProxy.properties.originalLight;
  259. var uniforms = lightProxy.material.uniforms;
  260. positionVS.copy( light.matrixWorld.getPosition() );
  261. camera.matrixWorldInverse.multiplyVector3( positionVS );
  262. directionVS.copy( light.matrixWorld.getPosition() );
  263. directionVS.subSelf( light.target.matrixWorld.getPosition() );
  264. directionVS.normalize();
  265. camera.matrixWorldInverse.rotateAxis( directionVS );
  266. uniforms[ "lightPositionVS" ].value.copy( positionVS );
  267. uniforms[ "lightDirectionVS" ].value.copy( directionVS );
  268. uniforms[ "lightAngle" ].value = light.angle;
  269. uniforms[ "lightDistance" ].value = light.distance;
  270. // linear space colors
  271. var intensity = light.intensity * light.intensity;
  272. uniforms[ "lightIntensity" ].value = intensity;
  273. uniforms[ "lightColor" ].value.copyGammaToLinear( light.color );
  274. };
  275. var createDeferredSpotLight = function ( light ) {
  276. // setup light material
  277. var uniforms = THREE.UniformsUtils.clone( spotLightShader.uniforms );
  278. var materialLight = new THREE.ShaderMaterial( {
  279. uniforms: uniforms,
  280. vertexShader: spotLightShader.vertexShader,
  281. fragmentShader: spotLightShader.fragmentShader,
  282. blending: THREE.AdditiveBlending,
  283. depthWrite: false,
  284. depthTest: false,
  285. transparent: true
  286. } );
  287. uniforms[ "viewWidth" ].value = scaledWidth;
  288. uniforms[ "viewHeight" ].value = scaledHeight;
  289. uniforms[ 'samplerColor' ].value = compColor.renderTarget2;
  290. uniforms[ 'samplerNormalDepth' ].value = compNormalDepth.renderTarget2;
  291. // create light proxy mesh
  292. var meshLight = new THREE.Mesh( geometryLightPlane, materialLight );
  293. // keep reference for color and intensity updates
  294. meshLight.properties.originalLight = light;
  295. // keep reference for size reset
  296. resizableMaterials.push( materialLight );
  297. // sync proxy uniforms to the original light
  298. updateSpotLightProxy( meshLight );
  299. return meshLight;
  300. };
  301. var updateDirectionalLightProxy = function ( lightProxy ) {
  302. var light = lightProxy.properties.originalLight;
  303. var uniforms = lightProxy.material.uniforms;
  304. directionVS.copy( light.matrixWorld.getPosition() );
  305. directionVS.subSelf( light.target.matrixWorld.getPosition() );
  306. directionVS.normalize();
  307. camera.matrixWorldInverse.rotateAxis( directionVS );
  308. uniforms[ "lightDirectionVS" ].value.copy( directionVS );
  309. // linear space colors
  310. var intensity = light.intensity * light.intensity;
  311. uniforms[ "lightIntensity" ].value = intensity;
  312. uniforms[ "lightColor" ].value.copyGammaToLinear( light.color );
  313. };
  314. var createDeferredDirectionalLight = function ( light ) {
  315. // setup light material
  316. var uniforms = THREE.UniformsUtils.clone( directionalLightShader.uniforms );
  317. var materialLight = new THREE.ShaderMaterial( {
  318. uniforms: uniforms,
  319. vertexShader: directionalLightShader.vertexShader,
  320. fragmentShader: directionalLightShader.fragmentShader,
  321. blending: THREE.AdditiveBlending,
  322. depthWrite: false,
  323. depthTest: false,
  324. transparent: true
  325. } );
  326. uniforms[ "viewWidth" ].value = scaledWidth;
  327. uniforms[ "viewHeight" ].value = scaledHeight;
  328. uniforms[ 'samplerColor' ].value = compColor.renderTarget2;
  329. uniforms[ 'samplerNormalDepth' ].value = compNormalDepth.renderTarget2;
  330. // create light proxy mesh
  331. var meshLight = new THREE.Mesh( geometryLightPlane, materialLight );
  332. // keep reference for color and intensity updates
  333. meshLight.properties.originalLight = light;
  334. // keep reference for size reset
  335. resizableMaterials.push( materialLight );
  336. // sync proxy uniforms to the original light
  337. updateDirectionalLightProxy( meshLight );
  338. return meshLight;
  339. };
  340. var updateHemisphereLightProxy = function ( lightProxy ) {
  341. var light = lightProxy.properties.originalLight;
  342. var uniforms = lightProxy.material.uniforms;
  343. directionVS.copy( light.matrixWorld.getPosition() );
  344. directionVS.normalize();
  345. camera.matrixWorldInverse.rotateAxis( directionVS );
  346. uniforms[ "lightDirectionVS" ].value.copy( directionVS );
  347. // linear space colors
  348. var intensity = light.intensity * light.intensity;
  349. uniforms[ "lightIntensity" ].value = intensity;
  350. uniforms[ "lightColorSky" ].value.copyGammaToLinear( light.color );
  351. uniforms[ "lightColorGround" ].value.copyGammaToLinear( light.groundColor );
  352. };
  353. var createDeferredHemisphereLight = function ( light ) {
  354. // setup light material
  355. var uniforms = THREE.UniformsUtils.clone( hemisphereLightShader.uniforms );
  356. var materialLight = new THREE.ShaderMaterial( {
  357. uniforms: uniforms,
  358. vertexShader: hemisphereLightShader.vertexShader,
  359. fragmentShader: hemisphereLightShader.fragmentShader,
  360. blending: THREE.AdditiveBlending,
  361. depthWrite: false,
  362. depthTest: false,
  363. transparent: true
  364. } );
  365. uniforms[ "viewWidth" ].value = scaledWidth;
  366. uniforms[ "viewHeight" ].value = scaledHeight;
  367. uniforms[ 'samplerColor' ].value = compColor.renderTarget2;
  368. uniforms[ 'samplerNormalDepth' ].value = compNormalDepth.renderTarget2;
  369. // create light proxy mesh
  370. var meshLight = new THREE.Mesh( geometryLightPlane, materialLight );
  371. // keep reference for color and intensity updates
  372. meshLight.properties.originalLight = light;
  373. // keep reference for size reset
  374. resizableMaterials.push( materialLight );
  375. // sync proxy uniforms to the original light
  376. updateHemisphereLightProxy( meshLight );
  377. return meshLight;
  378. };
  379. var updateAreaLightProxy = function ( lightProxy ) {
  380. var light = lightProxy.properties.originalLight;
  381. var uniforms = lightProxy.material.uniforms;
  382. uniforms[ "lightPosition" ].value.copy( light.matrixWorld.getPosition() );
  383. var matrix = light.matrixWorld;
  384. right.copy( light.right );
  385. normal.copy( light.normal );
  386. matrix.rotateAxis( right );
  387. matrix.rotateAxis( normal );
  388. uniforms[ "lightRight" ].value.copy( right );
  389. uniforms[ "lightNormal" ].value.copy( normal );
  390. uniforms[ "lightWidth" ].value = light.width;
  391. uniforms[ "lightHeight" ].value = light.height;
  392. // linear space colors
  393. var intensity = light.intensity * light.intensity;
  394. uniforms[ "lightIntensity" ].value = intensity;
  395. uniforms[ "lightColor" ].value.copyGammaToLinear( light.color );
  396. };
  397. var createDeferredAreaLight = function ( light ) {
  398. // setup light material
  399. var uniforms = THREE.UniformsUtils.clone( areaLightShader.uniforms );
  400. var materialLight = new THREE.ShaderMaterial( {
  401. uniforms: uniforms,
  402. vertexShader: areaLightShader.vertexShader,
  403. fragmentShader: areaLightShader.fragmentShader,
  404. blending: THREE.AdditiveBlending,
  405. depthWrite: false,
  406. depthTest: false,
  407. transparent: true
  408. } );
  409. uniforms[ "viewWidth" ].value = scaledWidth;
  410. uniforms[ "viewHeight" ].value = scaledHeight;
  411. uniforms[ 'samplerColor' ].value = compColor.renderTarget2;
  412. uniforms[ 'samplerNormalDepth' ].value = compNormalDepth.renderTarget2;
  413. // create light proxy mesh
  414. var meshLight = new THREE.Mesh( geometryLightPlane, materialLight );
  415. // keep reference for color and intensity updates
  416. meshLight.properties.originalLight = light;
  417. // keep reference for size reset
  418. resizableMaterials.push( materialLight );
  419. // sync proxy uniforms to the original light
  420. updateAreaLightProxy( meshLight );
  421. return meshLight;
  422. };
  423. var createDeferredEmissiveLight = function () {
  424. // setup light material
  425. var materialLight = new THREE.ShaderMaterial( {
  426. uniforms: THREE.UniformsUtils.clone( emissiveLightShader.uniforms ),
  427. vertexShader: emissiveLightShader.vertexShader,
  428. fragmentShader: emissiveLightShader.fragmentShader,
  429. depthTest: false,
  430. depthWrite: false,
  431. blending: THREE.NoBlending
  432. } );
  433. materialLight.uniforms[ "viewWidth" ].value = scaledWidth;
  434. materialLight.uniforms[ "viewHeight" ].value = scaledHeight;
  435. materialLight.uniforms[ 'samplerColor' ].value = compColor.renderTarget2;
  436. // create light proxy mesh
  437. var meshLight = new THREE.Mesh( geometryLightPlane, materialLight );
  438. // keep reference for size reset
  439. resizableMaterials.push( materialLight );
  440. return meshLight;
  441. };
  442. var initDeferredProperties = function ( object ) {
  443. if ( object.properties.deferredInitialized ) return;
  444. if ( object.material ) initDeferredMaterials( object );
  445. if ( object instanceof THREE.PointLight ) {
  446. var proxy = createDeferredPointLight( object );
  447. if ( object.distance > 0 ) {
  448. lightSceneProxy.add( proxy );
  449. } else {
  450. lightSceneFullscreen.add( proxy );
  451. }
  452. } else if ( object instanceof THREE.SpotLight ) {
  453. var proxy = createDeferredSpotLight( object );
  454. lightSceneFullscreen.add( proxy );
  455. } else if ( object instanceof THREE.DirectionalLight ) {
  456. var proxy = createDeferredDirectionalLight( object );
  457. lightSceneFullscreen.add( proxy );
  458. } else if ( object instanceof THREE.HemisphereLight ) {
  459. var proxy = createDeferredHemisphereLight( object );
  460. lightSceneFullscreen.add( proxy );
  461. } else if ( object instanceof THREE.AreaLight ) {
  462. var proxy = createDeferredAreaLight( object );
  463. lightSceneFullscreen.add( proxy );
  464. }
  465. object.properties.deferredInitialized = true;
  466. };
  467. //
  468. var setMaterialColor = function ( object ) {
  469. if ( object.material ) {
  470. if ( object.properties.transparent ) {
  471. object.material = invisibleMaterial;
  472. } else {
  473. object.material = object.properties.colorMaterial;
  474. }
  475. }
  476. };
  477. var setMaterialNormalDepth = function ( object ) {
  478. if ( object.material ) {
  479. if ( object.properties.transparent ) {
  480. object.material = invisibleMaterial;
  481. } else {
  482. object.material = object.properties.normalDepthMaterial;
  483. }
  484. }
  485. };
  486. // external API
  487. this.setAntialias = function ( enabled ) {
  488. antialias = enabled;
  489. if ( antialias ) {
  490. effectFXAA.enabled = true;
  491. compositePass.renderToScreen = false;
  492. } else {
  493. effectFXAA.enabled = false;
  494. compositePass.renderToScreen = true;
  495. }
  496. };
  497. this.getAntialias = function () {
  498. return antialias;
  499. };
  500. this.setScale = function ( scale ) {
  501. currentScale = scale;
  502. scaledWidth = Math.floor( currentScale * fullWidth );
  503. scaledHeight = Math.floor( currentScale * fullHeight );
  504. compNormalDepth.setSize( scaledWidth, scaledHeight );
  505. compColor.setSize( scaledWidth, scaledHeight );
  506. compLight.setSize( scaledWidth, scaledHeight );
  507. compFinal.setSize( scaledWidth, scaledHeight );
  508. compColor.renderTarget2.shareDepthFrom = compNormalDepth.renderTarget2;
  509. compLight.renderTarget2.shareDepthFrom = compNormalDepth.renderTarget2;
  510. for ( var i = 0, il = resizableMaterials.length; i < il; i ++ ) {
  511. var uniforms = resizableMaterials[ i ].uniforms;
  512. uniforms[ "viewWidth" ].value = scaledWidth;
  513. uniforms[ "viewHeight" ].value = scaledHeight;
  514. if ( uniforms[ 'samplerColor' ] ) uniforms[ 'samplerColor' ].value = compColor.renderTarget2;
  515. if ( uniforms[ 'samplerNormalDepth' ] ) uniforms[ 'samplerNormalDepth' ].value = compNormalDepth.renderTarget2;
  516. }
  517. compositePass.uniforms[ 'samplerLight' ].value = compLight.renderTarget2;
  518. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / fullWidth, 1 / fullHeight );
  519. };
  520. this.setSize = function ( width, height ) {
  521. fullWidth = width;
  522. fullHeight = height;
  523. this.renderer.setSize( fullWidth, fullHeight );
  524. this.setScale( currentScale );
  525. };
  526. //
  527. function updateLightProxy ( proxy, camera ) {
  528. var uniforms = proxy.material.uniforms;
  529. if ( uniforms[ "matProjInverse" ] ) uniforms[ "matProjInverse" ].value = camera.projectionMatrixInverse;
  530. if ( uniforms[ "matView" ] ) uniforms[ "matView" ].value = camera.matrixWorldInverse;
  531. var originalLight = proxy.properties.originalLight;
  532. if ( originalLight ) {
  533. proxy.visible = originalLight.visible;
  534. if ( originalLight instanceof THREE.PointLight ) {
  535. updatePointLightProxy( proxy );
  536. } else if ( originalLight instanceof THREE.SpotLight ) {
  537. updateSpotLightProxy( proxy );
  538. } else if ( originalLight instanceof THREE.DirectionalLight ) {
  539. updateDirectionalLightProxy( proxy );
  540. } else if ( originalLight instanceof THREE.HemisphereLight ) {
  541. updateHemisphereLightProxy( proxy );
  542. } else if ( originalLight instanceof THREE.AreaLight ) {
  543. updateAreaLightProxy( proxy );
  544. }
  545. }
  546. };
  547. this.render = function ( scene, camera ) {
  548. // setup deferred properties
  549. if ( ! scene.properties.lightSceneProxy ) {
  550. scene.properties.lightSceneProxy = new THREE.Scene();
  551. scene.properties.lightSceneFullscreen = new THREE.Scene();
  552. var meshLight = createDeferredEmissiveLight();
  553. scene.properties.lightSceneFullscreen.add( meshLight );
  554. }
  555. lightSceneProxy = scene.properties.lightSceneProxy;
  556. lightSceneFullscreen = scene.properties.lightSceneFullscreen;
  557. passColor.camera = camera;
  558. passNormalDepth.camera = camera;
  559. passLightProxy.camera = camera;
  560. passLightFullscreen.camera = THREE.EffectComposer.camera;
  561. passColor.scene = scene;
  562. passNormalDepth.scene = scene;
  563. passLightFullscreen.scene = lightSceneFullscreen;
  564. passLightProxy.scene = lightSceneProxy;
  565. scene.traverse( initDeferredProperties );
  566. // update scene graph only once per frame
  567. // (both color and normalDepth passes use exactly the same scene state)
  568. this.renderer.autoUpdateScene = false;
  569. scene.updateMatrixWorld();
  570. // 1) g-buffer normals + depth pass
  571. scene.traverse( setMaterialNormalDepth );
  572. // clear shared depth buffer
  573. this.renderer.autoClearDepth = true;
  574. this.renderer.autoClearStencil = true;
  575. // write 1 to shared stencil buffer
  576. // for non-background pixels
  577. //gl.enable( gl.STENCIL_TEST );
  578. gl.stencilOp( gl.REPLACE, gl.REPLACE, gl.REPLACE );
  579. gl.stencilFunc( gl.ALWAYS, 1, 0xffffffff );
  580. gl.clearStencil( 0 );
  581. compNormalDepth.render();
  582. // just touch foreground pixels (stencil == 1)
  583. // both in color and light passes
  584. gl.stencilFunc( gl.EQUAL, 1, 0xffffffff );
  585. gl.stencilOp( gl.KEEP, gl.KEEP, gl.KEEP );
  586. // 2) g-buffer color pass
  587. scene.traverse( setMaterialColor );
  588. // must use clean slate depth buffer
  589. // otherwise there are z-fighting glitches
  590. // not enough precision between two geometry passes
  591. // just to use EQUAL depth test
  592. this.renderer.autoClearDepth = true;
  593. this.renderer.autoClearStencil = false;
  594. compColor.render();
  595. // 3) light pass
  596. // do not clear depth buffer in this pass
  597. // depth from geometry pass is used for light culling
  598. // (write light proxy color pixel if behind scene pixel)
  599. this.renderer.autoClearDepth = false;
  600. this.renderer.autoUpdateScene = true;
  601. gl.depthFunc( gl.GEQUAL );
  602. camera.projectionMatrixInverse.getInverse( camera.projectionMatrix );
  603. for ( var i = 0, il = lightSceneProxy.children.length; i < il; i ++ ) {
  604. var proxy = lightSceneProxy.children[ i ];
  605. updateLightProxy( proxy, camera );
  606. }
  607. for ( var i = 0, il = lightSceneFullscreen.children.length; i < il; i ++ ) {
  608. var proxy = lightSceneFullscreen.children[ i ];
  609. updateLightProxy( proxy, camera );
  610. }
  611. compLight.render();
  612. // 4) composite pass
  613. // return back to usual depth and stencil handling state
  614. this.renderer.autoClearDepth = true;
  615. this.renderer.autoClearStencil = true;
  616. gl.depthFunc( gl.LEQUAL );
  617. gl.disable( gl.STENCIL_TEST );
  618. compFinal.render( 0.1 );
  619. };
  620. //
  621. var createRenderTargets = function ( ) {
  622. var rtParamsFloatLinear = { minFilter: THREE.NearestFilter, magFilter: THREE.LinearFilter, stencilBuffer: true,
  623. format: THREE.RGBAFormat, type: THREE.FloatType };
  624. var rtParamsFloatNearest = { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, stencilBuffer: true,
  625. format: THREE.RGBAFormat, type: THREE.FloatType };
  626. var rtParamsUByte = { minFilter: THREE.NearestFilter, magFilter: THREE.LinearFilter, stencilBuffer: false,
  627. format: THREE.RGBFormat, type: THREE.UnsignedByteType };
  628. // g-buffers
  629. var rtColor = new THREE.WebGLRenderTarget( scaledWidth, scaledHeight, rtParamsFloatNearest );
  630. var rtNormalDepth = new THREE.WebGLRenderTarget( scaledWidth, scaledHeight, rtParamsFloatNearest );
  631. var rtLight = new THREE.WebGLRenderTarget( scaledWidth, scaledHeight, rtParamsFloatLinear );
  632. var rtFinal = new THREE.WebGLRenderTarget( scaledWidth, scaledHeight, rtParamsUByte );
  633. rtColor.generateMipmaps = false;
  634. rtNormalDepth.generateMipmaps = false;
  635. rtLight.generateMipmaps = false;
  636. rtFinal.generateMipmaps = false;
  637. // normal + depth composer
  638. passNormalDepth = new THREE.RenderPass();
  639. passNormalDepth.clear = true;
  640. compNormalDepth = new THREE.EffectComposer( _this.renderer, rtNormalDepth );
  641. compNormalDepth.addPass( passNormalDepth );
  642. // color composer
  643. passColor = new THREE.RenderPass();
  644. passColor.clear = true;
  645. compColor = new THREE.EffectComposer( _this.renderer, rtColor );
  646. compColor.addPass( passColor );
  647. compColor.renderTarget2.shareDepthFrom = compNormalDepth.renderTarget2;
  648. // light composer
  649. passLightFullscreen = new THREE.RenderPass();
  650. passLightFullscreen.clear = true;
  651. passLightProxy = new THREE.RenderPass();
  652. passLightProxy.clear = false;
  653. compLight = new THREE.EffectComposer( _this.renderer, rtLight );
  654. compLight.addPass( passLightFullscreen );
  655. compLight.addPass( passLightProxy );
  656. compLight.renderTarget2.shareDepthFrom = compNormalDepth.renderTarget2;
  657. // final composer
  658. compositePass = new THREE.ShaderPass( compositeShader );
  659. compositePass.uniforms[ 'samplerLight' ].value = compLight.renderTarget2;
  660. compositePass.uniforms[ 'brightness' ].value = brightness;
  661. compositePass.material.blending = THREE.NoBlending;
  662. compositePass.clear = true;
  663. // FXAA
  664. effectFXAA = new THREE.ShaderPass( THREE.FXAAShader );
  665. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / fullWidth, 1 / fullHeight );
  666. effectFXAA.renderToScreen = true;
  667. //
  668. compFinal = new THREE.EffectComposer( _this.renderer, rtFinal );
  669. compFinal.addPass( compositePass );
  670. compFinal.addPass( effectFXAA );
  671. if ( antialias ) {
  672. effectFXAA.enabled = true;
  673. compositePass.renderToScreen = false;
  674. } else {
  675. effectFXAA.enabled = false;
  676. compositePass.renderToScreen = true;
  677. }
  678. };
  679. // init
  680. createRenderTargets();
  681. };