ShaderDeferred.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author MPanknin / http://www.redplant.de/
  4. * @author benaadams / http://blog.illyriad.co.uk/
  5. *
  6. */
  7. THREE.ShaderDeferred = {
  8. "color" : {
  9. uniforms: THREE.UniformsUtils.merge( [
  10. THREE.UniformsLib[ "common" ],
  11. THREE.UniformsLib[ "fog" ],
  12. THREE.UniformsLib[ "shadowmap" ],
  13. {
  14. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  15. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  16. "shininess": { type: "f", value: 30 },
  17. "wrapAround": { type: "f", value: 1 },
  18. "additiveSpecular": { type: "f", value: 1 },
  19. "samplerNormalDepth": { type: "t", value: null },
  20. "viewWidth": { type: "f", value: 800 },
  21. "viewHeight": { type: "f", value: 600 }
  22. }
  23. ] ),
  24. fragmentShader : [
  25. "uniform vec3 diffuse;",
  26. "uniform vec3 specular;",
  27. "uniform vec3 emissive;",
  28. "uniform float shininess;",
  29. "uniform float wrapAround;",
  30. "uniform float additiveSpecular;",
  31. THREE.ShaderChunk[ "color_pars_fragment" ],
  32. THREE.ShaderChunk[ "map_pars_fragment" ],
  33. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  34. "#ifdef USE_ENVMAP",
  35. "varying vec3 vWorldPosition;",
  36. "uniform float reflectivity;",
  37. "uniform samplerCube envMap;",
  38. "uniform float flipEnvMap;",
  39. "uniform int combine;",
  40. "uniform bool useRefract;",
  41. "uniform float refractionRatio;",
  42. "uniform sampler2D samplerNormalDepth;",
  43. "uniform float viewHeight;",
  44. "uniform float viewWidth;",
  45. "#endif",
  46. THREE.ShaderChunk[ "fog_pars_fragment" ],
  47. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  48. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  49. "const float unit = 255.0/256.0;",
  50. "float vec3_to_float( vec3 data ) {",
  51. "highp float compressed = fract( data.x * unit ) + floor( data.y * unit * 255.0 ) + floor( data.z * unit * 255.0 ) * 255.0;",
  52. "return compressed;",
  53. "}",
  54. "void main() {",
  55. "const float opacity = 1.0;",
  56. "gl_FragColor = vec4( diffuse, opacity );",
  57. THREE.ShaderChunk[ "map_fragment" ],
  58. THREE.ShaderChunk[ "alphatest_fragment" ],
  59. THREE.ShaderChunk[ "specularmap_fragment" ],
  60. THREE.ShaderChunk[ "lightmap_fragment" ],
  61. THREE.ShaderChunk[ "color_fragment" ],
  62. "#ifdef USE_ENVMAP",
  63. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  64. "vec4 normalDepth = texture2D( samplerNormalDepth, texCoord );",
  65. "vec3 normal = normalDepth.xyz * 2.0 - 1.0;",
  66. "vec3 reflectVec;",
  67. "vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );",
  68. "if ( useRefract ) {",
  69. "reflectVec = refract( cameraToVertex, normal, refractionRatio );",
  70. "} else { ",
  71. "reflectVec = reflect( cameraToVertex, normal );",
  72. "}",
  73. "#ifdef DOUBLE_SIDED",
  74. "float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  75. "vec4 cubeColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );",
  76. "#else",
  77. "vec4 cubeColor = textureCube( envMap, vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );",
  78. "#endif",
  79. "#ifdef GAMMA_INPUT",
  80. "cubeColor.xyz *= cubeColor.xyz;",
  81. "#endif",
  82. "if ( combine == 1 ) {",
  83. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularStrength * reflectivity );",
  84. "} else if ( combine == 2 ) {",
  85. "gl_FragColor.xyz += cubeColor.xyz * specularStrength * reflectivity;",
  86. "} else {",
  87. "gl_FragColor.xyz = mix( gl_FragColor.xyz, gl_FragColor.xyz * cubeColor.xyz, specularStrength * reflectivity );",
  88. "}",
  89. "#endif",
  90. THREE.ShaderChunk[ "shadowmap_fragment" ],
  91. THREE.ShaderChunk[ "fog_fragment" ],
  92. //
  93. "const float compressionScale = 0.999;",
  94. //
  95. "vec3 diffuseMapColor;",
  96. "#ifdef USE_MAP",
  97. "diffuseMapColor = texelColor.xyz;",
  98. "#else",
  99. "diffuseMapColor = vec3( 1.0 );",
  100. "#endif",
  101. // diffuse color
  102. "gl_FragColor.x = vec3_to_float( compressionScale * gl_FragColor.xyz );",
  103. // specular color
  104. "if ( additiveSpecular < 0.0 ) {",
  105. "gl_FragColor.y = vec3_to_float( compressionScale * specular );",
  106. "} else {",
  107. "gl_FragColor.y = vec3_to_float( compressionScale * specular * diffuseMapColor );",
  108. "}",
  109. "gl_FragColor.y *= additiveSpecular;",
  110. // shininess
  111. "gl_FragColor.z = wrapAround * shininess;",
  112. // emissive color
  113. "gl_FragColor.w = vec3_to_float( compressionScale * emissive * diffuseMapColor );",
  114. "}"
  115. ].join("\n"),
  116. vertexShader : [
  117. THREE.ShaderChunk[ "map_pars_vertex" ],
  118. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  119. THREE.ShaderChunk[ "color_pars_vertex" ],
  120. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  121. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  122. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  123. "#ifdef USE_ENVMAP",
  124. "varying vec3 vWorldPosition;",
  125. "#endif",
  126. "void main() {",
  127. THREE.ShaderChunk[ "map_vertex" ],
  128. THREE.ShaderChunk[ "lightmap_vertex" ],
  129. THREE.ShaderChunk[ "color_vertex" ],
  130. THREE.ShaderChunk[ "skinbase_vertex" ],
  131. THREE.ShaderChunk[ "morphtarget_vertex" ],
  132. THREE.ShaderChunk[ "skinning_vertex" ],
  133. THREE.ShaderChunk[ "default_vertex" ],
  134. THREE.ShaderChunk[ "worldpos_vertex" ],
  135. THREE.ShaderChunk[ "shadowmap_vertex" ],
  136. "#ifdef USE_ENVMAP",
  137. "vWorldPosition = worldPosition.xyz;",
  138. "#endif",
  139. "}"
  140. ].join("\n")
  141. },
  142. "normalDepth" : {
  143. uniforms: {
  144. bumpMap: { type: "t", value: null },
  145. bumpScale: { type: "f", value: 1 },
  146. offsetRepeat: { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) }
  147. },
  148. fragmentShader : [
  149. "#ifdef USE_BUMPMAP",
  150. "#extension GL_OES_standard_derivatives : enable\n",
  151. "varying vec2 vUv;",
  152. "varying vec3 vViewPosition;",
  153. THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
  154. "#endif",
  155. "varying vec3 normalView;",
  156. "varying vec4 clipPos;",
  157. "void main() {",
  158. "vec3 normal = normalize( normalView );",
  159. "#ifdef USE_BUMPMAP",
  160. "normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",
  161. "#endif",
  162. "gl_FragColor.xyz = normal * 0.5 + 0.5;",
  163. "gl_FragColor.w = clipPos.z / clipPos.w;",
  164. "}"
  165. ].join("\n"),
  166. vertexShader : [
  167. "varying vec3 normalView;",
  168. "varying vec4 clipPos;",
  169. "#ifdef USE_BUMPMAP",
  170. "varying vec2 vUv;",
  171. "varying vec3 vViewPosition;",
  172. "uniform vec4 offsetRepeat;",
  173. "#endif",
  174. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  175. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  176. "void main() {",
  177. THREE.ShaderChunk[ "morphnormal_vertex" ],
  178. THREE.ShaderChunk[ "skinbase_vertex" ],
  179. THREE.ShaderChunk[ "skinnormal_vertex" ],
  180. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  181. THREE.ShaderChunk[ "morphtarget_vertex" ],
  182. THREE.ShaderChunk[ "skinning_vertex" ],
  183. THREE.ShaderChunk[ "default_vertex" ],
  184. "normalView = normalize( normalMatrix * objectNormal );",
  185. "#ifdef USE_BUMPMAP",
  186. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  187. "vViewPosition = -mvPosition.xyz;",
  188. "#endif",
  189. "clipPos = gl_Position;",
  190. "}"
  191. ].join("\n")
  192. },
  193. "composite" : {
  194. uniforms: {
  195. samplerLight: { type: "t", value: null },
  196. brightness: { type: "f", value: 1 }
  197. },
  198. fragmentShader : [
  199. "varying vec2 texCoord;",
  200. "uniform sampler2D samplerLight;",
  201. "uniform float brightness;",
  202. "void main() {",
  203. "vec3 color = texture2D( samplerLight, texCoord ).xyz;",
  204. "gl_FragColor = vec4( brightness * sqrt( color ), 1.0 );",
  205. "}"
  206. ].join("\n"),
  207. vertexShader : [
  208. "varying vec2 texCoord;",
  209. "void main() {",
  210. "vec4 pos = vec4( sign( position.xy ), 0.0, 1.0 );",
  211. "texCoord = pos.xy * vec2( 0.5 ) + 0.5;",
  212. "gl_Position = pos;",
  213. "}"
  214. ].join("\n")
  215. },
  216. "pointLight" : {
  217. uniforms: {
  218. samplerNormalDepth: { type: "t", value: null },
  219. samplerColor: { type: "t", value: null },
  220. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  221. viewWidth: { type: "f", value: 800 },
  222. viewHeight: { type: "f", value: 600 },
  223. lightPositionVS:{ type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
  224. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  225. lightIntensity: { type: "f", value: 1.0 },
  226. lightRadius: { type: "f", value: 1.0 }
  227. },
  228. fragmentShader : [
  229. "varying vec4 clipPos;",
  230. "uniform sampler2D samplerColor;",
  231. "uniform sampler2D samplerNormalDepth;",
  232. "uniform float lightRadius;",
  233. "uniform float lightIntensity;",
  234. "uniform float viewHeight;",
  235. "uniform float viewWidth;",
  236. "uniform vec3 lightColor;",
  237. "uniform vec3 lightPositionVS;",
  238. "uniform mat4 matProjInverse;",
  239. "vec3 float_to_vec3( float data ) {",
  240. "vec3 uncompressed;",
  241. "uncompressed.x = fract( data );",
  242. "float zInt = floor( data / 255.0 );",
  243. "uncompressed.z = fract( zInt / 255.0 );",
  244. "uncompressed.y = fract( floor( data - ( zInt * 255.0 ) ) / 255.0 );",
  245. "return uncompressed;",
  246. "}",
  247. "void main() {",
  248. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  249. "vec4 normalDepth = texture2D( samplerNormalDepth, texCoord );",
  250. "float z = normalDepth.w;",
  251. "float lightZ = clipPos.z / clipPos.w;",
  252. //"if ( z == 0.0 || lightZ > z ) discard;",
  253. "float x = texCoord.x * 2.0 - 1.0;",
  254. "float y = texCoord.y * 2.0 - 1.0;",
  255. "vec4 projectedPos = vec4( x, y, z, 1.0 );",
  256. "vec4 viewPos = matProjInverse * projectedPos;",
  257. "viewPos.xyz /= viewPos.w;",
  258. "viewPos.w = 1.0;",
  259. "vec3 lightDir = lightPositionVS - viewPos.xyz;",
  260. "float dist = length( lightDir );",
  261. "if ( dist > lightRadius ) discard;",
  262. "lightDir = normalize( lightDir );",
  263. "float cutoff = 0.3;",
  264. "float denom = dist/lightRadius + 1.0;",
  265. "float attenuation = 1.0 / ( denom * denom );",
  266. "attenuation = ( attenuation - cutoff ) / ( 1.0 - cutoff );",
  267. "attenuation = max( attenuation, 0.0 );",
  268. "attenuation *= attenuation;",
  269. // normal
  270. "vec3 normal = normalDepth.xyz * 2.0 - 1.0;",
  271. // color
  272. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  273. "vec3 albedo = float_to_vec3( abs( colorMap.x ) );",
  274. "vec3 specularColor = float_to_vec3( abs( colorMap.y ) );",
  275. "float shininess = abs( colorMap.z );",
  276. "float wrapAround = sign( colorMap.z );",
  277. "float additiveSpecular = sign( colorMap.y );",
  278. // light
  279. "vec3 diffuse;",
  280. "float diffuseFull = max( dot( normal, lightDir ), 0.0 );",
  281. "if ( wrapAround < 0.0 ) {",
  282. // wrap around lighting
  283. "float diffuseHalf = max( 0.5 + 0.5 * dot( normal, lightDir ), 0.0 );",
  284. "const vec3 wrapRGB = vec3( 0.6, 0.2, 0.2 );",
  285. "diffuse = mix( vec3( diffuseFull ), vec3( diffuseHalf ), wrapRGB );",
  286. "} else {",
  287. // simple lighting
  288. "diffuse = vec3( diffuseFull );",
  289. "}",
  290. // specular
  291. "vec3 halfVector = normalize( lightDir - normalize( viewPos.xyz ) );",
  292. "float dotNormalHalf = max( dot( normal, halfVector ), 0.0 );",
  293. // simple specular
  294. //"vec3 specular = max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse;",
  295. // physically based specular
  296. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  297. "vec3 schlick = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lightDir, halfVector ), 5.0 );",
  298. "vec3 specular = schlick * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse * specularNormalization;",
  299. // combine
  300. "vec3 light = lightIntensity * lightColor;",
  301. "if ( additiveSpecular < 0.0 ) {",
  302. "gl_FragColor = vec4( light * ( albedo * diffuse + specular ), attenuation );",
  303. "} else {",
  304. "gl_FragColor = vec4( light * albedo * ( diffuse + specular ), attenuation );",
  305. "}",
  306. "}"
  307. ].join("\n"),
  308. vertexShader : [
  309. "varying vec4 clipPos;",
  310. "void main() { ",
  311. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  312. "gl_Position = projectionMatrix * mvPosition;",
  313. "clipPos = gl_Position;",
  314. "}"
  315. ].join("\n")
  316. },
  317. "spotLight" : {
  318. uniforms: {
  319. samplerNormalDepth: { type: "t", value: null },
  320. samplerColor: { type: "t", value: null },
  321. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  322. viewWidth: { type: "f", value: 800 },
  323. viewHeight: { type: "f", value: 600 },
  324. lightPositionVS :{ type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  325. lightDirectionVS:{ type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  326. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  327. lightIntensity: { type: "f", value: 1.0 },
  328. lightDistance: { type: "f", value: 1.0 },
  329. lightAngle: { type: "f", value: 1.0 }
  330. },
  331. fragmentShader : [
  332. "uniform vec3 lightPositionVS;",
  333. "uniform vec3 lightDirectionVS;",
  334. "uniform sampler2D samplerColor;",
  335. "uniform sampler2D samplerNormalDepth;",
  336. "uniform float viewHeight;",
  337. "uniform float viewWidth;",
  338. "uniform float lightAngle;"+
  339. "uniform float lightIntensity;"+
  340. "uniform vec3 lightColor;",
  341. "uniform mat4 matProjInverse;",
  342. "vec3 float_to_vec3( float data ) {",
  343. "vec3 uncompressed;",
  344. "uncompressed.x = fract( data );",
  345. "float zInt = floor( data / 255.0 );",
  346. "uncompressed.z = fract( zInt / 255.0 );",
  347. "uncompressed.y = fract( floor( data - ( zInt * 255.0 ) ) / 255.0 );",
  348. "return uncompressed;",
  349. "}",
  350. "void main() {",
  351. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  352. "vec4 normalDepth = texture2D( samplerNormalDepth, texCoord );",
  353. "float z = normalDepth.w;",
  354. "if ( z == 0.0 ) discard;",
  355. "float x = texCoord.x * 2.0 - 1.0;",
  356. "float y = texCoord.y * 2.0 - 1.0;",
  357. "vec4 projectedPos = vec4( x, y, z, 1.0 );",
  358. "vec4 positionVS = matProjInverse * projectedPos;",
  359. "positionVS.xyz /= positionVS.w;",
  360. "positionVS.w = 1.0;",
  361. // normal
  362. "vec3 normal = normalDepth.xyz * 2.0 - 1.0;",
  363. // color
  364. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  365. "vec3 albedo = float_to_vec3( abs( colorMap.x ) );",
  366. "vec3 specularColor = float_to_vec3( abs( colorMap.y ) );",
  367. "float shininess = abs( colorMap.z );",
  368. "float wrapAround = sign( colorMap.z );",
  369. "float additiveSpecular = sign( colorMap.y );",
  370. //
  371. "vec3 surfToLight = normalize( lightPositionVS.xyz - positionVS.xyz );",
  372. "float dotProduct = max( dot( normal, surfToLight ), 0.0 );",
  373. "float rho = dot( lightDirectionVS, surfToLight );",
  374. "float rhoMax = cos( lightAngle * 0.5 );",
  375. "if ( rho > rhoMax ) {",
  376. "float theta = rhoMax + 0.0001;",
  377. "float phi = rhoMax + 0.05;",
  378. "float falloff = 4.0;",
  379. "float spot = 0.0;",
  380. "if ( rho >= phi ) {",
  381. "spot = 1.0;",
  382. "} else if ( rho <= theta ) {",
  383. "spot = 0.0;",
  384. "} else { ",
  385. "spot = pow( ( rho - theta ) / ( phi - theta ), falloff );",
  386. "}",
  387. "float diffuse = spot * dotProduct;",
  388. "vec3 halfVector = normalize( surfToLight - normalize( positionVS.xyz ) );",
  389. "float dotNormalHalf = max( dot( normal, halfVector ), 0.0 );",
  390. // simple specular
  391. "vec3 specular = max( pow( dotNormalHalf, shininess ), 0.0 ) * spot * diffuse * specularColor;",
  392. // combine
  393. "vec3 light = lightIntensity * lightColor;",
  394. "if ( additiveSpecular < 0.0 ) {",
  395. "gl_FragColor = vec4( light * ( albedo * diffuse + specular ), 1.0 );",
  396. "} else {",
  397. "gl_FragColor = vec4( light * albedo * ( diffuse + specular ), 1.0 );",
  398. "}",
  399. "return;",
  400. "}",
  401. "gl_FragColor = vec4( 0.0 );",
  402. "}"
  403. ].join("\n"),
  404. vertexShader : [
  405. "void main() { ",
  406. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  407. "}"
  408. ].join("\n")
  409. },
  410. "directionalLight" : {
  411. uniforms: {
  412. samplerNormalDepth: { type: "t", value: null },
  413. samplerColor: { type: "t", value: null },
  414. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  415. viewWidth: { type: "f", value: 800 },
  416. viewHeight: { type: "f", value: 600 },
  417. lightDirectionVS: { type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  418. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  419. lightIntensity: { type: "f", value: 1.0 }
  420. },
  421. fragmentShader : [
  422. "uniform sampler2D samplerColor;",
  423. "uniform sampler2D samplerNormalDepth;",
  424. "uniform float lightRadius;",
  425. "uniform float lightIntensity;",
  426. "uniform float viewHeight;",
  427. "uniform float viewWidth;",
  428. "uniform vec3 lightColor;",
  429. "uniform vec3 lightDirectionVS;",
  430. "uniform mat4 matProjInverse;",
  431. "vec3 float_to_vec3( float data ) {",
  432. "vec3 uncompressed;",
  433. "uncompressed.x = fract( data );",
  434. "float zInt = floor( data / 255.0 );",
  435. "uncompressed.z = fract( zInt / 255.0 );",
  436. "uncompressed.y = fract( floor( data - ( zInt * 255.0 ) ) / 255.0 );",
  437. "return uncompressed;",
  438. "}",
  439. "void main() {",
  440. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  441. "vec4 normalDepth = texture2D( samplerNormalDepth, texCoord );",
  442. "float z = normalDepth.w;",
  443. "if ( z == 0.0 ) discard;",
  444. "float x = texCoord.x * 2.0 - 1.0;",
  445. "float y = texCoord.y * 2.0 - 1.0;",
  446. "vec4 projectedPos = vec4( x, y, z, 1.0 );",
  447. "vec4 viewPos = matProjInverse * projectedPos;",
  448. "viewPos.xyz /= viewPos.w;",
  449. "viewPos.w = 1.0;",
  450. // normal
  451. "vec3 normal = normalDepth.xyz * 2.0 - 1.0;",
  452. // color
  453. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  454. "vec3 albedo = float_to_vec3( abs( colorMap.x ) );",
  455. "vec3 specularColor = float_to_vec3( abs( colorMap.y ) );",
  456. "float shininess = abs( colorMap.z );",
  457. "float wrapAround = sign( colorMap.z );",
  458. "vec3 diffuse;",
  459. "float dotProduct = dot( normal, lightDirectionVS );",
  460. "float diffuseFull = max( dotProduct, 0.0 );",
  461. // wrap around lighting
  462. "if ( wrapAround < 0.0 ) {",
  463. "float diffuseHalf = max( 0.5 + 0.5 * dotProduct, 0.0 );",
  464. "const vec3 wrapRGB = vec3( 0.2, 0.2, 0.2 );",
  465. "diffuse = mix( vec3 ( diffuseFull ), vec3( diffuseHalf ), wrapRGB );",
  466. // simple lighting
  467. "} else {",
  468. "diffuse = vec3 ( diffuseFull );",
  469. "}",
  470. // specular
  471. "vec3 halfVector = normalize( lightDirectionVS - normalize( viewPos.xyz ) );",
  472. "float dotNormalHalf = max( dot( normal, halfVector ), 0.0 );",
  473. // simple specular
  474. //"vec3 specular = specularColor * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse;",
  475. // physically based specular
  476. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  477. "vec3 schlick = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lightDirectionVS, halfVector ), 5.0 );",
  478. "vec3 specular = schlick * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse * specularNormalization;",
  479. // combine
  480. "vec3 light = lightIntensity * lightColor;",
  481. "gl_FragColor = vec4( light * ( albedo * diffuse + specular ), 1.0 );",
  482. "}"
  483. ].join("\n"),
  484. vertexShader : [
  485. "void main() { ",
  486. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  487. "}"
  488. ].join("\n")
  489. },
  490. "emissiveLight" : {
  491. uniforms: {
  492. samplerColor: { type: "t", value: null },
  493. viewWidth: { type: "f", value: 800 },
  494. viewHeight: { type: "f", value: 600 },
  495. },
  496. fragmentShader : [
  497. "uniform sampler2D samplerColor;",
  498. "uniform float viewHeight;",
  499. "uniform float viewWidth;",
  500. "vec3 float_to_vec3( float data ) {",
  501. "vec3 uncompressed;",
  502. "uncompressed.x = fract( data );",
  503. "float zInt = floor( data / 255.0 );",
  504. "uncompressed.z = fract( zInt / 255.0 );",
  505. "uncompressed.y = fract( floor( data - ( zInt * 255.0 ) ) / 255.0 );",
  506. "return uncompressed;",
  507. "}",
  508. "void main() {",
  509. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  510. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  511. "vec3 emissiveColor = float_to_vec3( abs( colorMap.w ) );",
  512. "gl_FragColor = vec4( emissiveColor, 1.0 );",
  513. "}"
  514. ].join("\n"),
  515. vertexShader : [
  516. "void main() { ",
  517. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  518. "}"
  519. ].join("\n")
  520. }
  521. };