ShaderSkin.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. THREE.ShaderChunk[ "fog_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. THREE.ShaderChunk[ "fog_vertex" ],
  175. "}"
  176. ].join( "\n" )
  177. },
  178. /* ------------------------------------------------------------------------------------------
  179. // Skin shader
  180. // - Blinn-Phong diffuse term (using normal + diffuse maps)
  181. // - subsurface scattering approximation by four blur layers
  182. // - physically based specular term (Kelemen/Szirmay-Kalos specular reflectance)
  183. //
  184. // - point and directional lights (use with "lights: true" material option)
  185. //
  186. // - based on Nvidia Advanced Skin Rendering GDC 2007 presentation
  187. // and GPU Gems 3 Chapter 14. Advanced Techniques for Realistic Real-Time Skin Rendering
  188. //
  189. // http://developer.download.nvidia.com/presentations/2007/gdc/Advanced_Skin.pdf
  190. // http://http.developer.nvidia.com/GPUGems3/gpugems3_ch14.html
  191. // ------------------------------------------------------------------------------------------ */
  192. 'skin' : {
  193. uniforms: THREE.UniformsUtils.merge( [
  194. THREE.UniformsLib[ "fog" ],
  195. THREE.UniformsLib[ "lights" ],
  196. {
  197. "passID": { value: 0 },
  198. "tDiffuse" : { value: null },
  199. "tNormal" : { value: null },
  200. "tBlur1" : { value: null },
  201. "tBlur2" : { value: null },
  202. "tBlur3" : { value: null },
  203. "tBlur4" : { value: null },
  204. "tBeckmann" : { value: null },
  205. "uNormalScale": { value: 1.0 },
  206. "diffuse": { value: new THREE.Color( 0xeeeeee ) },
  207. "specular": { value: new THREE.Color( 0x111111 ) },
  208. "opacity": { value: 1 },
  209. "uRoughness": { value: 0.15 },
  210. "uSpecularBrightness": { 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[ "lights_pars" ],
  233. THREE.ShaderChunk[ "fog_pars_fragment" ],
  234. "float fresnelReflectance( vec3 H, vec3 V, float F0 ) {",
  235. "float base = 1.0 - dot( V, H );",
  236. "float exponential = pow( base, 5.0 );",
  237. "return exponential + F0 * ( 1.0 - exponential );",
  238. "}",
  239. // Kelemen/Szirmay-Kalos specular BRDF
  240. "float KS_Skin_Specular( vec3 N,", // Bumped surface normal
  241. "vec3 L,", // Points to light
  242. "vec3 V,", // Points to eye
  243. "float m,", // Roughness
  244. "float rho_s", // Specular brightness
  245. ") {",
  246. "float result = 0.0;",
  247. "float ndotl = dot( N, L );",
  248. "if( ndotl > 0.0 ) {",
  249. "vec3 h = L + V;", // Unnormalized half-way vector
  250. "vec3 H = normalize( h );",
  251. "float ndoth = dot( N, H );",
  252. "float PH = pow( 2.0 * texture2D( tBeckmann, vec2( ndoth, m ) ).x, 10.0 );",
  253. "float F = fresnelReflectance( H, V, 0.028 );",
  254. "float frSpec = max( PH * F / dot( h, h ), 0.0 );",
  255. "result = ndotl * rho_s * frSpec;", // BRDF * dot(N,L) * rho_s
  256. "}",
  257. "return result;",
  258. "}",
  259. "void main() {",
  260. "vec3 outgoingLight = vec3( 0.0 );", // outgoing light does not have an alpha, the surface does
  261. "vec4 diffuseColor = vec4( diffuse, opacity );",
  262. "vec4 mSpecular = vec4( specular, opacity );",
  263. "vec4 colDiffuse = texture2D( tDiffuse, vUv );",
  264. "colDiffuse *= colDiffuse;",
  265. "diffuseColor *= colDiffuse;",
  266. // normal mapping
  267. "vec4 posAndU = vec4( -vViewPosition, vUv.x );",
  268. "vec4 posAndU_dx = dFdx( posAndU ), posAndU_dy = dFdy( posAndU );",
  269. "vec3 tangent = posAndU_dx.w * posAndU_dx.xyz + posAndU_dy.w * posAndU_dy.xyz;",
  270. "vec3 normal = normalize( vNormal );",
  271. "vec3 binormal = normalize( cross( tangent, normal ) );",
  272. "tangent = cross( normal, binormal );", // no normalization required
  273. "mat3 tsb = mat3( tangent, binormal, normal );",
  274. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  275. "normalTex.xy *= uNormalScale;",
  276. "normalTex = normalize( normalTex );",
  277. "vec3 finalNormal = tsb * normalTex;",
  278. "normal = normalize( finalNormal );",
  279. "vec3 viewerDirection = normalize( vViewPosition );",
  280. // point lights
  281. "vec3 totalDiffuseLight = vec3( 0.0 );",
  282. "vec3 totalSpecularLight = vec3( 0.0 );",
  283. "#if NUM_POINT_LIGHTS > 0",
  284. "for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {",
  285. "vec3 pointVector = normalize( pointLights[ i ].direction );",
  286. "float attenuation = calcLightAttenuation( length( lVector ), pointLights[ i ].distance, pointLights[ i ].decay );",
  287. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  288. "totalDiffuseLight += pointLightColor[ i ] * ( pointDiffuseWeight * attenuation );",
  289. "if ( passID == 1 ) {",
  290. "float pointSpecularWeight = KS_Skin_Specular( normal, pointVector, viewerDirection, uRoughness, uSpecularBrightness );",
  291. "totalSpecularLight += pointLightColor[ i ] * mSpecular.xyz * ( pointSpecularWeight * attenuation );",
  292. "}",
  293. "}",
  294. "#endif",
  295. // directional lights
  296. "#if NUM_DIR_LIGHTS > 0",
  297. "for( int i = 0; i < NUM_DIR_LIGHTS; i++ ) {",
  298. "vec3 dirVector = directionalLights[ i ].direction;",
  299. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  300. "totalDiffuseLight += directionalLights[ i ].color * dirDiffuseWeight;",
  301. "if ( passID == 1 ) {",
  302. "float dirSpecularWeight = KS_Skin_Specular( normal, dirVector, viewerDirection, uRoughness, uSpecularBrightness );",
  303. "totalSpecularLight += directionalLights[ i ].color * mSpecular.xyz * dirSpecularWeight;",
  304. "}",
  305. "}",
  306. "#endif",
  307. "outgoingLight += diffuseColor.rgb * ( totalDiffuseLight + totalSpecularLight );",
  308. "if ( passID == 0 ) {",
  309. "outgoingLight = sqrt( outgoingLight );",
  310. "} else if ( passID == 1 ) {",
  311. //"#define VERSION1",
  312. "#ifdef VERSION1",
  313. "vec3 nonblurColor = sqrt(outgoingLight );",
  314. "#else",
  315. "vec3 nonblurColor = outgoingLight;",
  316. "#endif",
  317. "vec3 blur1Color = texture2D( tBlur1, vUv ).xyz;",
  318. "vec3 blur2Color = texture2D( tBlur2, vUv ).xyz;",
  319. "vec3 blur3Color = texture2D( tBlur3, vUv ).xyz;",
  320. "vec3 blur4Color = texture2D( tBlur4, vUv ).xyz;",
  321. //"gl_FragColor = vec4( blur1Color, gl_FragColor.w );",
  322. //"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 );",
  323. //"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 );",
  324. "outgoingLight = vec3( vec3( 0.22, 0.437, 0.635 ) * nonblurColor + ",
  325. "vec3( 0.101, 0.355, 0.365 ) * blur1Color + ",
  326. "vec3( 0.119, 0.208, 0.0 ) * blur2Color + ",
  327. "vec3( 0.114, 0.0, 0.0 ) * blur3Color + ",
  328. "vec3( 0.444, 0.0, 0.0 ) * blur4Color );",
  329. "outgoingLight *= sqrt( colDiffuse.xyz );",
  330. "outgoingLight += ambientLightColor * diffuse * colDiffuse.xyz + totalSpecularLight;",
  331. "#ifndef VERSION1",
  332. "outgoingLight = sqrt( outgoingLight );",
  333. "#endif",
  334. "}",
  335. "gl_FragColor = vec4( outgoingLight, diffuseColor.a );", // TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects
  336. THREE.ShaderChunk[ "fog_fragment" ],
  337. "}"
  338. ].join( "\n" ),
  339. vertexShader: [
  340. "#ifdef VERTEX_TEXTURES",
  341. "uniform sampler2D tDisplacement;",
  342. "uniform float uDisplacementScale;",
  343. "uniform float uDisplacementBias;",
  344. "#endif",
  345. "varying vec3 vNormal;",
  346. "varying vec2 vUv;",
  347. "varying vec3 vViewPosition;",
  348. THREE.ShaderChunk[ "common" ],
  349. THREE.ShaderChunk[ "fog_pars_vertex" ],
  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. THREE.ShaderChunk[ "fog_vertex" ],
  366. "}",
  367. ].join( "\n" ),
  368. vertexShaderUV: [
  369. "varying vec3 vNormal;",
  370. "varying vec2 vUv;",
  371. "varying vec3 vViewPosition;",
  372. THREE.ShaderChunk[ "common" ],
  373. "void main() {",
  374. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  375. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  376. "vViewPosition = -mvPosition.xyz;",
  377. "vNormal = normalize( normalMatrix * normal );",
  378. "vUv = uv;",
  379. "gl_Position = vec4( uv.x * 2.0 - 1.0, uv.y * 2.0 - 1.0, 0.0, 1.0 );",
  380. "}"
  381. ].join( "\n" )
  382. },
  383. /* ------------------------------------------------------------------------------------------
  384. // Beckmann distribution function
  385. // - to be used in specular term of skin shader
  386. // - render a screen-aligned quad to precompute a 512 x 512 texture
  387. //
  388. // - from http://developer.nvidia.com/node/171
  389. ------------------------------------------------------------------------------------------ */
  390. "beckmann" : {
  391. uniforms: {},
  392. vertexShader: [
  393. "varying vec2 vUv;",
  394. "void main() {",
  395. "vUv = uv;",
  396. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  397. "}"
  398. ].join( "\n" ),
  399. fragmentShader: [
  400. "varying vec2 vUv;",
  401. "float PHBeckmann( float ndoth, float m ) {",
  402. "float alpha = acos( ndoth );",
  403. "float ta = tan( alpha );",
  404. "float val = 1.0 / ( m * m * pow( ndoth, 4.0 ) ) * exp( -( ta * ta ) / ( m * m ) );",
  405. "return val;",
  406. "}",
  407. "float KSTextureCompute( vec2 tex ) {",
  408. // Scale the value to fit within [0,1] invert upon lookup.
  409. "return 0.5 * pow( PHBeckmann( tex.x, tex.y ), 0.1 );",
  410. "}",
  411. "void main() {",
  412. "float x = KSTextureCompute( vUv );",
  413. "gl_FragColor = vec4( x, x, x, 1.0 );",
  414. "}"
  415. ].join( "\n" )
  416. }
  417. };