ShaderSkin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. */
  5. THREE.ShaderSkin = {
  6. /* ------------------------------------------------------------------------------------------
  7. // Skin shader
  8. // - Blinn-Phong diffuse term (using normal + diffuse maps)
  9. // - subsurface scattering approximation by four blur layers
  10. // - physically based specular term (Kelemen/Szirmay-Kalos specular reflectance)
  11. //
  12. // - point and directional lights (use with "lights: true" material option)
  13. //
  14. // - based on Nvidia Advanced Skin Rendering GDC 2007 presentation
  15. // and GPU Gems 3 Chapter 14. Advanced Techniques for Realistic Real-Time Skin Rendering
  16. //
  17. // http://developer.download.nvidia.com/presentations/2007/gdc/Advanced_Skin.pdf
  18. // http://http.developer.nvidia.com/GPUGems3/gpugems3_ch14.html
  19. // ------------------------------------------------------------------------------------------ */
  20. 'skin' : {
  21. uniforms: THREE.UniformsUtils.merge( [
  22. THREE.UniformsLib[ "fog" ],
  23. THREE.UniformsLib[ "lights" ],
  24. {
  25. "passID": { type: "i", value: 0 },
  26. "tDiffuse" : { type: "t", value: 0, texture: null },
  27. "tNormal" : { type: "t", value: 1, texture: null },
  28. "tBlur1" : { type: "t", value: 2, texture: null },
  29. "tBlur2" : { type: "t", value: 3, texture: null },
  30. "tBlur3" : { type: "t", value: 4, texture: null },
  31. "tBlur4" : { type: "t", value: 5, texture: null },
  32. "tBeckmann" : { type: "t", value: 6, texture: null },
  33. "uNormalScale": { type: "f", value: 1.0 },
  34. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  35. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  36. "uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
  37. "uOpacity": { type: "f", value: 1 },
  38. "uRoughness": { type: "f", value: 0.15 },
  39. "uSpecularBrightness": { type: "f", value: 0.75 }
  40. }
  41. ] ),
  42. fragmentShader: [
  43. "uniform vec3 uAmbientColor;",
  44. "uniform vec3 uDiffuseColor;",
  45. "uniform vec3 uSpecularColor;",
  46. "uniform float uOpacity;",
  47. "uniform float uRoughness;",
  48. "uniform float uSpecularBrightness;",
  49. "uniform int passID;",
  50. "uniform sampler2D tDiffuse;",
  51. "uniform sampler2D tNormal;",
  52. "uniform sampler2D tBlur1;",
  53. "uniform sampler2D tBlur2;",
  54. "uniform sampler2D tBlur3;",
  55. "uniform sampler2D tBlur4;",
  56. "uniform sampler2D tBeckmann;",
  57. "uniform float uNormalScale;",
  58. "varying vec3 vTangent;",
  59. "varying vec3 vBinormal;",
  60. "varying vec3 vNormal;",
  61. "varying vec2 vUv;",
  62. "uniform vec3 ambientLightColor;",
  63. "#if MAX_DIR_LIGHTS > 0",
  64. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  65. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  66. "#endif",
  67. "#if MAX_POINT_LIGHTS > 0",
  68. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  69. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  70. "#endif",
  71. "varying vec3 vViewPosition;",
  72. THREE.ShaderChunk[ "fog_pars_fragment" ],
  73. "float fresnelReflectance( vec3 H, vec3 V, float F0 ) {",
  74. "float base = 1.0 - dot( V, H );",
  75. "float exponential = pow( base, 5.0 );",
  76. "return exponential + F0 * ( 1.0 - exponential );",
  77. "}",
  78. // Kelemen/Szirmay-Kalos specular BRDF
  79. "float KS_Skin_Specular( vec3 N,", // Bumped surface normal
  80. "vec3 L,", // Points to light
  81. "vec3 V,", // Points to eye
  82. "float m,", // Roughness
  83. "float rho_s", // Specular brightness
  84. ") {",
  85. "float result = 0.0;",
  86. "float ndotl = dot( N, L );",
  87. "if( ndotl > 0.0 ) {",
  88. "vec3 h = L + V;", // Unnormalized half-way vector
  89. "vec3 H = normalize( h );",
  90. "float ndoth = dot( N, H );",
  91. "float PH = pow( 2.0 * texture2D( tBeckmann, vec2( ndoth, m ) ).x, 10.0 );",
  92. "float F = fresnelReflectance( H, V, 0.028 );",
  93. "float frSpec = max( PH * F / dot( h, h ), 0.0 );",
  94. "result = ndotl * rho_s * frSpec;", // BRDF * dot(N,L) * rho_s
  95. "}",
  96. "return result;",
  97. "}",
  98. "void main() {",
  99. "gl_FragColor = vec4( 1.0 );",
  100. "vec4 mColor = vec4( uDiffuseColor, uOpacity );",
  101. "vec4 mSpecular = vec4( uSpecularColor, uOpacity );",
  102. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  103. "normalTex.xy *= uNormalScale;",
  104. "normalTex = normalize( normalTex );",
  105. "vec4 colDiffuse = texture2D( tDiffuse, vUv );",
  106. "colDiffuse *= colDiffuse;",
  107. "gl_FragColor = gl_FragColor * pow( colDiffuse, vec4( 0.5 ) );",
  108. "mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
  109. "vec3 finalNormal = tsb * normalTex;",
  110. "vec3 normal = normalize( finalNormal );",
  111. "vec3 viewPosition = normalize( vViewPosition );",
  112. // point lights
  113. "vec3 specularTotal = vec3( 0.0 );",
  114. "#if MAX_POINT_LIGHTS > 0",
  115. "vec4 pointTotal = vec4( vec3( 0.0 ), 1.0 );",
  116. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  117. "vec3 pointVector = normalize( vPointLight[ i ].xyz );",
  118. "float pointDistance = vPointLight[ i ].w;",
  119. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  120. "pointTotal += pointDistance * vec4( pointLightColor[ i ], 1.0 ) * ( mColor * pointDiffuseWeight );",
  121. "if ( passID == 1 )",
  122. "specularTotal += pointDistance * mSpecular.xyz * pointLightColor[ i ] * KS_Skin_Specular( normal, pointVector, viewPosition, uRoughness, uSpecularBrightness );",
  123. "}",
  124. "#endif",
  125. // directional lights
  126. "#if MAX_DIR_LIGHTS > 0",
  127. "vec4 dirTotal = vec4( vec3( 0.0 ), 1.0 );",
  128. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  129. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  130. "vec3 dirVector = normalize( lDirection.xyz );",
  131. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  132. "dirTotal += vec4( directionalLightColor[ i ], 1.0 ) * ( mColor * dirDiffuseWeight );",
  133. "if ( passID == 1 )",
  134. "specularTotal += mSpecular.xyz * directionalLightColor[ i ] * KS_Skin_Specular( normal, dirVector, viewPosition, uRoughness, uSpecularBrightness );",
  135. "}",
  136. "#endif",
  137. // all lights contribution summation
  138. "vec4 totalLight = vec4( vec3( 0.0 ), uOpacity );",
  139. "#if MAX_DIR_LIGHTS > 0",
  140. "totalLight += dirTotal;",
  141. "#endif",
  142. "#if MAX_POINT_LIGHTS > 0",
  143. "totalLight += pointTotal;",
  144. "#endif",
  145. "gl_FragColor = gl_FragColor * totalLight;",
  146. "if ( passID == 0 ) {",
  147. "gl_FragColor = vec4( sqrt( gl_FragColor.xyz ), gl_FragColor.w );",
  148. "} else if ( passID == 1 ) {",
  149. //"#define VERSION1",
  150. "#ifdef VERSION1",
  151. "vec3 nonblurColor = sqrt( gl_FragColor.xyz );",
  152. "#else",
  153. "vec3 nonblurColor = gl_FragColor.xyz;",
  154. "#endif",
  155. "vec3 blur1Color = texture2D( tBlur1, vUv ).xyz;",
  156. "vec3 blur2Color = texture2D( tBlur2, vUv ).xyz;",
  157. "vec3 blur3Color = texture2D( tBlur3, vUv ).xyz;",
  158. "vec3 blur4Color = texture2D( tBlur4, vUv ).xyz;",
  159. //"gl_FragColor = vec4( blur1Color, gl_FragColor.w );",
  160. //"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 );",
  161. //"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 );",
  162. "gl_FragColor = vec4( vec3( 0.22, 0.437, 0.635 ) * nonblurColor + ",
  163. "vec3( 0.101, 0.355, 0.365 ) * blur1Color + ",
  164. "vec3( 0.119, 0.208, 0.0 ) * blur2Color + ",
  165. "vec3( 0.114, 0.0, 0.0 ) * blur3Color + ",
  166. "vec3( 0.444, 0.0, 0.0 ) * blur4Color",
  167. ", gl_FragColor.w );",
  168. "gl_FragColor.xyz *= pow( colDiffuse.xyz, vec3( 0.5 ) );",
  169. "gl_FragColor.xyz += ambientLightColor * uAmbientColor * colDiffuse.xyz + specularTotal;",
  170. "#ifndef VERSION1",
  171. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  172. "#endif",
  173. "}",
  174. THREE.ShaderChunk[ "fog_fragment" ],
  175. "}"
  176. ].join("\n"),
  177. vertexShader: [
  178. "attribute vec4 tangent;",
  179. "#ifdef VERTEX_TEXTURES",
  180. "uniform sampler2D tDisplacement;",
  181. "uniform float uDisplacementScale;",
  182. "uniform float uDisplacementBias;",
  183. "#endif",
  184. "varying vec3 vTangent;",
  185. "varying vec3 vBinormal;",
  186. "varying vec3 vNormal;",
  187. "varying vec2 vUv;",
  188. "#if MAX_POINT_LIGHTS > 0",
  189. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  190. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  191. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  192. "#endif",
  193. "varying vec3 vViewPosition;",
  194. "void main() {",
  195. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  196. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  197. "vViewPosition = -mvPosition.xyz;",
  198. "vNormal = normalize( normalMatrix * normal );",
  199. // tangent and binormal vectors
  200. "vTangent = normalize( normalMatrix * tangent.xyz );",
  201. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  202. "vBinormal = normalize( vBinormal );",
  203. "vUv = uv;",
  204. // point lights
  205. "#if MAX_POINT_LIGHTS > 0",
  206. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  207. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  208. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  209. "float lDistance = 1.0;",
  210. "if ( pointLightDistance[ i ] > 0.0 )",
  211. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  212. "lVector = normalize( lVector );",
  213. "vPointLight[ i ] = vec4( lVector, lDistance );",
  214. "}",
  215. "#endif",
  216. // displacement mapping
  217. "#ifdef VERTEX_TEXTURES",
  218. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  219. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  220. "vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
  221. "gl_Position = projectionMatrix * displacedPosition;",
  222. "#else",
  223. "gl_Position = projectionMatrix * mvPosition;",
  224. "#endif",
  225. "}"
  226. ].join("\n"),
  227. vertexShaderUV: [
  228. "attribute vec4 tangent;",
  229. "#ifdef VERTEX_TEXTURES",
  230. "uniform sampler2D tDisplacement;",
  231. "uniform float uDisplacementScale;",
  232. "uniform float uDisplacementBias;",
  233. "#endif",
  234. "varying vec3 vTangent;",
  235. "varying vec3 vBinormal;",
  236. "varying vec3 vNormal;",
  237. "varying vec2 vUv;",
  238. "#if MAX_POINT_LIGHTS > 0",
  239. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  240. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  241. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  242. "#endif",
  243. "varying vec3 vViewPosition;",
  244. "void main() {",
  245. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  246. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  247. "vViewPosition = -mvPosition.xyz;",
  248. "vNormal = normalize( normalMatrix * normal );",
  249. // tangent and binormal vectors
  250. "vTangent = normalize( normalMatrix * tangent.xyz );",
  251. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  252. "vBinormal = normalize( vBinormal );",
  253. "vUv = uv;",
  254. // point lights
  255. "#if MAX_POINT_LIGHTS > 0",
  256. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  257. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  258. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  259. "float lDistance = 1.0;",
  260. "if ( pointLightDistance[ i ] > 0.0 )",
  261. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  262. "lVector = normalize( lVector );",
  263. "vPointLight[ i ] = vec4( lVector, lDistance );",
  264. "}",
  265. "#endif",
  266. "gl_Position = vec4( uv.x * 2.0 - 1.0, uv.y * 2.0 - 1.0, 0.0, 1.0 );",
  267. "}"
  268. ].join("\n")
  269. },
  270. /* ------------------------------------------------------------------------------------------
  271. // Beckmann distribution function
  272. // - to be used in specular term of skin shader
  273. // - render a screen-aligned quad to precompute a 512 x 512 texture
  274. //
  275. // - from http://developer.nvidia.com/node/171
  276. ------------------------------------------------------------------------------------------ */
  277. "beckmann" : {
  278. uniforms: {},
  279. vertexShader: [
  280. "varying vec2 vUv;",
  281. "void main() {",
  282. "vUv = vec2( uv.x, 1.0 - uv.y );",
  283. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  284. "}"
  285. ].join("\n"),
  286. fragmentShader: [
  287. "varying vec2 vUv;",
  288. "float PHBeckmann( float ndoth, float m ) {",
  289. "float alpha = acos( ndoth );",
  290. "float ta = tan( alpha );",
  291. "float val = 1.0 / ( m * m * pow( ndoth, 4.0 ) ) * exp( -( ta * ta ) / ( m * m ) );",
  292. "return val;",
  293. "}",
  294. "float KSTextureCompute( vec2 tex ) {",
  295. // Scale the value to fit within [0,1] – invert upon lookup.
  296. "return 0.5 * pow( PHBeckmann( tex.x, tex.y ), 0.1 );",
  297. "}",
  298. "void main() {",
  299. "float x = KSTextureCompute( vUv );",
  300. "gl_FragColor = vec4( x, x, x, 1.0 );",
  301. "}"
  302. ].join("\n")
  303. }
  304. };