ShaderSkin.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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: 0, texture: null },
  28. "tBeckmann" : { type: "t", value: 1, texture: 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: 2, texture: null },
  36. "bumpScale" : { type: "f", value: 1 },
  37. "specularMap" : { type: "t", value: 3, texture: 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. "vViewPosition = -mvPosition.xyz;",
  172. "vNormal = normalMatrix * normal;",
  173. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  174. "gl_Position = projectionMatrix * mvPosition;",
  175. THREE.ShaderChunk[ "shadowmap_vertex" ],
  176. "}"
  177. ].join( "\n" )
  178. },
  179. /* ------------------------------------------------------------------------------------------
  180. // Skin shader
  181. // - Blinn-Phong diffuse term (using normal + diffuse maps)
  182. // - subsurface scattering approximation by four blur layers
  183. // - physically based specular term (Kelemen/Szirmay-Kalos specular reflectance)
  184. //
  185. // - point and directional lights (use with "lights: true" material option)
  186. //
  187. // - based on Nvidia Advanced Skin Rendering GDC 2007 presentation
  188. // and GPU Gems 3 Chapter 14. Advanced Techniques for Realistic Real-Time Skin Rendering
  189. //
  190. // http://developer.download.nvidia.com/presentations/2007/gdc/Advanced_Skin.pdf
  191. // http://http.developer.nvidia.com/GPUGems3/gpugems3_ch14.html
  192. // ------------------------------------------------------------------------------------------ */
  193. 'skin' : {
  194. uniforms: THREE.UniformsUtils.merge( [
  195. THREE.UniformsLib[ "fog" ],
  196. THREE.UniformsLib[ "lights" ],
  197. {
  198. "passID": { type: "i", value: 0 },
  199. "tDiffuse" : { type: "t", value: 0, texture: null },
  200. "tNormal" : { type: "t", value: 1, texture: null },
  201. "tBlur1" : { type: "t", value: 2, texture: null },
  202. "tBlur2" : { type: "t", value: 3, texture: null },
  203. "tBlur3" : { type: "t", value: 4, texture: null },
  204. "tBlur4" : { type: "t", value: 5, texture: null },
  205. "tBeckmann" : { type: "t", value: 6, texture: null },
  206. "uNormalScale": { type: "f", value: 1.0 },
  207. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  208. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  209. "uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
  210. "uOpacity": { type: "f", value: 1 },
  211. "uRoughness": { type: "f", value: 0.15 },
  212. "uSpecularBrightness": { type: "f", value: 0.75 }
  213. }
  214. ] ),
  215. fragmentShader: [
  216. "uniform vec3 uAmbientColor;",
  217. "uniform vec3 uDiffuseColor;",
  218. "uniform vec3 uSpecularColor;",
  219. "uniform float uOpacity;",
  220. "uniform float uRoughness;",
  221. "uniform float uSpecularBrightness;",
  222. "uniform int passID;",
  223. "uniform sampler2D tDiffuse;",
  224. "uniform sampler2D tNormal;",
  225. "uniform sampler2D tBlur1;",
  226. "uniform sampler2D tBlur2;",
  227. "uniform sampler2D tBlur3;",
  228. "uniform sampler2D tBlur4;",
  229. "uniform sampler2D tBeckmann;",
  230. "uniform float uNormalScale;",
  231. "varying vec3 vTangent;",
  232. "varying vec3 vBinormal;",
  233. "varying vec3 vNormal;",
  234. "varying vec2 vUv;",
  235. "uniform vec3 ambientLightColor;",
  236. "#if MAX_DIR_LIGHTS > 0",
  237. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  238. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  239. "#endif",
  240. "#if MAX_POINT_LIGHTS > 0",
  241. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  242. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  243. "#endif",
  244. "varying vec3 vViewPosition;",
  245. THREE.ShaderChunk[ "fog_pars_fragment" ],
  246. "float fresnelReflectance( vec3 H, vec3 V, float F0 ) {",
  247. "float base = 1.0 - dot( V, H );",
  248. "float exponential = pow( base, 5.0 );",
  249. "return exponential + F0 * ( 1.0 - exponential );",
  250. "}",
  251. // Kelemen/Szirmay-Kalos specular BRDF
  252. "float KS_Skin_Specular( vec3 N,", // Bumped surface normal
  253. "vec3 L,", // Points to light
  254. "vec3 V,", // Points to eye
  255. "float m,", // Roughness
  256. "float rho_s", // Specular brightness
  257. ") {",
  258. "float result = 0.0;",
  259. "float ndotl = dot( N, L );",
  260. "if( ndotl > 0.0 ) {",
  261. "vec3 h = L + V;", // Unnormalized half-way vector
  262. "vec3 H = normalize( h );",
  263. "float ndoth = dot( N, H );",
  264. "float PH = pow( 2.0 * texture2D( tBeckmann, vec2( ndoth, m ) ).x, 10.0 );",
  265. "float F = fresnelReflectance( H, V, 0.028 );",
  266. "float frSpec = max( PH * F / dot( h, h ), 0.0 );",
  267. "result = ndotl * rho_s * frSpec;", // BRDF * dot(N,L) * rho_s
  268. "}",
  269. "return result;",
  270. "}",
  271. "void main() {",
  272. "gl_FragColor = vec4( 1.0 );",
  273. "vec4 mColor = vec4( uDiffuseColor, uOpacity );",
  274. "vec4 mSpecular = vec4( uSpecularColor, uOpacity );",
  275. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  276. "normalTex.xy *= uNormalScale;",
  277. "normalTex = normalize( normalTex );",
  278. "vec4 colDiffuse = texture2D( tDiffuse, vUv );",
  279. "colDiffuse *= colDiffuse;",
  280. "gl_FragColor = gl_FragColor * colDiffuse;",
  281. "mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
  282. "vec3 finalNormal = tsb * normalTex;",
  283. "vec3 normal = normalize( finalNormal );",
  284. "vec3 viewPosition = normalize( vViewPosition );",
  285. // point lights
  286. "vec3 specularTotal = vec3( 0.0 );",
  287. "#if MAX_POINT_LIGHTS > 0",
  288. "vec4 pointTotal = vec4( vec3( 0.0 ), 1.0 );",
  289. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  290. "vec3 pointVector = normalize( vPointLight[ i ].xyz );",
  291. "float pointDistance = vPointLight[ i ].w;",
  292. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  293. "pointTotal += pointDistance * vec4( pointLightColor[ i ], 1.0 ) * ( mColor * pointDiffuseWeight );",
  294. "if ( passID == 1 )",
  295. "specularTotal += pointDistance * mSpecular.xyz * pointLightColor[ i ] * KS_Skin_Specular( normal, pointVector, viewPosition, uRoughness, uSpecularBrightness );",
  296. "}",
  297. "#endif",
  298. // directional lights
  299. "#if MAX_DIR_LIGHTS > 0",
  300. "vec4 dirTotal = vec4( vec3( 0.0 ), 1.0 );",
  301. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  302. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  303. "vec3 dirVector = normalize( lDirection.xyz );",
  304. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  305. "dirTotal += vec4( directionalLightColor[ i ], 1.0 ) * ( mColor * dirDiffuseWeight );",
  306. "if ( passID == 1 )",
  307. "specularTotal += mSpecular.xyz * directionalLightColor[ i ] * KS_Skin_Specular( normal, dirVector, viewPosition, uRoughness, uSpecularBrightness );",
  308. "}",
  309. "#endif",
  310. // all lights contribution summation
  311. "vec4 totalLight = vec4( vec3( 0.0 ), uOpacity );",
  312. "#if MAX_DIR_LIGHTS > 0",
  313. "totalLight += dirTotal;",
  314. "#endif",
  315. "#if MAX_POINT_LIGHTS > 0",
  316. "totalLight += pointTotal;",
  317. "#endif",
  318. "gl_FragColor = gl_FragColor * totalLight;",
  319. "if ( passID == 0 ) {",
  320. "gl_FragColor = vec4( sqrt( gl_FragColor.xyz ), gl_FragColor.w );",
  321. "} else if ( passID == 1 ) {",
  322. //"#define VERSION1",
  323. "#ifdef VERSION1",
  324. "vec3 nonblurColor = sqrt( gl_FragColor.xyz );",
  325. "#else",
  326. "vec3 nonblurColor = gl_FragColor.xyz;",
  327. "#endif",
  328. "vec3 blur1Color = texture2D( tBlur1, vUv ).xyz;",
  329. "vec3 blur2Color = texture2D( tBlur2, vUv ).xyz;",
  330. "vec3 blur3Color = texture2D( tBlur3, vUv ).xyz;",
  331. "vec3 blur4Color = texture2D( tBlur4, vUv ).xyz;",
  332. //"gl_FragColor = vec4( blur1Color, gl_FragColor.w );",
  333. //"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 );",
  334. //"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 );",
  335. "gl_FragColor = vec4( vec3( 0.22, 0.437, 0.635 ) * nonblurColor + ",
  336. "vec3( 0.101, 0.355, 0.365 ) * blur1Color + ",
  337. "vec3( 0.119, 0.208, 0.0 ) * blur2Color + ",
  338. "vec3( 0.114, 0.0, 0.0 ) * blur3Color + ",
  339. "vec3( 0.444, 0.0, 0.0 ) * blur4Color",
  340. ", gl_FragColor.w );",
  341. "gl_FragColor.xyz *= pow( colDiffuse.xyz, vec3( 0.5 ) );",
  342. "gl_FragColor.xyz += ambientLightColor * uAmbientColor * colDiffuse.xyz + specularTotal;",
  343. "#ifndef VERSION1",
  344. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  345. "#endif",
  346. "}",
  347. THREE.ShaderChunk[ "fog_fragment" ],
  348. "}"
  349. ].join("\n"),
  350. vertexShader: [
  351. "attribute vec4 tangent;",
  352. "#ifdef VERTEX_TEXTURES",
  353. "uniform sampler2D tDisplacement;",
  354. "uniform float uDisplacementScale;",
  355. "uniform float uDisplacementBias;",
  356. "#endif",
  357. "varying vec3 vTangent;",
  358. "varying vec3 vBinormal;",
  359. "varying vec3 vNormal;",
  360. "varying vec2 vUv;",
  361. "#if MAX_POINT_LIGHTS > 0",
  362. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  363. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  364. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  365. "#endif",
  366. "varying vec3 vViewPosition;",
  367. "void main() {",
  368. "vec4 mPosition = modelMatrix * vec4( position, 1.0 );",
  369. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  370. "vViewPosition = -mvPosition.xyz;",
  371. "vNormal = normalize( normalMatrix * normal );",
  372. // tangent and binormal vectors
  373. "vTangent = normalize( normalMatrix * tangent.xyz );",
  374. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  375. "vBinormal = normalize( vBinormal );",
  376. "vUv = uv;",
  377. // point lights
  378. "#if MAX_POINT_LIGHTS > 0",
  379. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  380. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  381. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  382. "float lDistance = 1.0;",
  383. "if ( pointLightDistance[ i ] > 0.0 )",
  384. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  385. "lVector = normalize( lVector );",
  386. "vPointLight[ i ] = vec4( lVector, lDistance );",
  387. "}",
  388. "#endif",
  389. // displacement mapping
  390. "#ifdef VERTEX_TEXTURES",
  391. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  392. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  393. "vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
  394. "gl_Position = projectionMatrix * displacedPosition;",
  395. "#else",
  396. "gl_Position = projectionMatrix * mvPosition;",
  397. "#endif",
  398. "}"
  399. ].join("\n"),
  400. vertexShaderUV: [
  401. "attribute vec4 tangent;",
  402. "#ifdef VERTEX_TEXTURES",
  403. "uniform sampler2D tDisplacement;",
  404. "uniform float uDisplacementScale;",
  405. "uniform float uDisplacementBias;",
  406. "#endif",
  407. "varying vec3 vTangent;",
  408. "varying vec3 vBinormal;",
  409. "varying vec3 vNormal;",
  410. "varying vec2 vUv;",
  411. "#if MAX_POINT_LIGHTS > 0",
  412. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  413. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  414. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  415. "#endif",
  416. "varying vec3 vViewPosition;",
  417. "void main() {",
  418. "vec4 mPosition = modelMatrix * vec4( position, 1.0 );",
  419. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  420. "vViewPosition = -mvPosition.xyz;",
  421. "vNormal = normalize( normalMatrix * normal );",
  422. // tangent and binormal vectors
  423. "vTangent = normalize( normalMatrix * tangent.xyz );",
  424. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  425. "vBinormal = normalize( vBinormal );",
  426. "vUv = uv;",
  427. // point lights
  428. "#if MAX_POINT_LIGHTS > 0",
  429. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  430. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  431. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  432. "float lDistance = 1.0;",
  433. "if ( pointLightDistance[ i ] > 0.0 )",
  434. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  435. "lVector = normalize( lVector );",
  436. "vPointLight[ i ] = vec4( lVector, lDistance );",
  437. "}",
  438. "#endif",
  439. "gl_Position = vec4( uv.x * 2.0 - 1.0, uv.y * 2.0 - 1.0, 0.0, 1.0 );",
  440. "}"
  441. ].join("\n")
  442. },
  443. /* ------------------------------------------------------------------------------------------
  444. // Beckmann distribution function
  445. // - to be used in specular term of skin shader
  446. // - render a screen-aligned quad to precompute a 512 x 512 texture
  447. //
  448. // - from http://developer.nvidia.com/node/171
  449. ------------------------------------------------------------------------------------------ */
  450. "beckmann" : {
  451. uniforms: {},
  452. vertexShader: [
  453. "varying vec2 vUv;",
  454. "void main() {",
  455. "vUv = uv;",
  456. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  457. "}"
  458. ].join("\n"),
  459. fragmentShader: [
  460. "varying vec2 vUv;",
  461. "float PHBeckmann( float ndoth, float m ) {",
  462. "float alpha = acos( ndoth );",
  463. "float ta = tan( alpha );",
  464. "float val = 1.0 / ( m * m * pow( ndoth, 4.0 ) ) * exp( -( ta * ta ) / ( m * m ) );",
  465. "return val;",
  466. "}",
  467. "float KSTextureCompute( vec2 tex ) {",
  468. // Scale the value to fit within [0,1] invert upon lookup.
  469. "return 0.5 * pow( PHBeckmann( tex.x, tex.y ), 0.1 );",
  470. "}",
  471. "void main() {",
  472. "float x = KSTextureCompute( vUv );",
  473. "gl_FragColor = vec4( x, x, x, 1.0 );",
  474. "}"
  475. ].join("\n")
  476. }
  477. };