WebGLDeferredRenderer.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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 direction = new THREE.Vector3();
  28. //
  29. var geometryLightSphere = new THREE.SphereGeometry( 1, 16, 8 );
  30. var geometryLightPlane = new THREE.PlaneGeometry( 2, 2 );
  31. var black = new THREE.Color( 0x000000 );
  32. var colorShader = THREE.ShaderDeferred[ "color" ];
  33. var normalDepthShader = THREE.ShaderDeferred[ "normalDepth" ];
  34. //
  35. var emissiveLightShader = THREE.ShaderDeferred[ "emissiveLight" ];
  36. var pointLightShader = THREE.ShaderDeferred[ "pointLight" ];
  37. var spotLightShader = THREE.ShaderDeferred[ "spotLight" ];
  38. var directionalLightShader = THREE.ShaderDeferred[ "directionalLight" ];
  39. var compositeShader = THREE.ShaderDeferred[ "composite" ];
  40. //
  41. var compColor, compNormal, compDepth, compLight, compFinal;
  42. var passColor, passNormal, passDepth, passLightFullscreen, passLightProxy, compositePass;
  43. var effectFXAA;
  44. //
  45. var lightSceneFullscreen, lightSceneProxy;
  46. //
  47. var resizableMaterials = [];
  48. //
  49. var invisibleMaterial = new THREE.ShaderMaterial();
  50. invisibleMaterial.visible = false;
  51. var defaultNormalDepthMaterial = new THREE.ShaderMaterial( {
  52. uniforms: THREE.UniformsUtils.clone( normalDepthShader.uniforms ),
  53. vertexShader: normalDepthShader.vertexShader,
  54. fragmentShader: normalDepthShader.fragmentShader,
  55. blending: THREE.NoBlending
  56. } );
  57. //
  58. var initDeferredMaterials = function ( object ) {
  59. if ( object.material instanceof THREE.MeshFaceMaterial ) {
  60. var colorMaterials = [];
  61. var normalDepthMaterials = [];
  62. var materials = object.material.materials;
  63. for ( var i = 0, il = materials.length; i < il; i ++ ) {
  64. var deferredMaterials = createDeferredMaterials( materials[ i ] );
  65. if ( deferredMaterials.transparent ) {
  66. colorMaterials.push( invisibleMaterial );
  67. normalDepthMaterials.push( invisibleMaterial );
  68. } else {
  69. colorMaterials.push( deferredMaterials.colorMaterial );
  70. normalDepthMaterials.push( deferredMaterials.normalDepthMaterial );
  71. }
  72. }
  73. object.properties.colorMaterial = new THREE.MeshFaceMaterial( colorMaterials );
  74. object.properties.normalDepthMaterial = new THREE.MeshFaceMaterial( normalDepthMaterials );
  75. } else {
  76. var deferredMaterials = createDeferredMaterials( object.material );
  77. object.properties.colorMaterial = deferredMaterials.colorMaterial;
  78. object.properties.normalDepthMaterial = deferredMaterials.normalDepthMaterial;
  79. object.properties.transparent = deferredMaterials.transparent;
  80. }
  81. };
  82. var createDeferredMaterials = function ( originalMaterial ) {
  83. var deferredMaterials = {};
  84. // color material
  85. // -----------------
  86. // diffuse color
  87. // specular color
  88. // shininess
  89. // diffuse map
  90. // vertex colors
  91. // alphaTest
  92. // morphs
  93. var uniforms = THREE.UniformsUtils.clone( colorShader.uniforms );
  94. var defines = { "USE_MAP": !! originalMaterial.map, "USE_ENVMAP": !! originalMaterial.envMap, "GAMMA_INPUT": true };
  95. var material = new THREE.ShaderMaterial( {
  96. fragmentShader: colorShader.fragmentShader,
  97. vertexShader: colorShader.vertexShader,
  98. uniforms: uniforms,
  99. defines: defines,
  100. shading: originalMaterial.shading
  101. } );
  102. if ( originalMaterial instanceof THREE.MeshBasicMaterial ) {
  103. var diffuse = black;
  104. var emissive = originalMaterial.color;
  105. } else {
  106. var diffuse = originalMaterial.color;
  107. var emissive = originalMaterial.emissive !== undefined ? originalMaterial.emissive : black;
  108. }
  109. var specular = originalMaterial.specular !== undefined ? originalMaterial.specular : black;
  110. var shininess = originalMaterial.shininess !== undefined ? originalMaterial.shininess : 1;
  111. var wrapAround = originalMaterial.wrapAround !== undefined ? ( originalMaterial.wrapAround ? -1 : 1 ) : 1;
  112. var additiveSpecular = originalMaterial.metal !== undefined ? ( originalMaterial.metal ? 1 : -1 ) : -1;
  113. uniforms.emissive.value.copyGammaToLinear( emissive );
  114. uniforms.diffuse.value.copyGammaToLinear( diffuse );
  115. uniforms.specular.value.copyGammaToLinear( specular );
  116. uniforms.shininess.value = shininess;
  117. uniforms.wrapAround.value = wrapAround;
  118. uniforms.additiveSpecular.value = additiveSpecular;
  119. uniforms.map.value = originalMaterial.map;
  120. if ( originalMaterial.envMap ) {
  121. uniforms.envMap.value = originalMaterial.envMap;
  122. uniforms.useRefract.value = originalMaterial.envMap.mapping instanceof THREE.CubeRefractionMapping;
  123. uniforms.refractionRatio.value = originalMaterial.refractionRatio;
  124. uniforms.combine.value = originalMaterial.combine;
  125. uniforms.reflectivity.value = originalMaterial.reflectivity;
  126. uniforms.flipEnvMap.value = ( originalMaterial.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : -1;
  127. uniforms.samplerNormalDepth.value = compNormalDepth.renderTarget2;
  128. uniforms.viewWidth.value = scaledWidth;
  129. uniforms.viewHeight.value = scaledHeight;
  130. resizableMaterials.push( material );
  131. }
  132. material.vertexColors = originalMaterial.vertexColors;
  133. material.morphTargets = originalMaterial.morphTargets;
  134. material.morphNormals = originalMaterial.morphNormals;
  135. material.skinning = originalMaterial.skinning;
  136. material.alphaTest = originalMaterial.alphaTest;
  137. material.wireframe = originalMaterial.wireframe;
  138. // uv repeat and offset setting priorities
  139. // 1. color map
  140. // 2. specular map
  141. // 3. normal map
  142. // 4. bump map
  143. var uvScaleMap;
  144. if ( originalMaterial.map ) {
  145. uvScaleMap = originalMaterial.map;
  146. } else if ( originalMaterial.specularMap ) {
  147. uvScaleMap = originalMaterial.specularMap;
  148. } else if ( originalMaterial.normalMap ) {
  149. uvScaleMap = originalMaterial.normalMap;
  150. } else if ( originalMaterial.bumpMap ) {
  151. uvScaleMap = originalMaterial.bumpMap;
  152. }
  153. if ( uvScaleMap !== undefined ) {
  154. var offset = uvScaleMap.offset;
  155. var repeat = uvScaleMap.repeat;
  156. uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
  157. }
  158. deferredMaterials.colorMaterial = material;
  159. // normal + depth material
  160. // -----------------
  161. // vertex normals
  162. // morph normals
  163. // bump map
  164. // bump scale
  165. // clip depth
  166. if ( originalMaterial.morphTargets || originalMaterial.skinning || originalMaterial.bumpMap ) {
  167. var uniforms = THREE.UniformsUtils.clone( normalDepthShader.uniforms );
  168. var defines = { "USE_BUMPMAP": !!originalMaterial.bumpMap };
  169. var normalDepthMaterial = new THREE.ShaderMaterial( {
  170. uniforms: uniforms,
  171. vertexShader: normalDepthShader.vertexShader,
  172. fragmentShader: normalDepthShader.fragmentShader,
  173. shading: originalMaterial.shading,
  174. defines: defines,
  175. blending: THREE.NoBlending
  176. } );
  177. normalDepthMaterial.morphTargets = originalMaterial.morphTargets;
  178. normalDepthMaterial.morphNormals = originalMaterial.morphNormals;
  179. normalDepthMaterial.skinning = originalMaterial.skinning;
  180. if ( originalMaterial.bumpMap ) {
  181. uniforms.bumpMap.value = originalMaterial.bumpMap;
  182. uniforms.bumpScale.value = originalMaterial.bumpScale;
  183. var offset = originalMaterial.bumpMap.offset;
  184. var repeat = originalMaterial.bumpMap.repeat;
  185. uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
  186. }
  187. } else {
  188. var normalDepthMaterial = defaultNormalDepthMaterial.clone();
  189. }
  190. normalDepthMaterial.wireframe = originalMaterial.wireframe;
  191. normalDepthMaterial.vertexColors = originalMaterial.vertexColors;
  192. deferredMaterials.normalDepthMaterial = normalDepthMaterial;
  193. //
  194. deferredMaterials.transparent = originalMaterial.transparent;
  195. return deferredMaterials;
  196. };
  197. var updatePointLightProxy = function ( lightProxy ) {
  198. var light = lightProxy.properties.originalLight;
  199. var uniforms = lightProxy.material.uniforms;
  200. // skip infinite pointlights
  201. // right now you can't switch between infinite and finite pointlights
  202. // it's just too messy as they use different proxies
  203. var distance = light.distance;
  204. if ( distance > 0 ) {
  205. lightProxy.scale.set( 1, 1, 1 ).multiplyScalar( distance );
  206. uniforms[ "lightRadius" ].value = distance;
  207. var position = light.matrixWorld.getPosition();
  208. uniforms[ "lightPos" ].value.copy( position );
  209. lightProxy.position.copy( position );
  210. } else {
  211. uniforms[ "lightRadius" ].value = Infinity;
  212. }
  213. // linear space colors
  214. var intensity = light.intensity * light.intensity;
  215. uniforms[ "lightIntensity" ].value = intensity;
  216. uniforms[ "lightColor" ].value.copyGammaToLinear( light.color );
  217. };
  218. var createDeferredPointLight = function ( light ) {
  219. // setup light material
  220. var materialLight = new THREE.ShaderMaterial( {
  221. uniforms: THREE.UniformsUtils.clone( pointLightShader.uniforms ),
  222. vertexShader: pointLightShader.vertexShader,
  223. fragmentShader: pointLightShader.fragmentShader,
  224. blending: THREE.AdditiveBlending,
  225. depthWrite: false,
  226. transparent: true,
  227. side: THREE.BackSide
  228. } );
  229. // infinite pointlights use full-screen quad proxy
  230. // regular pointlights use sphere proxy
  231. var geometry;
  232. if ( light.distance > 0 ) {
  233. geometry = geometryLightSphere;
  234. } else {
  235. geometry = geometryLightPlane;
  236. materialLight.depthTest = false;
  237. materialLight.side = THREE.FrontSide;
  238. }
  239. materialLight.uniforms[ "viewWidth" ].value = scaledWidth;
  240. materialLight.uniforms[ "viewHeight" ].value = scaledHeight;
  241. materialLight.uniforms[ 'samplerColor' ].value = compColor.renderTarget2;
  242. materialLight.uniforms[ 'samplerNormalDepth' ].value = compNormalDepth.renderTarget2;
  243. // create light proxy mesh
  244. var meshLight = new THREE.Mesh( geometry, materialLight );
  245. // keep reference for color and intensity updates
  246. meshLight.properties.originalLight = light;
  247. // keep reference for size reset
  248. resizableMaterials.push( materialLight );
  249. // sync proxy uniforms to the original light
  250. updatePointLightProxy( meshLight );
  251. return meshLight;
  252. };
  253. var updateSpotLightProxy = function ( lightProxy ) {
  254. var light = lightProxy.properties.originalLight;
  255. var uniforms = lightProxy.material.uniforms;
  256. positionVS.copy( light.matrixWorld.getPosition() );
  257. camera.matrixWorldInverse.multiplyVector3( positionVS );
  258. directionVS.copy( light.matrixWorld.getPosition() );
  259. directionVS.subSelf( light.target.matrixWorld.getPosition() );
  260. directionVS.normalize();
  261. camera.matrixWorldInverse.rotateAxis( directionVS );
  262. uniforms[ "lightPositionVS" ].value.copy( positionVS );
  263. uniforms[ "lightDirectionVS" ].value.copy( directionVS );
  264. uniforms[ "lightAngle" ].value = light.angle;
  265. uniforms[ "lightDistance" ].value = light.distance;
  266. // linear space colors
  267. var intensity = light.intensity * light.intensity;
  268. uniforms[ "lightIntensity" ].value = intensity;
  269. uniforms[ "lightColor" ].value.copyGammaToLinear( light.color );
  270. };
  271. var createDeferredSpotLight = function ( light ) {
  272. // setup light material
  273. var uniforms = THREE.UniformsUtils.clone( spotLightShader.uniforms );
  274. var materialLight = new THREE.ShaderMaterial( {
  275. uniforms: uniforms,
  276. vertexShader: spotLightShader.vertexShader,
  277. fragmentShader: spotLightShader.fragmentShader,
  278. blending: THREE.AdditiveBlending,
  279. depthWrite: false,
  280. depthTest: false,
  281. transparent: true
  282. } );
  283. uniforms[ "viewWidth" ].value = scaledWidth;
  284. uniforms[ "viewHeight" ].value = scaledHeight;
  285. uniforms[ 'samplerColor' ].value = compColor.renderTarget2;
  286. uniforms[ 'samplerNormalDepth' ].value = compNormalDepth.renderTarget2;
  287. // create light proxy mesh
  288. var meshLight = new THREE.Mesh( geometryLightPlane, materialLight );
  289. // keep reference for color and intensity updates
  290. meshLight.properties.originalLight = light;
  291. // keep reference for size reset
  292. resizableMaterials.push( materialLight );
  293. // sync proxy uniforms to the original light
  294. updateSpotLightProxy( meshLight );
  295. return meshLight;
  296. };
  297. var updateDirectionalLightProxy = function ( lightProxy ) {
  298. var light = lightProxy.properties.originalLight;
  299. var uniforms = lightProxy.material.uniforms;
  300. direction.copy( light.matrixWorld.getPosition() );
  301. direction.subSelf( light.target.matrixWorld.getPosition() );
  302. direction.normalize();
  303. uniforms[ "lightDir" ].value.copy( direction );
  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. };