SkinShader.js 17 KB

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