ShaderSkin.js 20 KB

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