ShaderSkin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. */
  5. THREE.ShaderSkin = {
  6. /* ------------------------------------------------------------------------------------------
  7. // Skin shader
  8. // - Blinn-Phong
  9. // - normal + diffuse maps
  10. // - four blur layers
  11. // - point and directional lights (use with "lights: true" material option)
  12. //
  13. // - based on Nvidia Advanced Skin Rendering GDC 2007 presentation
  14. // http://developer.download.nvidia.com/presentations/2007/gdc/Advanced_Skin.pdf
  15. // ------------------------------------------------------------------------------------------ */
  16. 'skin' : {
  17. uniforms: THREE.UniformsUtils.merge( [
  18. THREE.UniformsLib[ "fog" ],
  19. THREE.UniformsLib[ "lights" ],
  20. {
  21. "passID": { type: "i", value: 0 },
  22. "tDiffuse" : { type: "t", value: 0, texture: null },
  23. "tNormal" : { type: "t", value: 1, texture: null },
  24. "tBlur1" : { type: "t", value: 2, texture: null },
  25. "tBlur2" : { type: "t", value: 3, texture: null },
  26. "tBlur3" : { type: "t", value: 4, texture: null },
  27. "tBlur4" : { type: "t", value: 5, texture: null },
  28. "uNormalScale": { type: "f", value: 1.0 },
  29. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  30. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  31. "uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
  32. "uShininess": { type: "f", value: 30 },
  33. "uOpacity": { type: "f", value: 1 }
  34. }
  35. ] ),
  36. fragmentShader: [
  37. "uniform vec3 uAmbientColor;",
  38. "uniform vec3 uDiffuseColor;",
  39. "uniform vec3 uSpecularColor;",
  40. "uniform float uShininess;",
  41. "uniform float uOpacity;",
  42. "uniform int passID;",
  43. "uniform sampler2D tDiffuse;",
  44. "uniform sampler2D tNormal;",
  45. "uniform sampler2D tBlur1;",
  46. "uniform sampler2D tBlur2;",
  47. "uniform sampler2D tBlur3;",
  48. "uniform sampler2D tBlur4;",
  49. "uniform float uNormalScale;",
  50. "varying vec3 vTangent;",
  51. "varying vec3 vBinormal;",
  52. "varying vec3 vNormal;",
  53. "varying vec2 vUv;",
  54. "uniform vec3 ambientLightColor;",
  55. "#if MAX_DIR_LIGHTS > 0",
  56. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  57. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  58. "#endif",
  59. "#if MAX_POINT_LIGHTS > 0",
  60. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  61. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  62. "#endif",
  63. "varying vec3 vViewPosition;",
  64. THREE.ShaderChunk[ "fog_pars_fragment" ],
  65. "void main() {",
  66. "gl_FragColor = vec4( 1.0 );",
  67. "vec4 mColor = vec4( uDiffuseColor, uOpacity );",
  68. "vec4 mSpecular = vec4( uSpecularColor, uOpacity );",
  69. "vec3 specularTex = vec3( 1.0 );",
  70. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  71. "normalTex.xy *= uNormalScale;",
  72. "normalTex = normalize( normalTex );",
  73. "vec4 colDiffuse = texture2D( tDiffuse, vUv );",
  74. "colDiffuse *= colDiffuse;",
  75. "gl_FragColor = gl_FragColor * pow( colDiffuse, vec4( 0.5 ) );",
  76. "mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
  77. "vec3 finalNormal = tsb * normalTex;",
  78. "vec3 normal = normalize( finalNormal );",
  79. "vec3 viewPosition = normalize( vViewPosition );",
  80. // point lights
  81. "vec4 specularTotal = vec4( vec3( 0.0 ), 1.0 );",
  82. "#if MAX_POINT_LIGHTS > 0",
  83. "vec4 pointTotal = vec4( vec3( 0.0 ), 1.0 );",
  84. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  85. "vec3 pointVector = normalize( vPointLight[ i ].xyz );",
  86. "vec3 pointHalfVector = normalize( vPointLight[ i ].xyz + viewPosition );",
  87. "float pointDistance = vPointLight[ i ].w;",
  88. "float pointDotNormalHalf = dot( normal, pointHalfVector );",
  89. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  90. "float pointSpecularWeight = 0.0;",
  91. "if ( passID == 1 && pointDotNormalHalf >= 0.0 )",
  92. "pointSpecularWeight = specularTex.r * pow( pointDotNormalHalf, uShininess );",
  93. "pointTotal += pointDistance * vec4( pointLightColor[ i ], 1.0 ) * ( mColor * pointDiffuseWeight );",
  94. "specularTotal += pointDistance * vec4( pointLightColor[ i ], 1.0 ) * ( mSpecular * pointSpecularWeight * pointDiffuseWeight );",
  95. "}",
  96. "#endif",
  97. // directional lights
  98. "#if MAX_DIR_LIGHTS > 0",
  99. "vec4 dirTotal = vec4( vec3( 0.0 ), 1.0 );",
  100. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  101. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  102. "vec3 dirVector = normalize( lDirection.xyz );",
  103. "vec3 dirHalfVector = normalize( lDirection.xyz + viewPosition );",
  104. "float dirDotNormalHalf = dot( normal, dirHalfVector );",
  105. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  106. "float dirSpecularWeight = 0.0;",
  107. "if ( passID == 1 && dirDotNormalHalf >= 0.0 )",
  108. "dirSpecularWeight = specularTex.r * pow( dirDotNormalHalf, uShininess );",
  109. "dirTotal += vec4( directionalLightColor[ i ], 1.0 ) * ( mColor * dirDiffuseWeight );",
  110. "specularTotal += vec4( directionalLightColor[ i ], 1.0 ) * ( mSpecular * dirSpecularWeight * dirDiffuseWeight );",
  111. "}",
  112. "#endif",
  113. // all lights contribution summation
  114. "vec4 totalLight = vec4( vec3( 0.0 ), uOpacity );",
  115. "#if MAX_DIR_LIGHTS > 0",
  116. "totalLight += dirTotal;",
  117. "#endif",
  118. "#if MAX_POINT_LIGHTS > 0",
  119. "totalLight += pointTotal;",
  120. "#endif",
  121. "gl_FragColor = gl_FragColor * totalLight;",
  122. "if ( passID == 0 ) {",
  123. "gl_FragColor = vec4( sqrt( gl_FragColor.xyz ), gl_FragColor.w );",
  124. "} else if ( passID == 1 ) {",
  125. //"#define VERSION1",
  126. "#ifdef VERSION1",
  127. "vec3 nonblurColor = sqrt( gl_FragColor.xyz );",
  128. "#else",
  129. "vec3 nonblurColor = gl_FragColor.xyz;",
  130. "#endif",
  131. "vec3 blur1Color = texture2D( tBlur1, vUv ).xyz;",
  132. "vec3 blur2Color = texture2D( tBlur2, vUv ).xyz;",
  133. "vec3 blur3Color = texture2D( tBlur3, vUv ).xyz;",
  134. "vec3 blur4Color = texture2D( tBlur4, vUv ).xyz;",
  135. //"gl_FragColor = vec4( blur1Color, gl_FragColor.w );",
  136. //"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 );",
  137. //"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 );",
  138. "gl_FragColor = vec4( vec3( 0.22, 0.437, 0.635 ) * nonblurColor + ",
  139. "vec3( 0.101, 0.355, 0.365 ) * blur1Color + ",
  140. "vec3( 0.119, 0.208, 0.0 ) * blur2Color + ",
  141. "vec3( 0.114, 0.0, 0.0 ) * blur3Color + ",
  142. "vec3( 0.444, 0.0, 0.0 ) * blur4Color",
  143. ", gl_FragColor.w );",
  144. "gl_FragColor.xyz *= pow( colDiffuse.xyz, vec3( 0.5 ) );",
  145. "gl_FragColor += specularTotal;",
  146. "gl_FragColor.xyz += ambientLightColor * uAmbientColor * colDiffuse.xyz;",
  147. "#ifndef VERSION1",
  148. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  149. "#endif",
  150. "}",
  151. THREE.ShaderChunk[ "fog_fragment" ],
  152. "}"
  153. ].join("\n"),
  154. vertexShader: [
  155. "attribute vec4 tangent;",
  156. "#ifdef VERTEX_TEXTURES",
  157. "uniform sampler2D tDisplacement;",
  158. "uniform float uDisplacementScale;",
  159. "uniform float uDisplacementBias;",
  160. "#endif",
  161. "varying vec3 vTangent;",
  162. "varying vec3 vBinormal;",
  163. "varying vec3 vNormal;",
  164. "varying vec2 vUv;",
  165. "#if MAX_POINT_LIGHTS > 0",
  166. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  167. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  168. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  169. "#endif",
  170. "varying vec3 vViewPosition;",
  171. "void main() {",
  172. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  173. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  174. "vViewPosition = -mvPosition.xyz;",
  175. "vNormal = normalize( normalMatrix * normal );",
  176. // tangent and binormal vectors
  177. "vTangent = normalize( normalMatrix * tangent.xyz );",
  178. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  179. "vBinormal = normalize( vBinormal );",
  180. "vUv = uv;",
  181. // point lights
  182. "#if MAX_POINT_LIGHTS > 0",
  183. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  184. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  185. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  186. "float lDistance = 1.0;",
  187. "if ( pointLightDistance[ i ] > 0.0 )",
  188. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  189. "lVector = normalize( lVector );",
  190. "vPointLight[ i ] = vec4( lVector, lDistance );",
  191. "}",
  192. "#endif",
  193. // displacement mapping
  194. "#ifdef VERTEX_TEXTURES",
  195. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  196. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  197. "vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
  198. "gl_Position = projectionMatrix * displacedPosition;",
  199. "#else",
  200. "gl_Position = projectionMatrix * mvPosition;",
  201. "#endif",
  202. "}"
  203. ].join("\n"),
  204. vertexShaderUV: [
  205. "attribute vec4 tangent;",
  206. "#ifdef VERTEX_TEXTURES",
  207. "uniform sampler2D tDisplacement;",
  208. "uniform float uDisplacementScale;",
  209. "uniform float uDisplacementBias;",
  210. "#endif",
  211. "varying vec3 vTangent;",
  212. "varying vec3 vBinormal;",
  213. "varying vec3 vNormal;",
  214. "varying vec2 vUv;",
  215. "#if MAX_POINT_LIGHTS > 0",
  216. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  217. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  218. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  219. "#endif",
  220. "varying vec3 vViewPosition;",
  221. "void main() {",
  222. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  223. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  224. "vViewPosition = -mvPosition.xyz;",
  225. "vNormal = normalize( normalMatrix * normal );",
  226. // tangent and binormal vectors
  227. "vTangent = normalize( normalMatrix * tangent.xyz );",
  228. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  229. "vBinormal = normalize( vBinormal );",
  230. "vUv = uv;",
  231. // point lights
  232. "#if MAX_POINT_LIGHTS > 0",
  233. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  234. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  235. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  236. "float lDistance = 1.0;",
  237. "if ( pointLightDistance[ i ] > 0.0 )",
  238. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  239. "lVector = normalize( lVector );",
  240. "vPointLight[ i ] = vec4( lVector, lDistance );",
  241. "}",
  242. "#endif",
  243. "gl_Position = vec4( uv.x * 2.0 - 1.0, uv.y * 2.0 - 1.0, 0.0, 1.0 );",
  244. "}"
  245. ].join("\n")
  246. }
  247. };