ShaderSkin.js 18 KB

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