ShaderSkin.js 21 KB

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