ShaderSkin.js 21 KB

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