ShaderSkin.js 18 KB

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