ShaderSkin.js 18 KB

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