PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/js/ShaderSkin.js

https://github.com/Dinesh-Ramakrishnan/three.js
JavaScript | 752 lines | 397 code | 300 blank | 55 comment | 0 complexity | 4f380b662af6f64cf9197a2ac1975995 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  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. // - point and directional lights (use with "lights: true" material option)
  13. // - fog (use with "fog: true" material option)
  14. // - shadow maps
  15. //
  16. // ------------------------------------------------------------------------------------------ */
  17. 'skinSimple' : {
  18. uniforms: THREE.UniformsUtils.merge( [
  19. THREE.UniformsLib[ "fog" ],
  20. THREE.UniformsLib[ "lights" ],
  21. THREE.UniformsLib[ "shadowmap" ],
  22. {
  23. "tDiffuse" : { type: "t", value: 0, texture: null },
  24. "tBeckmann" : { type: "t", value: 1, texture: null },
  25. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  26. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  27. "uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
  28. "uOpacity": { type: "f", value: 1 },
  29. "uRoughness": { type: "f", value: 0.15 },
  30. "uSpecularBrightness": { type: "f", value: 0.75 },
  31. "uWrapRGB": { type: "v3", value: new THREE.Vector3( 0.75, 0.375, 0.1875 ) }
  32. }
  33. ] ),
  34. fragmentShader: [
  35. "uniform vec3 uAmbientColor;",
  36. "uniform vec3 uDiffuseColor;",
  37. "uniform vec3 uSpecularColor;",
  38. "uniform float uOpacity;",
  39. "uniform float uRoughness;",
  40. "uniform float uSpecularBrightness;",
  41. "uniform vec3 uWrapRGB;",
  42. "uniform sampler2D tDiffuse;",
  43. "uniform sampler2D tBeckmann;",
  44. "varying vec3 vNormal;",
  45. "varying vec2 vUv;",
  46. "uniform vec3 ambientLightColor;",
  47. "#if MAX_DIR_LIGHTS > 0",
  48. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  49. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  50. "#endif",
  51. "#if MAX_POINT_LIGHTS > 0",
  52. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  53. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  54. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  55. "#endif",
  56. "varying vec3 vViewPosition;",
  57. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  58. THREE.ShaderChunk[ "fog_pars_fragment" ],
  59. // Fresnel term
  60. "float fresnelReflectance( vec3 H, vec3 V, float F0 ) {",
  61. "float base = 1.0 - dot( V, H );",
  62. "float exponential = pow( base, 5.0 );",
  63. "return exponential + F0 * ( 1.0 - exponential );",
  64. "}",
  65. // Kelemen/Szirmay-Kalos specular BRDF
  66. "float KS_Skin_Specular( vec3 N,", // Bumped surface normal
  67. "vec3 L,", // Points to light
  68. "vec3 V,", // Points to eye
  69. "float m,", // Roughness
  70. "float rho_s", // Specular brightness
  71. ") {",
  72. "float result = 0.0;",
  73. "float ndotl = dot( N, L );",
  74. "if( ndotl > 0.0 ) {",
  75. "vec3 h = L + V;", // Unnormalized half-way vector
  76. "vec3 H = normalize( h );",
  77. "float ndoth = dot( N, H );",
  78. "float PH = pow( 2.0 * texture2D( tBeckmann, vec2( ndoth, m ) ).x, 10.0 );",
  79. "float F = fresnelReflectance( H, V, 0.028 );",
  80. "float frSpec = max( PH * F / dot( h, h ), 0.0 );",
  81. "result = ndotl * rho_s * frSpec;", // BRDF * dot(N,L) * rho_s
  82. "}",
  83. "return result;",
  84. "}",
  85. "void main() {",
  86. "gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
  87. "vec4 colDiffuse = texture2D( tDiffuse, vUv );",
  88. "colDiffuse.rgb *= colDiffuse.rgb;",
  89. "gl_FragColor = gl_FragColor * colDiffuse;",
  90. "vec3 normal = normalize( vNormal );",
  91. "vec3 viewPosition = normalize( vViewPosition );",
  92. // point lights
  93. "vec3 specularTotal = vec3( 0.0 );",
  94. "#if MAX_POINT_LIGHTS > 0",
  95. "vec3 pointTotal = vec3( 0.0 );",
  96. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  97. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  98. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  99. "float lDistance = 1.0;",
  100. "if ( pointLightDistance[ i ] > 0.0 )",
  101. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  102. "lVector = normalize( lVector );",
  103. "float pointDiffuseWeightFull = max( dot( normal, lVector ), 0.0 );",
  104. "float pointDiffuseWeightHalf = max( 0.5 * dot( normal, lVector ) + 0.5, 0.0 );",
  105. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), uWrapRGB );",
  106. "float pointSpecularWeight = KS_Skin_Specular( normal, lVector, viewPosition, uRoughness, uSpecularBrightness );",
  107. "pointTotal += lDistance * uDiffuseColor * pointLightColor[ i ] * pointDiffuseWeight;",
  108. "specularTotal += lDistance * uSpecularColor * pointLightColor[ i ] * pointSpecularWeight;",
  109. "}",
  110. "#endif",
  111. // directional lights
  112. "#if MAX_DIR_LIGHTS > 0",
  113. "vec3 dirTotal = vec3( 0.0 );",
  114. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  115. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  116. "vec3 dirVector = normalize( lDirection.xyz );",
  117. "float dirDiffuseWeightFull = max( dot( normal, dirVector ), 0.0 );",
  118. "float dirDiffuseWeightHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
  119. "vec3 dirDiffuseWeight = mix( vec3 ( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), uWrapRGB );",
  120. "float dirSpecularWeight = KS_Skin_Specular( normal, dirVector, viewPosition, uRoughness, uSpecularBrightness );",
  121. "dirTotal += uDiffuseColor * directionalLightColor[ i ] * dirDiffuseWeight;",
  122. "specularTotal += uSpecularColor * directionalLightColor[ i ] * dirSpecularWeight;",
  123. "}",
  124. "#endif",
  125. // all lights contribution summation
  126. "vec3 totalLight = vec3( 0.0 );",
  127. "#if MAX_DIR_LIGHTS > 0",
  128. "totalLight += dirTotal;",
  129. "#endif",
  130. "#if MAX_POINT_LIGHTS > 0",
  131. "totalLight += pointTotal;",
  132. "#endif",
  133. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalLight + ambientLightColor * uAmbientColor ) + specularTotal;",
  134. THREE.ShaderChunk[ "shadowmap_fragment" ],
  135. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  136. THREE.ShaderChunk[ "fog_fragment" ],
  137. "}"
  138. ].join("\n"),
  139. vertexShader: [
  140. "varying vec3 vNormal;",
  141. "varying vec2 vUv;",
  142. "varying vec3 vViewPosition;",
  143. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  144. "void main() {",
  145. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  146. "vViewPosition = -mvPosition.xyz;",
  147. "vNormal = normalMatrix * normal;",
  148. "vUv = uv;",
  149. "gl_Position = projectionMatrix * mvPosition;",
  150. THREE.ShaderChunk[ "shadowmap_vertex" ],
  151. "}"
  152. ].join( "\n" )
  153. },
  154. /* ------------------------------------------------------------------------------------------
  155. // Skin shader
  156. // - Blinn-Phong diffuse term (using normal + diffuse maps)
  157. // - subsurface scattering approximation by four blur layers
  158. // - physically based specular term (Kelemen/Szirmay-Kalos specular reflectance)
  159. //
  160. // - point and directional lights (use with "lights: true" material option)
  161. //
  162. // - based on Nvidia Advanced Skin Rendering GDC 2007 presentation
  163. // and GPU Gems 3 Chapter 14. Advanced Techniques for Realistic Real-Time Skin Rendering
  164. //
  165. // http://developer.download.nvidia.com/presentations/2007/gdc/Advanced_Skin.pdf
  166. // http://http.developer.nvidia.com/GPUGems3/gpugems3_ch14.html
  167. // ------------------------------------------------------------------------------------------ */
  168. 'skin' : {
  169. uniforms: THREE.UniformsUtils.merge( [
  170. THREE.UniformsLib[ "fog" ],
  171. THREE.UniformsLib[ "lights" ],
  172. {
  173. "passID": { type: "i", value: 0 },
  174. "tDiffuse" : { type: "t", value: 0, texture: null },
  175. "tNormal" : { type: "t", value: 1, texture: null },
  176. "tBlur1" : { type: "t", value: 2, texture: null },
  177. "tBlur2" : { type: "t", value: 3, texture: null },
  178. "tBlur3" : { type: "t", value: 4, texture: null },
  179. "tBlur4" : { type: "t", value: 5, texture: null },
  180. "tBeckmann" : { type: "t", value: 6, texture: null },
  181. "uNormalScale": { type: "f", value: 1.0 },
  182. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  183. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  184. "uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
  185. "uOpacity": { type: "f", value: 1 },
  186. "uRoughness": { type: "f", value: 0.15 },
  187. "uSpecularBrightness": { type: "f", value: 0.75 }
  188. }
  189. ] ),
  190. fragmentShader: [
  191. "uniform vec3 uAmbientColor;",
  192. "uniform vec3 uDiffuseColor;",
  193. "uniform vec3 uSpecularColor;",
  194. "uniform float uOpacity;",
  195. "uniform float uRoughness;",
  196. "uniform float uSpecularBrightness;",
  197. "uniform int passID;",
  198. "uniform sampler2D tDiffuse;",
  199. "uniform sampler2D tNormal;",
  200. "uniform sampler2D tBlur1;",
  201. "uniform sampler2D tBlur2;",
  202. "uniform sampler2D tBlur3;",
  203. "uniform sampler2D tBlur4;",
  204. "uniform sampler2D tBeckmann;",
  205. "uniform float uNormalScale;",
  206. "varying vec3 vTangent;",
  207. "varying vec3 vBinormal;",
  208. "varying vec3 vNormal;",
  209. "varying vec2 vUv;",
  210. "uniform vec3 ambientLightColor;",
  211. "#if MAX_DIR_LIGHTS > 0",
  212. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  213. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  214. "#endif",
  215. "#if MAX_POINT_LIGHTS > 0",
  216. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  217. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  218. "#endif",
  219. "varying vec3 vViewPosition;",
  220. THREE.ShaderChunk[ "fog_pars_fragment" ],
  221. "float fresnelReflectance( vec3 H, vec3 V, float F0 ) {",
  222. "float base = 1.0 - dot( V, H );",
  223. "float exponential = pow( base, 5.0 );",
  224. "return exponential + F0 * ( 1.0 - exponential );",
  225. "}",
  226. // Kelemen/Szirmay-Kalos specular BRDF
  227. "float KS_Skin_Specular( vec3 N,", // Bumped surface normal
  228. "vec3 L,", // Points to light
  229. "vec3 V,", // Points to eye
  230. "float m,", // Roughness
  231. "float rho_s", // Specular brightness
  232. ") {",
  233. "float result = 0.0;",
  234. "float ndotl = dot( N, L );",
  235. "if( ndotl > 0.0 ) {",
  236. "vec3 h = L + V;", // Unnormalized half-way vector
  237. "vec3 H = normalize( h );",
  238. "float ndoth = dot( N, H );",
  239. "float PH = pow( 2.0 * texture2D( tBeckmann, vec2( ndoth, m ) ).x, 10.0 );",
  240. "float F = fresnelReflectance( H, V, 0.028 );",
  241. "float frSpec = max( PH * F / dot( h, h ), 0.0 );",
  242. "result = ndotl * rho_s * frSpec;", // BRDF * dot(N,L) * rho_s
  243. "}",
  244. "return result;",
  245. "}",
  246. "void main() {",
  247. "gl_FragColor = vec4( 1.0 );",
  248. "vec4 mColor = vec4( uDiffuseColor, uOpacity );",
  249. "vec4 mSpecular = vec4( uSpecularColor, uOpacity );",
  250. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  251. "normalTex.xy *= uNormalScale;",
  252. "normalTex = normalize( normalTex );",
  253. "vec4 colDiffuse = texture2D( tDiffuse, vUv );",
  254. "colDiffuse *= colDiffuse;",
  255. "gl_FragColor = gl_FragColor * colDiffuse;",
  256. "mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
  257. "vec3 finalNormal = tsb * normalTex;",
  258. "vec3 normal = normalize( finalNormal );",
  259. "vec3 viewPosition = normalize( vViewPosition );",
  260. // point lights
  261. "vec3 specularTotal = vec3( 0.0 );",
  262. "#if MAX_POINT_LIGHTS > 0",
  263. "vec4 pointTotal = vec4( vec3( 0.0 ), 1.0 );",
  264. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  265. "vec3 pointVector = normalize( vPointLight[ i ].xyz );",
  266. "float pointDistance = vPointLight[ i ].w;",
  267. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  268. "pointTotal += pointDistance * vec4( pointLightColor[ i ], 1.0 ) * ( mColor * pointDiffuseWeight );",
  269. "if ( passID == 1 )",
  270. "specularTotal += pointDistance * mSpecular.xyz * pointLightColor[ i ] * KS_Skin_Specular( normal, pointVector, viewPosition, uRoughness, uSpecularBrightness );",
  271. "}",
  272. "#endif",
  273. // directional lights
  274. "#if MAX_DIR_LIGHTS > 0",
  275. "vec4 dirTotal = vec4( vec3( 0.0 ), 1.0 );",
  276. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  277. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  278. "vec3 dirVector = normalize( lDirection.xyz );",
  279. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  280. "dirTotal += vec4( directionalLightColor[ i ], 1.0 ) * ( mColor * dirDiffuseWeight );",
  281. "if ( passID == 1 )",
  282. "specularTotal += mSpecular.xyz * directionalLightColor[ i ] * KS_Skin_Specular( normal, dirVector, viewPosition, uRoughness, uSpecularBrightness );",
  283. "}",
  284. "#endif",
  285. // all lights contribution summation
  286. "vec4 totalLight = vec4( vec3( 0.0 ), uOpacity );",
  287. "#if MAX_DIR_LIGHTS > 0",
  288. "totalLight += dirTotal;",
  289. "#endif",
  290. "#if MAX_POINT_LIGHTS > 0",
  291. "totalLight += pointTotal;",
  292. "#endif",
  293. "gl_FragColor = gl_FragColor * totalLight;",
  294. "if ( passID == 0 ) {",
  295. "gl_FragColor = vec4( sqrt( gl_FragColor.xyz ), gl_FragColor.w );",
  296. "} else if ( passID == 1 ) {",
  297. //"#define VERSION1",
  298. "#ifdef VERSION1",
  299. "vec3 nonblurColor = sqrt( gl_FragColor.xyz );",
  300. "#else",
  301. "vec3 nonblurColor = gl_FragColor.xyz;",
  302. "#endif",
  303. "vec3 blur1Color = texture2D( tBlur1, vUv ).xyz;",
  304. "vec3 blur2Color = texture2D( tBlur2, vUv ).xyz;",
  305. "vec3 blur3Color = texture2D( tBlur3, vUv ).xyz;",
  306. "vec3 blur4Color = texture2D( tBlur4, vUv ).xyz;",
  307. //"gl_FragColor = vec4( blur1Color, gl_FragColor.w );",
  308. //"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 );",
  309. //"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 );",
  310. "gl_FragColor = vec4( vec3( 0.22, 0.437, 0.635 ) * nonblurColor + ",
  311. "vec3( 0.101, 0.355, 0.365 ) * blur1Color + ",
  312. "vec3( 0.119, 0.208, 0.0 ) * blur2Color + ",
  313. "vec3( 0.114, 0.0, 0.0 ) * blur3Color + ",
  314. "vec3( 0.444, 0.0, 0.0 ) * blur4Color",
  315. ", gl_FragColor.w );",
  316. "gl_FragColor.xyz *= pow( colDiffuse.xyz, vec3( 0.5 ) );",
  317. "gl_FragColor.xyz += ambientLightColor * uAmbientColor * colDiffuse.xyz + specularTotal;",
  318. "#ifndef VERSION1",
  319. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  320. "#endif",
  321. "}",
  322. THREE.ShaderChunk[ "fog_fragment" ],
  323. "}"
  324. ].join("\n"),
  325. vertexShader: [
  326. "attribute vec4 tangent;",
  327. "#ifdef VERTEX_TEXTURES",
  328. "uniform sampler2D tDisplacement;",
  329. "uniform float uDisplacementScale;",
  330. "uniform float uDisplacementBias;",
  331. "#endif",
  332. "varying vec3 vTangent;",
  333. "varying vec3 vBinormal;",
  334. "varying vec3 vNormal;",
  335. "varying vec2 vUv;",
  336. "#if MAX_POINT_LIGHTS > 0",
  337. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  338. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  339. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  340. "#endif",
  341. "varying vec3 vViewPosition;",
  342. "void main() {",
  343. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  344. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  345. "vViewPosition = -mvPosition.xyz;",
  346. "vNormal = normalize( normalMatrix * normal );",
  347. // tangent and binormal vectors
  348. "vTangent = normalize( normalMatrix * tangent.xyz );",
  349. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  350. "vBinormal = normalize( vBinormal );",
  351. "vUv = uv;",
  352. // point lights
  353. "#if MAX_POINT_LIGHTS > 0",
  354. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  355. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  356. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  357. "float lDistance = 1.0;",
  358. "if ( pointLightDistance[ i ] > 0.0 )",
  359. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  360. "lVector = normalize( lVector );",
  361. "vPointLight[ i ] = vec4( lVector, lDistance );",
  362. "}",
  363. "#endif",
  364. // displacement mapping
  365. "#ifdef VERTEX_TEXTURES",
  366. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  367. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  368. "vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
  369. "gl_Position = projectionMatrix * displacedPosition;",
  370. "#else",
  371. "gl_Position = projectionMatrix * mvPosition;",
  372. "#endif",
  373. "}"
  374. ].join("\n"),
  375. vertexShaderUV: [
  376. "attribute vec4 tangent;",
  377. "#ifdef VERTEX_TEXTURES",
  378. "uniform sampler2D tDisplacement;",
  379. "uniform float uDisplacementScale;",
  380. "uniform float uDisplacementBias;",
  381. "#endif",
  382. "varying vec3 vTangent;",
  383. "varying vec3 vBinormal;",
  384. "varying vec3 vNormal;",
  385. "varying vec2 vUv;",
  386. "#if MAX_POINT_LIGHTS > 0",
  387. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  388. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  389. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  390. "#endif",
  391. "varying vec3 vViewPosition;",
  392. "void main() {",
  393. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  394. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  395. "vViewPosition = -mvPosition.xyz;",
  396. "vNormal = normalize( normalMatrix * normal );",
  397. // tangent and binormal vectors
  398. "vTangent = normalize( normalMatrix * tangent.xyz );",
  399. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  400. "vBinormal = normalize( vBinormal );",
  401. "vUv = uv;",
  402. // point lights
  403. "#if MAX_POINT_LIGHTS > 0",
  404. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  405. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  406. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  407. "float lDistance = 1.0;",
  408. "if ( pointLightDistance[ i ] > 0.0 )",
  409. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  410. "lVector = normalize( lVector );",
  411. "vPointLight[ i ] = vec4( lVector, lDistance );",
  412. "}",
  413. "#endif",
  414. "gl_Position = vec4( uv.x * 2.0 - 1.0, uv.y * 2.0 - 1.0, 0.0, 1.0 );",
  415. "}"
  416. ].join("\n")
  417. },
  418. /* ------------------------------------------------------------------------------------------
  419. // Beckmann distribution function
  420. // - to be used in specular term of skin shader
  421. // - render a screen-aligned quad to precompute a 512 x 512 texture
  422. //
  423. // - from http://developer.nvidia.com/node/171
  424. ------------------------------------------------------------------------------------------ */
  425. "beckmann" : {
  426. uniforms: {},
  427. vertexShader: [
  428. "varying vec2 vUv;",
  429. "void main() {",
  430. "vUv = vec2( uv.x, 1.0 - uv.y );",
  431. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  432. "}"
  433. ].join("\n"),
  434. fragmentShader: [
  435. "varying vec2 vUv;",
  436. "float PHBeckmann( float ndoth, float m ) {",
  437. "float alpha = acos( ndoth );",
  438. "float ta = tan( alpha );",
  439. "float val = 1.0 / ( m * m * pow( ndoth, 4.0 ) ) * exp( -( ta * ta ) / ( m * m ) );",
  440. "return val;",
  441. "}",
  442. "float KSTextureCompute( vec2 tex ) {",
  443. // Scale the value to fit within [0,1] – invert upon lookup.
  444. "return 0.5 * pow( PHBeckmann( tex.x, tex.y ), 0.1 );",
  445. "}",
  446. "void main() {",
  447. "float x = KSTextureCompute( vUv );",
  448. "gl_FragColor = vec4( x, x, x, 1.0 );",
  449. "}"
  450. ].join("\n")
  451. }
  452. };