WebGLDeferredRenderer.js 24 KB

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