NormalDisplacementShader.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. * Normal map shader
  5. * - Blinn-Phong
  6. * - normal + diffuse + specular + AO + displacement + reflection + shadow maps
  7. * - point and directional lights (use with "lights: true" material option)
  8. */
  9. THREE.NormalDisplacementShader = {
  10. uniforms: THREE.UniformsUtils.merge( [
  11. THREE.UniformsLib[ "fog" ],
  12. THREE.UniformsLib[ "lights" ],
  13. THREE.UniformsLib[ "shadowmap" ],
  14. {
  15. "enableAO" : { type: "i", value: 0 },
  16. "enableDiffuse" : { type: "i", value: 0 },
  17. "enableSpecular" : { type: "i", value: 0 },
  18. "enableReflection" : { type: "i", value: 0 },
  19. "enableDisplacement": { type: "i", value: 0 },
  20. "tDisplacement": { type: "t", value: null }, // must go first as this is vertex texture
  21. "tDiffuse" : { type: "t", value: null },
  22. "tCube" : { type: "t", value: null },
  23. "tNormal" : { type: "t", value: null },
  24. "tSpecular" : { type: "t", value: null },
  25. "tAO" : { type: "t", value: null },
  26. "uNormalScale": { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  27. "uDisplacementBias": { type: "f", value: 0.0 },
  28. "uDisplacementScale": { type: "f", value: 1.0 },
  29. "diffuse": { type: "c", value: new THREE.Color( 0xffffff ) },
  30. "specular": { type: "c", value: new THREE.Color( 0x111111 ) },
  31. "shininess": { type: "f", value: 30 },
  32. "opacity": { type: "f", value: 1 },
  33. "refractionRatio": { type: "f", value: 0.98 },
  34. "reflectivity": { type: "f", value: 0.5 },
  35. "uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },
  36. "uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  37. }
  38. ] ),
  39. fragmentShader: [
  40. "uniform vec3 diffuse;",
  41. "uniform vec3 specular;",
  42. "uniform float shininess;",
  43. "uniform float opacity;",
  44. "uniform bool enableDiffuse;",
  45. "uniform bool enableSpecular;",
  46. "uniform bool enableAO;",
  47. "uniform bool enableReflection;",
  48. "uniform sampler2D tDiffuse;",
  49. "uniform sampler2D tNormal;",
  50. "uniform sampler2D tSpecular;",
  51. "uniform sampler2D tAO;",
  52. "uniform samplerCube tCube;",
  53. "uniform vec2 uNormalScale;",
  54. "uniform float refractionRatio;",
  55. "uniform float reflectivity;",
  56. "varying vec3 vTangent;",
  57. "varying vec3 vBinormal;",
  58. "varying vec3 vNormal;",
  59. "varying vec2 vUv;",
  60. "uniform vec3 ambientLightColor;",
  61. "#if MAX_DIR_LIGHTS > 0",
  62. " uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  63. " uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  64. "#endif",
  65. "#if MAX_HEMI_LIGHTS > 0",
  66. " uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  67. " uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  68. " uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  69. "#endif",
  70. "#if MAX_POINT_LIGHTS > 0",
  71. " uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  72. " uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  73. " uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  74. "#endif",
  75. "#if MAX_SPOT_LIGHTS > 0",
  76. " uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  77. " uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  78. " uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  79. " uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
  80. " uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  81. " uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  82. "#endif",
  83. "varying vec3 vWorldPosition;",
  84. "varying vec3 vViewPosition;",
  85. THREE.ShaderChunk[ "common" ],
  86. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  87. THREE.ShaderChunk[ "fog_pars_fragment" ],
  88. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  89. "void main() {",
  90. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  91. " vec3 outgoingLight = vec3( 0.0 );",
  92. " vec4 diffuseColor = vec4( diffuse, opacity );",
  93. " vec3 specularTex = vec3( 1.0 );",
  94. " vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  95. " normalTex.xy *= uNormalScale;",
  96. " normalTex = normalize( normalTex );",
  97. " if( enableDiffuse ) {",
  98. " #ifdef GAMMA_INPUT",
  99. " vec4 texelColor = texture2D( tDiffuse, vUv );",
  100. " texelColor.xyz *= texelColor.xyz;",
  101. " diffuseColor *= texelColor;",
  102. " #else",
  103. " diffuseColor *= texture2D( tDiffuse, vUv );",
  104. " #endif",
  105. " }",
  106. " if( enableAO ) {",
  107. " diffuseColor.rgb *= texture2D( tAO, vUv ).xyz;", // actually, the AOmap should be modulating ambient light sources only,
  108. // and should use the second set of UVs, as is done elsewhere in the library
  109. " }",
  110. THREE.ShaderChunk[ "alphatest_fragment" ],
  111. " if( enableSpecular )",
  112. " specularTex = texture2D( tSpecular, vUv ).xyz;",
  113. " mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",
  114. " vec3 finalNormal = tsb * normalTex;",
  115. " #ifdef FLIP_SIDED",
  116. " finalNormal = - finalNormal;",
  117. " #endif",
  118. " vec3 normal = normalize( finalNormal );",
  119. " vec3 viewPosition = normalize( vViewPosition );",
  120. " vec3 totalDiffuseLight = vec3( 0.0 );",
  121. " vec3 totalSpecularLight = vec3( 0.0 );",
  122. // point lights
  123. " #if MAX_POINT_LIGHTS > 0",
  124. " for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  125. " vec3 pointVector = pointLightPosition[ i ] + vViewPosition.xyz;",
  126. " float pointDistance = 1.0;",
  127. " if ( pointLightDistance[ i ] > 0.0 )",
  128. " pointDistance = 1.0 - min( ( length( pointVector ) / pointLightDistance[ i ] ), 1.0 );",
  129. " pointVector = normalize( pointVector );",
  130. // diffuse
  131. " float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  132. " totalDiffuseLight += pointDistance * pointLightColor[ i ] * pointDiffuseWeight;",
  133. // specular
  134. " vec3 pointHalfVector = normalize( pointVector + viewPosition );",
  135. " float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  136. " float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  137. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  138. " vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( pointVector, pointHalfVector ), 0.0 ), 5.0 );",
  139. " totalSpecularLight += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",
  140. " }",
  141. " #endif",
  142. // spot lights
  143. " #if MAX_SPOT_LIGHTS > 0",
  144. " for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  145. " vec3 spotVector = spotLightPosition[ i ] + vViewPosition.xyz;",
  146. " float spotDistance = 1.0;",
  147. " if ( spotLightDistance[ i ] > 0.0 )",
  148. " spotDistance = 1.0 - min( ( length( spotVector ) / spotLightDistance[ i ] ), 1.0 );",
  149. " spotVector = normalize( spotVector );",
  150. " float spotEffect = dot( spotLightDirection[ i ], spotVector );",
  151. " if ( spotEffect > spotLightAngleCos[ i ] ) {",
  152. " spotEffect = max( pow( max( spotEffect, 0.0 ), spotLightExponent[ i ] ), 0.0 );",
  153. // diffuse
  154. " float spotDiffuseWeight = max( dot( normal, spotVector ), 0.0 );",
  155. " totalDiffuseLight += spotDistance * spotLightColor[ i ] * spotDiffuseWeight * spotEffect;",
  156. // specular
  157. " vec3 spotHalfVector = normalize( spotVector + viewPosition );",
  158. " float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
  159. " float spotSpecularWeight = specularTex.r * max( pow( spotDotNormalHalf, shininess ), 0.0 );",
  160. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  161. " vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( spotVector, spotHalfVector ), 0.0 ), 5.0 );",
  162. " totalSpecularLight += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * spotDistance * specularNormalization * spotEffect;",
  163. " }",
  164. " }",
  165. " #endif",
  166. // directional lights
  167. " #if MAX_DIR_LIGHTS > 0",
  168. " for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  169. " vec3 dirVector = directionalLightDirection[ i ];",
  170. // diffuse
  171. " float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  172. " totalDiffuseLight += directionalLightColor[ i ] * dirDiffuseWeight;",
  173. // specular
  174. " vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  175. " float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  176. " float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  177. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  178. " vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( dirVector, dirHalfVector ), 0.0 ), 5.0 );",
  179. " totalSpecularLight += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  180. " }",
  181. " #endif",
  182. // hemisphere lights
  183. " #if MAX_HEMI_LIGHTS > 0",
  184. " for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  185. " vec3 lVector = hemisphereLightDirection[ i ];",
  186. // diffuse
  187. " float dotProduct = dot( normal, lVector );",
  188. " float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  189. " vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  190. " totalDiffuseLight += hemiColor;",
  191. // specular (sky light)
  192. " vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  193. " float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  194. " float hemiSpecularWeightSky = specularTex.r * max( pow( max( hemiDotNormalHalfSky, 0.0 ), shininess ), 0.0 );",
  195. //
  196. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  197. " vec3 schlickSky = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( lVector, hemiHalfVectorSky ), 0.0 ), 5.0 );",
  198. " totalSpecularLight += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) );",
  199. " }",
  200. " #endif",
  201. " #ifdef METAL",
  202. " outgoingLight += diffuseColor.xyz * ( totalDiffuseLight + ambientLightColor + totalSpecularLight );",
  203. " #else",
  204. " outgoingLight += diffuseColor.xyz * ( totalDiffuseLight + ambientLightColor ) + totalSpecularLight;",
  205. " #endif",
  206. " if ( enableReflection ) {",
  207. " vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );",
  208. " vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );",
  209. " #ifdef ENVMAP_MODE_REFLECTION",
  210. " vec3 vReflect = reflect( cameraToVertex, worldNormal );",
  211. " #else",
  212. " vec3 vReflect = refract( cameraToVertex, worldNormal, refractionRatio );",
  213. " #endif",
  214. " vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  215. " #ifdef GAMMA_INPUT",
  216. " cubeColor.xyz *= cubeColor.xyz;",
  217. " #endif",
  218. " outgoingLight = mix( outgoingLight, cubeColor.xyz, specularTex.r * reflectivity );",
  219. " }",
  220. THREE.ShaderChunk[ "shadowmap_fragment" ],
  221. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  222. THREE.ShaderChunk[ "fog_fragment" ],
  223. " gl_FragColor = vec4( outgoingLight, diffuseColor.a );",
  224. "}"
  225. ].join( "\n" ),
  226. vertexShader: [
  227. "attribute vec4 tangent;",
  228. "uniform vec2 uOffset;",
  229. "uniform vec2 uRepeat;",
  230. "uniform bool enableDisplacement;",
  231. "#ifdef VERTEX_TEXTURES",
  232. " uniform sampler2D tDisplacement;",
  233. " uniform float uDisplacementScale;",
  234. " uniform float uDisplacementBias;",
  235. "#endif",
  236. "varying vec3 vTangent;",
  237. "varying vec3 vBinormal;",
  238. "varying vec3 vNormal;",
  239. "varying vec2 vUv;",
  240. "varying vec3 vWorldPosition;",
  241. "varying vec3 vViewPosition;",
  242. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  243. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  244. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  245. "void main() {",
  246. THREE.ShaderChunk[ "skinbase_vertex" ],
  247. THREE.ShaderChunk[ "skinnormal_vertex" ],
  248. // normal, tangent and binormal vectors
  249. " #ifdef USE_SKINNING",
  250. " vNormal = normalize( normalMatrix * skinnedNormal.xyz );",
  251. " vec4 skinnedTangent = skinMatrix * vec4( tangent.xyz, 0.0 );",
  252. " vTangent = normalize( normalMatrix * skinnedTangent.xyz );",
  253. " #else",
  254. " vNormal = normalize( normalMatrix * normal );",
  255. " vTangent = normalize( normalMatrix * tangent.xyz );",
  256. " #endif",
  257. " vBinormal = normalize( cross( vNormal, vTangent ) * tangent.w );",
  258. " vUv = uv * uRepeat + uOffset;",
  259. // displacement mapping
  260. " vec3 displacedPosition;",
  261. " #ifdef VERTEX_TEXTURES",
  262. " if ( enableDisplacement ) {",
  263. " vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  264. " float df = uDisplacementScale * dv.x + uDisplacementBias;",
  265. " displacedPosition = position + normalize( normal ) * df;",
  266. " } else {",
  267. " #ifdef USE_SKINNING",
  268. " vec4 skinVertex = bindMatrix * vec4( position, 1.0 );",
  269. " vec4 skinned = vec4( 0.0 );",
  270. " skinned += boneMatX * skinVertex * skinWeight.x;",
  271. " skinned += boneMatY * skinVertex * skinWeight.y;",
  272. " skinned += boneMatZ * skinVertex * skinWeight.z;",
  273. " skinned += boneMatW * skinVertex * skinWeight.w;",
  274. " skinned = bindMatrixInverse * skinned;",
  275. " displacedPosition = skinned.xyz;",
  276. " #else",
  277. " displacedPosition = position;",
  278. " #endif",
  279. " }",
  280. " #else",
  281. " #ifdef USE_SKINNING",
  282. " vec4 skinVertex = bindMatrix * vec4( position, 1.0 );",
  283. " vec4 skinned = vec4( 0.0 );",
  284. " skinned += boneMatX * skinVertex * skinWeight.x;",
  285. " skinned += boneMatY * skinVertex * skinWeight.y;",
  286. " skinned += boneMatZ * skinVertex * skinWeight.z;",
  287. " skinned += boneMatW * skinVertex * skinWeight.w;",
  288. " skinned = bindMatrixInverse * skinned;",
  289. " displacedPosition = skinned.xyz;",
  290. " #else",
  291. " displacedPosition = position;",
  292. " #endif",
  293. " #endif",
  294. //
  295. " vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
  296. " vec4 worldPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
  297. " gl_Position = projectionMatrix * mvPosition;",
  298. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  299. //
  300. " vWorldPosition = worldPosition.xyz;",
  301. " vViewPosition = - mvPosition.xyz;",
  302. // shadows
  303. " #ifdef USE_SHADOWMAP",
  304. " for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  305. " vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;",
  306. " }",
  307. " #endif",
  308. "}"
  309. ].join( "\n" )
  310. };