ShaderSkin.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. */
  5. THREE.ShaderSkin = {
  6. /* ------------------------------------------------------------------------------------------
  7. // Simple skin shader
  8. // - per-pixel Blinn-Phong diffuse term mixed with half-Lambert wrap-around term (per color component)
  9. // - physically based specular term (Kelemen/Szirmay-Kalos specular reflectance)
  10. //
  11. // - diffuse map
  12. // - bump map
  13. // - specular map
  14. // - point, directional and hemisphere lights (use with "lights: true" material option)
  15. // - fog (use with "fog: true" material option)
  16. // - shadow maps
  17. //
  18. // ------------------------------------------------------------------------------------------ */
  19. 'skinSimple' : {
  20. uniforms: THREE.UniformsUtils.merge( [
  21. THREE.UniformsLib[ "fog" ],
  22. THREE.UniformsLib[ "ambient" ],
  23. THREE.UniformsLib[ "lights" ],
  24. THREE.UniformsLib[ "shadowmap" ],
  25. {
  26. "enableBump" : { type: "i", value: 0 },
  27. "enableSpecular": { type: "i", value: 0 },
  28. "tDiffuse" : { type: "t", value: null },
  29. "tBeckmann" : { type: "t", value: null },
  30. "diffuse": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  31. "specular": { type: "c", value: new THREE.Color( 0x111111 ) },
  32. "opacity": { type: "f", value: 1 },
  33. "uRoughness": { type: "f", value: 0.15 },
  34. "uSpecularBrightness": { type: "f", value: 0.75 },
  35. "bumpMap" : { type: "t", value: null },
  36. "bumpScale" : { type: "f", value: 1 },
  37. "specularMap" : { type: "t", value: null },
  38. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  39. "uWrapRGB": { type: "v3", value: new THREE.Vector3( 0.75, 0.375, 0.1875 ) }
  40. }
  41. ] ),
  42. fragmentShader: [
  43. "#define USE_BUMPMAP",
  44. "uniform bool enableBump;",
  45. "uniform bool enableSpecular;",
  46. "uniform vec3 diffuse;",
  47. "uniform vec3 specular;",
  48. "uniform float opacity;",
  49. "uniform float uRoughness;",
  50. "uniform float uSpecularBrightness;",
  51. "uniform vec3 uWrapRGB;",
  52. "uniform sampler2D tDiffuse;",
  53. "uniform sampler2D tBeckmann;",
  54. "uniform sampler2D specularMap;",
  55. "varying vec3 vNormal;",
  56. "varying vec2 vUv;",
  57. "varying vec3 vViewPosition;",
  58. THREE.ShaderChunk[ "common" ],
  59. THREE.ShaderChunk[ "bsdfs" ],
  60. THREE.ShaderChunk[ "ambient_pars" ],
  61. THREE.ShaderChunk[ "lights_pars" ],
  62. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  63. THREE.ShaderChunk[ "fog_pars_fragment" ],
  64. THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
  65. // Fresnel term
  66. "float fresnelReflectance( vec3 H, vec3 V, float F0 ) {",
  67. "float base = 1.0 - dot( V, H );",
  68. "float exponential = pow( base, 5.0 );",
  69. "return exponential + F0 * ( 1.0 - exponential );",
  70. "}",
  71. // Kelemen/Szirmay-Kalos specular BRDF
  72. "float KS_Skin_Specular( vec3 N,", // Bumped surface normal
  73. "vec3 L,", // Points to light
  74. "vec3 V,", // Points to eye
  75. "float m,", // Roughness
  76. "float rho_s", // Specular brightness
  77. ") {",
  78. "float result = 0.0;",
  79. "float ndotl = dot( N, L );",
  80. "if( ndotl > 0.0 ) {",
  81. "vec3 h = L + V;", // Unnormalized half-way vector
  82. "vec3 H = normalize( h );",
  83. "float ndoth = dot( N, H );",
  84. "float PH = pow( 2.0 * texture2D( tBeckmann, vec2( ndoth, m ) ).x, 10.0 );",
  85. "float F = fresnelReflectance( H, V, 0.028 );",
  86. "float frSpec = max( PH * F / dot( h, h ), 0.0 );",
  87. "result = ndotl * rho_s * frSpec;", // BRDF * dot(N,L) * rho_s
  88. "}",
  89. "return result;",
  90. "}",
  91. "void main() {",
  92. "vec3 outgoingLight = vec3( 0.0 );", // outgoing light does not have an alpha, the surface does
  93. "vec4 diffuseColor = vec4( diffuse, opacity );",
  94. "vec4 colDiffuse = texture2D( tDiffuse, vUv );",
  95. "colDiffuse.rgb *= colDiffuse.rgb;",
  96. "diffuseColor = diffuseColor * colDiffuse;",
  97. "vec3 normal = normalize( vNormal );",
  98. "vec3 viewerDirection = normalize( vViewPosition );",
  99. "float specularStrength;",
  100. "if ( enableSpecular ) {",
  101. "vec4 texelSpecular = texture2D( specularMap, vUv );",
  102. "specularStrength = texelSpecular.r;",
  103. "} else {",
  104. "specularStrength = 1.0;",
  105. "}",
  106. "#ifdef USE_BUMPMAP",
  107. "if ( enableBump ) normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",
  108. "#endif",
  109. // point lights
  110. "vec3 totalSpecularLight = vec3( 0.0 );",
  111. "vec3 totalDiffuseLight = vec3( 0.0 );",
  112. "#if NUM_POINT_LIGHTS > 0",
  113. "for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {",
  114. "vec3 lVector = pointLights[ i ].position + vViewPosition.xyz;",
  115. "float attenuation = calcLightAttenuation( length( lVector ), pointLights[ i ].distance, pointLights[ i ].decay );",
  116. "lVector = normalize( lVector );",
  117. "float pointDiffuseWeightFull = max( dot( normal, lVector ), 0.0 );",
  118. "float pointDiffuseWeightHalf = max( 0.5 * dot( normal, lVector ) + 0.5, 0.0 );",
  119. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), uWrapRGB );",
  120. "float pointSpecularWeight = KS_Skin_Specular( normal, lVector, viewerDirection, uRoughness, uSpecularBrightness );",
  121. "totalDiffuseLight += pointLight[ i ].color * ( pointDiffuseWeight * attenuation );",
  122. "totalSpecularLight += pointLight[ i ].color * specular * ( pointSpecularWeight * specularStrength * attenuation );",
  123. "}",
  124. "#endif",
  125. // directional lights
  126. "#if NUM_DIR_LIGHTS > 0",
  127. "for( int i = 0; i < NUM_DIR_LIGHTS; i++ ) {",
  128. "vec3 dirVector = directionalLights[ i ].direction;",
  129. "float dirDiffuseWeightFull = max( dot( normal, dirVector ), 0.0 );",
  130. "float dirDiffuseWeightHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
  131. "vec3 dirDiffuseWeight = mix( vec3 ( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), uWrapRGB );",
  132. "float dirSpecularWeight = KS_Skin_Specular( normal, dirVector, viewerDirection, uRoughness, uSpecularBrightness );",
  133. "totalDiffuseLight += directionalLights[ i ].color * dirDiffuseWeight;",
  134. "totalSpecularLight += directionalLights[ i ].color * ( dirSpecularWeight * specularStrength );",
  135. "}",
  136. "#endif",
  137. // hemisphere lights
  138. "#if NUM_HEMI_LIGHTS > 0",
  139. "for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {",
  140. "vec3 lVector = hemisphereLightDirection[ i ];",
  141. "float dotProduct = dot( normal, lVector );",
  142. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  143. "totalDiffuseLight += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  144. // specular (sky light)
  145. "float hemiSpecularWeight = 0.0;",
  146. "hemiSpecularWeight += KS_Skin_Specular( normal, lVector, viewerDirection, uRoughness, uSpecularBrightness );",
  147. // specular (ground light)
  148. "vec3 lVectorGround = -lVector;",
  149. "hemiSpecularWeight += KS_Skin_Specular( normal, lVectorGround, viewerDirection, uRoughness, uSpecularBrightness );",
  150. "vec3 hemiSpecularColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  151. "totalSpecularLight += hemiSpecularColor * specular * ( hemiSpecularWeight * specularStrength );",
  152. "}",
  153. "#endif",
  154. THREE.ShaderChunk[ "shadowmap_fragment" ],
  155. "totalDiffuseLight *= shadowMask;",
  156. "totalSpecularLight *= shadowMask;",
  157. "outgoingLight += diffuseColor.xyz * ( totalDiffuseLight + ambientLightColor * diffuse ) + totalSpecularLight;",
  158. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  159. THREE.ShaderChunk[ "fog_fragment" ],
  160. "gl_FragColor = vec4( outgoingLight, diffuseColor.a );", // TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects
  161. "}"
  162. ].join( "\n" ),
  163. vertexShader: [
  164. "uniform vec4 offsetRepeat;",
  165. "varying vec3 vNormal;",
  166. "varying vec2 vUv;",
  167. "varying vec3 vViewPosition;",
  168. THREE.ShaderChunk[ "common" ],
  169. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  170. "void main() {",
  171. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  172. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  173. "vViewPosition = -mvPosition.xyz;",
  174. "vNormal = normalize( normalMatrix * normal );",
  175. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  176. "gl_Position = projectionMatrix * mvPosition;",
  177. THREE.ShaderChunk[ "shadowmap_vertex" ],
  178. "}"
  179. ].join( "\n" )
  180. },
  181. /* ------------------------------------------------------------------------------------------
  182. // Skin shader
  183. // - Blinn-Phong diffuse term (using normal + diffuse maps)
  184. // - subsurface scattering approximation by four blur layers
  185. // - physically based specular term (Kelemen/Szirmay-Kalos specular reflectance)
  186. //
  187. // - point and directional lights (use with "lights: true" material option)
  188. //
  189. // - based on Nvidia Advanced Skin Rendering GDC 2007 presentation
  190. // and GPU Gems 3 Chapter 14. Advanced Techniques for Realistic Real-Time Skin Rendering
  191. //
  192. // http://developer.download.nvidia.com/presentations/2007/gdc/Advanced_Skin.pdf
  193. // http://http.developer.nvidia.com/GPUGems3/gpugems3_ch14.html
  194. // ------------------------------------------------------------------------------------------ */
  195. 'skin' : {
  196. uniforms: THREE.UniformsUtils.merge( [
  197. THREE.UniformsLib[ "fog" ],
  198. THREE.UniformsLib[ "ambient" ],
  199. THREE.UniformsLib[ "lights" ],
  200. {
  201. "passID": { type: "i", value: 0 },
  202. "tDiffuse" : { type: "t", value: null },
  203. "tNormal" : { type: "t", value: null },
  204. "tBlur1" : { type: "t", value: null },
  205. "tBlur2" : { type: "t", value: null },
  206. "tBlur3" : { type: "t", value: null },
  207. "tBlur4" : { type: "t", value: null },
  208. "tBeckmann" : { type: "t", value: null },
  209. "uNormalScale": { type: "f", value: 1.0 },
  210. "diffuse": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  211. "specular": { type: "c", value: new THREE.Color( 0x111111 ) },
  212. "opacity": { type: "f", value: 1 },
  213. "uRoughness": { type: "f", value: 0.15 },
  214. "uSpecularBrightness": { type: "f", value: 0.75 }
  215. }
  216. ] ),
  217. fragmentShader: [
  218. "uniform vec3 diffuse;",
  219. "uniform vec3 specular;",
  220. "uniform float opacity;",
  221. "uniform float uRoughness;",
  222. "uniform float uSpecularBrightness;",
  223. "uniform int passID;",
  224. "uniform sampler2D tDiffuse;",
  225. "uniform sampler2D tNormal;",
  226. "uniform sampler2D tBlur1;",
  227. "uniform sampler2D tBlur2;",
  228. "uniform sampler2D tBlur3;",
  229. "uniform sampler2D tBlur4;",
  230. "uniform sampler2D tBeckmann;",
  231. "uniform float uNormalScale;",
  232. "varying vec3 vNormal;",
  233. "varying vec2 vUv;",
  234. "varying vec3 vViewPosition;",
  235. THREE.ShaderChunk[ "common" ],
  236. THREE.ShaderChunk[ "ambient_pars" ],
  237. THREE.ShaderChunk[ "lights_pars" ],
  238. THREE.ShaderChunk[ "fog_pars_fragment" ],
  239. "float fresnelReflectance( vec3 H, vec3 V, float F0 ) {",
  240. "float base = 1.0 - dot( V, H );",
  241. "float exponential = pow( base, 5.0 );",
  242. "return exponential + F0 * ( 1.0 - exponential );",
  243. "}",
  244. // Kelemen/Szirmay-Kalos specular BRDF
  245. "float KS_Skin_Specular( vec3 N,", // Bumped surface normal
  246. "vec3 L,", // Points to light
  247. "vec3 V,", // Points to eye
  248. "float m,", // Roughness
  249. "float rho_s", // Specular brightness
  250. ") {",
  251. "float result = 0.0;",
  252. "float ndotl = dot( N, L );",
  253. "if( ndotl > 0.0 ) {",
  254. "vec3 h = L + V;", // Unnormalized half-way vector
  255. "vec3 H = normalize( h );",
  256. "float ndoth = dot( N, H );",
  257. "float PH = pow( 2.0 * texture2D( tBeckmann, vec2( ndoth, m ) ).x, 10.0 );",
  258. "float F = fresnelReflectance( H, V, 0.028 );",
  259. "float frSpec = max( PH * F / dot( h, h ), 0.0 );",
  260. "result = ndotl * rho_s * frSpec;", // BRDF * dot(N,L) * rho_s
  261. "}",
  262. "return result;",
  263. "}",
  264. "void main() {",
  265. "vec3 outgoingLight = vec3( 0.0 );", // outgoing light does not have an alpha, the surface does
  266. "vec4 diffuseColor = vec4( diffuse, opacity );",
  267. "vec4 mSpecular = vec4( specular, opacity );",
  268. "vec4 colDiffuse = texture2D( tDiffuse, vUv );",
  269. "colDiffuse *= colDiffuse;",
  270. "diffuseColor *= colDiffuse;",
  271. // normal mapping
  272. "vec4 posAndU = vec4( -vViewPosition, vUv.x );",
  273. "vec4 posAndU_dx = dFdx( posAndU ), posAndU_dy = dFdy( posAndU );",
  274. "vec3 tangent = posAndU_dx.w * posAndU_dx.xyz + posAndU_dy.w * posAndU_dy.xyz;",
  275. "vec3 normal = normalize( vNormal );",
  276. "vec3 binormal = normalize( cross( tangent, normal ) );",
  277. "tangent = cross( normal, binormal );", // no normalization required
  278. "mat3 tsb = mat3( tangent, binormal, normal );",
  279. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  280. "normalTex.xy *= uNormalScale;",
  281. "normalTex = normalize( normalTex );",
  282. "vec3 finalNormal = tsb * normalTex;",
  283. "normal = normalize( finalNormal );",
  284. "vec3 viewerDirection = normalize( vViewPosition );",
  285. // point lights
  286. "vec3 totalDiffuseLight = vec3( 0.0 );",
  287. "vec3 totalSpecularLight = vec3( 0.0 );",
  288. "#if NUM_POINT_LIGHTS > 0",
  289. "for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {",
  290. "vec3 pointVector = normalize( pointLights[ i ].direction );",
  291. "float attenuation = calcLightAttenuation( length( lVector ), pointLights[ i ].distance, pointLights[ i ].decay );",
  292. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  293. "totalDiffuseLight += pointLightColor[ i ] * ( pointDiffuseWeight * attenuation );",
  294. "if ( passID == 1 ) {",
  295. "float pointSpecularWeight = KS_Skin_Specular( normal, pointVector, viewerDirection, uRoughness, uSpecularBrightness );",
  296. "totalSpecularLight += pointLightColor[ i ] * mSpecular.xyz * ( pointSpecularWeight * attenuation );",
  297. "}",
  298. "}",
  299. "#endif",
  300. // directional lights
  301. "#if NUM_DIR_LIGHTS > 0",
  302. "for( int i = 0; i < NUM_DIR_LIGHTS; i++ ) {",
  303. "vec3 dirVector = directionalLights[ i ].direction;",
  304. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  305. "totalDiffuseLight += directionalLights[ i ].color * dirDiffuseWeight;",
  306. "if ( passID == 1 ) {",
  307. "float dirSpecularWeight = KS_Skin_Specular( normal, dirVector, viewerDirection, uRoughness, uSpecularBrightness );",
  308. "totalSpecularLight += directionalLights[ i ].color * mSpecular.xyz * dirSpecularWeight;",
  309. "}",
  310. "}",
  311. "#endif",
  312. "outgoingLight += diffuseColor.rgb * ( totalDiffuseLight + totalSpecularLight );",
  313. "if ( passID == 0 ) {",
  314. "outgoingLight = sqrt( outgoingLight );",
  315. "} else if ( passID == 1 ) {",
  316. //"#define VERSION1",
  317. "#ifdef VERSION1",
  318. "vec3 nonblurColor = sqrt(outgoingLight );",
  319. "#else",
  320. "vec3 nonblurColor = outgoingLight;",
  321. "#endif",
  322. "vec3 blur1Color = texture2D( tBlur1, vUv ).xyz;",
  323. "vec3 blur2Color = texture2D( tBlur2, vUv ).xyz;",
  324. "vec3 blur3Color = texture2D( tBlur3, vUv ).xyz;",
  325. "vec3 blur4Color = texture2D( tBlur4, vUv ).xyz;",
  326. //"gl_FragColor = vec4( blur1Color, gl_FragColor.w );",
  327. //"gl_FragColor = vec4( vec3( 0.22, 0.5, 0.7 ) * nonblurColor + vec3( 0.2, 0.5, 0.3 ) * blur1Color + vec3( 0.58, 0.0, 0.0 ) * blur2Color, gl_FragColor.w );",
  328. //"gl_FragColor = vec4( vec3( 0.25, 0.6, 0.8 ) * nonblurColor + vec3( 0.15, 0.25, 0.2 ) * blur1Color + vec3( 0.15, 0.15, 0.0 ) * blur2Color + vec3( 0.45, 0.0, 0.0 ) * blur3Color, gl_FragColor.w );",
  329. "outgoingLight = vec3( vec3( 0.22, 0.437, 0.635 ) * nonblurColor + ",
  330. "vec3( 0.101, 0.355, 0.365 ) * blur1Color + ",
  331. "vec3( 0.119, 0.208, 0.0 ) * blur2Color + ",
  332. "vec3( 0.114, 0.0, 0.0 ) * blur3Color + ",
  333. "vec3( 0.444, 0.0, 0.0 ) * blur4Color );",
  334. "outgoingLight *= sqrt( colDiffuse.xyz );",
  335. "outgoingLight += ambientLightColor * diffuse * colDiffuse.xyz + totalSpecularLight;",
  336. "#ifndef VERSION1",
  337. "outgoingLight = sqrt( outgoingLight );",
  338. "#endif",
  339. "}",
  340. THREE.ShaderChunk[ "fog_fragment" ],
  341. "gl_FragColor = vec4( outgoingLight, diffuseColor.a );", // TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects
  342. "}"
  343. ].join( "\n" ),
  344. vertexShader: [
  345. "#ifdef VERTEX_TEXTURES",
  346. "uniform sampler2D tDisplacement;",
  347. "uniform float uDisplacementScale;",
  348. "uniform float uDisplacementBias;",
  349. "#endif",
  350. "varying vec3 vNormal;",
  351. "varying vec2 vUv;",
  352. "varying vec3 vViewPosition;",
  353. THREE.ShaderChunk[ "common" ],
  354. "void main() {",
  355. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  356. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  357. "vViewPosition = -mvPosition.xyz;",
  358. "vNormal = normalize( normalMatrix * normal );",
  359. "vUv = uv;",
  360. // displacement mapping
  361. "#ifdef VERTEX_TEXTURES",
  362. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  363. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  364. "vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
  365. "gl_Position = projectionMatrix * displacedPosition;",
  366. "#else",
  367. "gl_Position = projectionMatrix * mvPosition;",
  368. "#endif",
  369. "}"
  370. ].join( "\n" ),
  371. vertexShaderUV: [
  372. "varying vec3 vNormal;",
  373. "varying vec2 vUv;",
  374. "varying vec3 vViewPosition;",
  375. THREE.ShaderChunk[ "common" ],
  376. "void main() {",
  377. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  378. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  379. "vViewPosition = -mvPosition.xyz;",
  380. "vNormal = normalize( normalMatrix * normal );",
  381. "vUv = uv;",
  382. "gl_Position = vec4( uv.x * 2.0 - 1.0, uv.y * 2.0 - 1.0, 0.0, 1.0 );",
  383. "}"
  384. ].join( "\n" )
  385. },
  386. /* ------------------------------------------------------------------------------------------
  387. // Beckmann distribution function
  388. // - to be used in specular term of skin shader
  389. // - render a screen-aligned quad to precompute a 512 x 512 texture
  390. //
  391. // - from http://developer.nvidia.com/node/171
  392. ------------------------------------------------------------------------------------------ */
  393. "beckmann" : {
  394. uniforms: {},
  395. vertexShader: [
  396. "varying vec2 vUv;",
  397. "void main() {",
  398. "vUv = uv;",
  399. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  400. "}"
  401. ].join( "\n" ),
  402. fragmentShader: [
  403. "varying vec2 vUv;",
  404. "float PHBeckmann( float ndoth, float m ) {",
  405. "float alpha = acos( ndoth );",
  406. "float ta = tan( alpha );",
  407. "float val = 1.0 / ( m * m * pow( ndoth, 4.0 ) ) * exp( -( ta * ta ) / ( m * m ) );",
  408. "return val;",
  409. "}",
  410. "float KSTextureCompute( vec2 tex ) {",
  411. // Scale the value to fit within [0,1] invert upon lookup.
  412. "return 0.5 * pow( PHBeckmann( tex.x, tex.y ), 0.1 );",
  413. "}",
  414. "void main() {",
  415. "float x = KSTextureCompute( vUv );",
  416. "gl_FragColor = vec4( x, x, x, 1.0 );",
  417. "}"
  418. ].join( "\n" )
  419. }
  420. };