ShaderSkin.js 21 KB

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