ShaderSkin.js 21 KB

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