WebGLDeferredRenderer.js 30 KB

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