ShaderDeferred.js 18 KB

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