SkinShader.js 17 KB

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