ShaderSkin.js 21 KB

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