ShaderSkin.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. "uniform float pointLightDecay[ MAX_POINT_LIGHTS ];",
  72. "#endif",
  73. "varying vec3 vViewPosition;",
  74. THREE.ShaderChunk[ "common" ],
  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 attenuation = calcLightAttenuation( length( lVector ), pointLightDistance[ i ], pointLightDecay[i] );",
  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 += attenuation * diffuse * pointLightColor[ i ] * pointDiffuseWeight;",
  135. "specularTotal += attenuation * 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. "vec3 dirVector = transformDirection( directionalLightDirection[ i ], viewMatrix );",
  143. "float dirDiffuseWeightFull = max( dot( normal, dirVector ), 0.0 );",
  144. "float dirDiffuseWeightHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
  145. "vec3 dirDiffuseWeight = mix( vec3 ( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), uWrapRGB );",
  146. "float dirSpecularWeight = KS_Skin_Specular( normal, dirVector, viewPosition, uRoughness, uSpecularBrightness );",
  147. "dirTotal += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
  148. "specularTotal += specular * directionalLightColor[ i ] * dirSpecularWeight * specularStrength;",
  149. "}",
  150. "#endif",
  151. // hemisphere lights
  152. "#if MAX_HEMI_LIGHTS > 0",
  153. "vec3 hemiTotal = vec3( 0.0 );",
  154. "for ( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  155. "vec3 lVector = transformDirection( hemisphereLightDirection[ i ], viewMatrix );",
  156. "float dotProduct = dot( normal, lVector );",
  157. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  158. "hemiTotal += diffuse * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  159. // specular (sky light)
  160. "float hemiSpecularWeight = 0.0;",
  161. "hemiSpecularWeight += KS_Skin_Specular( normal, lVector, viewPosition, uRoughness, uSpecularBrightness );",
  162. // specular (ground light)
  163. "vec3 lVectorGround = -lVector;",
  164. "hemiSpecularWeight += KS_Skin_Specular( normal, lVectorGround, viewPosition, uRoughness, uSpecularBrightness );",
  165. "specularTotal += specular * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight ) * hemiSpecularWeight * specularStrength;",
  166. "}",
  167. "#endif",
  168. // all lights contribution summation
  169. "vec3 totalLight = vec3( 0.0 );",
  170. "#if MAX_DIR_LIGHTS > 0",
  171. "totalLight += dirTotal;",
  172. "#endif",
  173. "#if MAX_POINT_LIGHTS > 0",
  174. "totalLight += pointTotal;",
  175. "#endif",
  176. "#if MAX_HEMI_LIGHTS > 0",
  177. "totalLight += hemiTotal;",
  178. "#endif",
  179. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalLight + ambientLightColor * diffuse ) + specularTotal;",
  180. THREE.ShaderChunk[ "shadowmap_fragment" ],
  181. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  182. THREE.ShaderChunk[ "fog_fragment" ],
  183. "}"
  184. ].join("\n"),
  185. vertexShader: [
  186. "uniform vec4 offsetRepeat;",
  187. "varying vec3 vNormal;",
  188. "varying vec2 vUv;",
  189. "varying vec3 vViewPosition;",
  190. THREE.ShaderChunk[ "common" ],
  191. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  192. "void main() {",
  193. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  194. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  195. "vViewPosition = -mvPosition.xyz;",
  196. "vNormal = normalize( normalMatrix * normal );",
  197. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  198. "gl_Position = projectionMatrix * mvPosition;",
  199. THREE.ShaderChunk[ "shadowmap_vertex" ],
  200. "}"
  201. ].join( "\n" )
  202. },
  203. /* ------------------------------------------------------------------------------------------
  204. // Skin shader
  205. // - Blinn-Phong diffuse term (using normal + diffuse maps)
  206. // - subsurface scattering approximation by four blur layers
  207. // - physically based specular term (Kelemen/Szirmay-Kalos specular reflectance)
  208. //
  209. // - point and directional lights (use with "lights: true" material option)
  210. //
  211. // - based on Nvidia Advanced Skin Rendering GDC 2007 presentation
  212. // and GPU Gems 3 Chapter 14. Advanced Techniques for Realistic Real-Time Skin Rendering
  213. //
  214. // http://developer.download.nvidia.com/presentations/2007/gdc/Advanced_Skin.pdf
  215. // http://http.developer.nvidia.com/GPUGems3/gpugems3_ch14.html
  216. // ------------------------------------------------------------------------------------------ */
  217. 'skin' : {
  218. uniforms: THREE.UniformsUtils.merge( [
  219. THREE.UniformsLib[ "fog" ],
  220. THREE.UniformsLib[ "lights" ],
  221. {
  222. "passID": { type: "i", value: 0 },
  223. "tDiffuse" : { type: "t", value: null },
  224. "tNormal" : { type: "t", value: null },
  225. "tBlur1" : { type: "t", value: null },
  226. "tBlur2" : { type: "t", value: null },
  227. "tBlur3" : { type: "t", value: null },
  228. "tBlur4" : { type: "t", value: null },
  229. "tBeckmann" : { type: "t", value: null },
  230. "uNormalScale": { type: "f", value: 1.0 },
  231. "diffuse": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  232. "specular": { type: "c", value: new THREE.Color( 0x111111 ) },
  233. "opacity": { type: "f", value: 1 },
  234. "uRoughness": { type: "f", value: 0.15 },
  235. "uSpecularBrightness": { type: "f", value: 0.75 }
  236. }
  237. ] ),
  238. fragmentShader: [
  239. "uniform vec3 diffuse;",
  240. "uniform vec3 specular;",
  241. "uniform float opacity;",
  242. "uniform float uRoughness;",
  243. "uniform float uSpecularBrightness;",
  244. "uniform int passID;",
  245. "uniform sampler2D tDiffuse;",
  246. "uniform sampler2D tNormal;",
  247. "uniform sampler2D tBlur1;",
  248. "uniform sampler2D tBlur2;",
  249. "uniform sampler2D tBlur3;",
  250. "uniform sampler2D tBlur4;",
  251. "uniform sampler2D tBeckmann;",
  252. "uniform float uNormalScale;",
  253. "varying vec3 vTangent;",
  254. "varying vec3 vBinormal;",
  255. "varying vec3 vNormal;",
  256. "varying vec2 vUv;",
  257. "uniform vec3 ambientLightColor;",
  258. "#if MAX_DIR_LIGHTS > 0",
  259. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  260. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  261. "#endif",
  262. "#if MAX_POINT_LIGHTS > 0",
  263. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  264. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  265. "#endif",
  266. "varying vec3 vViewPosition;",
  267. THREE.ShaderChunk[ "common" ],
  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. "vec3 dirVector = transformDirection( directionalLightDirection[ i ], viewMatrix );",
  326. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  327. "dirTotal += vec4( directionalLightColor[ i ], 1.0 ) * ( mColor * dirDiffuseWeight );",
  328. "if ( passID == 1 )",
  329. "specularTotal += mSpecular.xyz * directionalLightColor[ i ] * KS_Skin_Specular( normal, dirVector, viewPosition, uRoughness, uSpecularBrightness );",
  330. "}",
  331. "#endif",
  332. // all lights contribution summation
  333. "vec4 totalLight = vec4( vec3( 0.0 ), opacity );",
  334. "#if MAX_DIR_LIGHTS > 0",
  335. "totalLight += dirTotal;",
  336. "#endif",
  337. "#if MAX_POINT_LIGHTS > 0",
  338. "totalLight += pointTotal;",
  339. "#endif",
  340. "gl_FragColor = gl_FragColor * totalLight;",
  341. "if ( passID == 0 ) {",
  342. "gl_FragColor = vec4( sqrt( gl_FragColor.xyz ), gl_FragColor.w );",
  343. "} else if ( passID == 1 ) {",
  344. //"#define VERSION1",
  345. "#ifdef VERSION1",
  346. "vec3 nonblurColor = sqrt( gl_FragColor.xyz );",
  347. "#else",
  348. "vec3 nonblurColor = gl_FragColor.xyz;",
  349. "#endif",
  350. "vec3 blur1Color = texture2D( tBlur1, vUv ).xyz;",
  351. "vec3 blur2Color = texture2D( tBlur2, vUv ).xyz;",
  352. "vec3 blur3Color = texture2D( tBlur3, vUv ).xyz;",
  353. "vec3 blur4Color = texture2D( tBlur4, vUv ).xyz;",
  354. //"gl_FragColor = vec4( blur1Color, gl_FragColor.w );",
  355. //"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 );",
  356. //"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 );",
  357. "gl_FragColor = vec4( vec3( 0.22, 0.437, 0.635 ) * nonblurColor + ",
  358. "vec3( 0.101, 0.355, 0.365 ) * blur1Color + ",
  359. "vec3( 0.119, 0.208, 0.0 ) * blur2Color + ",
  360. "vec3( 0.114, 0.0, 0.0 ) * blur3Color + ",
  361. "vec3( 0.444, 0.0, 0.0 ) * blur4Color",
  362. ", gl_FragColor.w );",
  363. "gl_FragColor.xyz *= pow( colDiffuse.xyz, vec3( 0.5 ) );",
  364. "gl_FragColor.xyz += ambientLightColor * diffuse * colDiffuse.xyz + specularTotal;",
  365. "#ifndef VERSION1",
  366. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  367. "#endif",
  368. "}",
  369. THREE.ShaderChunk[ "fog_fragment" ],
  370. "}"
  371. ].join("\n"),
  372. vertexShader: [
  373. "attribute vec4 tangent;",
  374. "#ifdef VERTEX_TEXTURES",
  375. "uniform sampler2D tDisplacement;",
  376. "uniform float uDisplacementScale;",
  377. "uniform float uDisplacementBias;",
  378. "#endif",
  379. "varying vec3 vTangent;",
  380. "varying vec3 vBinormal;",
  381. "varying vec3 vNormal;",
  382. "varying vec2 vUv;",
  383. "#if MAX_POINT_LIGHTS > 0",
  384. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  385. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  386. "uniform float pointLightDecay[ MAX_POINT_LIGHTS ];",
  387. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  388. "#endif",
  389. "varying vec3 vViewPosition;",
  390. THREE.ShaderChunk[ "common" ],
  391. "void main() {",
  392. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  393. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  394. "vViewPosition = -mvPosition.xyz;",
  395. "vNormal = normalize( normalMatrix * normal );",
  396. // tangent and binormal vectors
  397. "vTangent = normalize( normalMatrix * tangent.xyz );",
  398. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  399. "vBinormal = normalize( vBinormal );",
  400. "vUv = uv;",
  401. // point lights
  402. "#if MAX_POINT_LIGHTS > 0",
  403. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  404. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  405. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  406. "float attenuation = calcLightAttenuation( length( lVector ), pointLightDistance[ i ], pointLightDecay[i] );",
  407. "lVector = normalize( lVector );",
  408. "vPointLight[ i ] = vec4( lVector, attenuation );",
  409. "}",
  410. "#endif",
  411. // displacement mapping
  412. "#ifdef VERTEX_TEXTURES",
  413. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  414. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  415. "vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
  416. "gl_Position = projectionMatrix * displacedPosition;",
  417. "#else",
  418. "gl_Position = projectionMatrix * mvPosition;",
  419. "#endif",
  420. "}"
  421. ].join("\n"),
  422. vertexShaderUV: [
  423. "attribute vec4 tangent;",
  424. "#ifdef VERTEX_TEXTURES",
  425. "uniform sampler2D tDisplacement;",
  426. "uniform float uDisplacementScale;",
  427. "uniform float uDisplacementBias;",
  428. "#endif",
  429. "varying vec3 vTangent;",
  430. "varying vec3 vBinormal;",
  431. "varying vec3 vNormal;",
  432. "varying vec2 vUv;",
  433. "#if MAX_POINT_LIGHTS > 0",
  434. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  435. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  436. "uniform float pointLightDecay[ MAX_POINT_LIGHTS ];",
  437. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  438. "#endif",
  439. "varying vec3 vViewPosition;",
  440. THREE.ShaderChunk[ "common" ],
  441. "void main() {",
  442. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  443. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  444. "vViewPosition = -mvPosition.xyz;",
  445. "vNormal = normalize( normalMatrix * normal );",
  446. // tangent and binormal vectors
  447. "vTangent = normalize( normalMatrix * tangent.xyz );",
  448. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  449. "vBinormal = normalize( vBinormal );",
  450. "vUv = uv;",
  451. // point lights
  452. "#if MAX_POINT_LIGHTS > 0",
  453. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  454. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  455. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  456. "float attenuation = calcLightAttenuation( length( lVector ), pointLightDistance[ i ], pointLightDecay[i] );",
  457. "lVector = normalize( lVector );",
  458. "vPointLight[ i ] = vec4( lVector, attenuation );",
  459. "}",
  460. "#endif",
  461. "gl_Position = vec4( uv.x * 2.0 - 1.0, uv.y * 2.0 - 1.0, 0.0, 1.0 );",
  462. "}"
  463. ].join("\n")
  464. },
  465. /* ------------------------------------------------------------------------------------------
  466. // Beckmann distribution function
  467. // - to be used in specular term of skin shader
  468. // - render a screen-aligned quad to precompute a 512 x 512 texture
  469. //
  470. // - from http://developer.nvidia.com/node/171
  471. ------------------------------------------------------------------------------------------ */
  472. "beckmann" : {
  473. uniforms: {},
  474. vertexShader: [
  475. "varying vec2 vUv;",
  476. "void main() {",
  477. "vUv = uv;",
  478. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  479. "}"
  480. ].join("\n"),
  481. fragmentShader: [
  482. "varying vec2 vUv;",
  483. "float PHBeckmann( float ndoth, float m ) {",
  484. "float alpha = acos( ndoth );",
  485. "float ta = tan( alpha );",
  486. "float val = 1.0 / ( m * m * pow( ndoth, 4.0 ) ) * exp( -( ta * ta ) / ( m * m ) );",
  487. "return val;",
  488. "}",
  489. "float KSTextureCompute( vec2 tex ) {",
  490. // Scale the value to fit within [0,1] invert upon lookup.
  491. "return 0.5 * pow( PHBeckmann( tex.x, tex.y ), 0.1 );",
  492. "}",
  493. "void main() {",
  494. "float x = KSTextureCompute( vUv );",
  495. "gl_FragColor = vec4( x, x, x, 1.0 );",
  496. "}"
  497. ].join("\n")
  498. }
  499. };