NormalDisplacementShader.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  38. }
  39. ] ),
  40. fragmentShader: [
  41. "uniform vec3 diffuse;",
  42. "uniform vec3 specular;",
  43. "uniform float shininess;",
  44. "uniform float opacity;",
  45. "uniform bool enableDiffuse;",
  46. "uniform bool enableSpecular;",
  47. "uniform bool enableAO;",
  48. "uniform bool enableReflection;",
  49. "uniform sampler2D tDiffuse;",
  50. "uniform sampler2D tNormal;",
  51. "uniform sampler2D tSpecular;",
  52. "uniform sampler2D tAO;",
  53. "uniform samplerCube tCube;",
  54. "uniform vec2 uNormalScale;",
  55. "uniform float refractionRatio;",
  56. "uniform float reflectivity;",
  57. "varying vec3 vTangent;",
  58. "varying vec3 vBinormal;",
  59. "varying vec3 vNormal;",
  60. "varying vec2 vUv;",
  61. "uniform vec3 ambientLightColor;",
  62. "#if MAX_DIR_LIGHTS > 0",
  63. " uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  64. " uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  65. "#endif",
  66. "#if MAX_HEMI_LIGHTS > 0",
  67. " uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  68. " uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  69. " uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  70. "#endif",
  71. "#if MAX_POINT_LIGHTS > 0",
  72. " uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  73. " uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  74. " uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  75. "#endif",
  76. "#if MAX_SPOT_LIGHTS > 0",
  77. " uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  78. " uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  79. " uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  80. " uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
  81. " uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  82. " uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  83. "#endif",
  84. "#ifdef WRAP_AROUND",
  85. " uniform vec3 wrapRGB;",
  86. "#endif",
  87. "varying vec3 vWorldPosition;",
  88. "varying vec3 vViewPosition;",
  89. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  90. THREE.ShaderChunk[ "fog_pars_fragment" ],
  91. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  92. "void main() {",
  93. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  94. " gl_FragColor = vec4( vec3( 1.0 ), opacity );",
  95. " vec3 specularTex = vec3( 1.0 );",
  96. " vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  97. " normalTex.xy *= uNormalScale;",
  98. " normalTex = normalize( normalTex );",
  99. " if( enableDiffuse ) {",
  100. " #ifdef GAMMA_INPUT",
  101. " vec4 texelColor = texture2D( tDiffuse, vUv );",
  102. " texelColor.xyz *= texelColor.xyz;",
  103. " gl_FragColor = gl_FragColor * texelColor;",
  104. " #else",
  105. " gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
  106. " #endif",
  107. " }",
  108. " if( enableAO ) {",
  109. " #ifdef GAMMA_INPUT",
  110. " vec4 aoColor = texture2D( tAO, vUv );",
  111. " aoColor.xyz *= aoColor.xyz;",
  112. " gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",
  113. " #else",
  114. " gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",
  115. " #endif",
  116. " }",
  117. THREE.ShaderChunk[ "alphatest_fragment" ],
  118. " if( enableSpecular )",
  119. " specularTex = texture2D( tSpecular, vUv ).xyz;",
  120. " mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",
  121. " vec3 finalNormal = tsb * normalTex;",
  122. " #ifdef FLIP_SIDED",
  123. " finalNormal = -finalNormal;",
  124. " #endif",
  125. " vec3 normal = normalize( finalNormal );",
  126. " vec3 viewPosition = normalize( vViewPosition );",
  127. // point lights
  128. " #if MAX_POINT_LIGHTS > 0",
  129. " vec3 pointDiffuse = vec3( 0.0 );",
  130. " vec3 pointSpecular = vec3( 0.0 );",
  131. " for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  132. " vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  133. " vec3 pointVector = lPosition.xyz + vViewPosition.xyz;",
  134. " float pointDistance = 1.0;",
  135. " if ( pointLightDistance[ i ] > 0.0 )",
  136. " pointDistance = 1.0 - min( ( length( pointVector ) / pointLightDistance[ i ] ), 1.0 );",
  137. " pointVector = normalize( pointVector );",
  138. // diffuse
  139. " #ifdef WRAP_AROUND",
  140. " float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",
  141. " float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",
  142. " vec3 pointDiffuseWeight = mix( vec3( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  143. " #else",
  144. " float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  145. " #endif",
  146. " pointDiffuse += pointDistance * pointLightColor[ i ] * diffuse * pointDiffuseWeight;",
  147. // specular
  148. " vec3 pointHalfVector = normalize( pointVector + viewPosition );",
  149. " float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  150. " float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  151. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  152. " vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( pointVector, pointHalfVector ), 0.0 ), 5.0 );",
  153. " pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",
  154. " }",
  155. " #endif",
  156. // spot lights
  157. " #if MAX_SPOT_LIGHTS > 0",
  158. " vec3 spotDiffuse = vec3( 0.0 );",
  159. " vec3 spotSpecular = vec3( 0.0 );",
  160. " for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  161. " vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  162. " vec3 spotVector = lPosition.xyz + vViewPosition.xyz;",
  163. " float spotDistance = 1.0;",
  164. " if ( spotLightDistance[ i ] > 0.0 )",
  165. " spotDistance = 1.0 - min( ( length( spotVector ) / spotLightDistance[ i ] ), 1.0 );",
  166. " spotVector = normalize( spotVector );",
  167. " float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );",
  168. " if ( spotEffect > spotLightAngleCos[ i ] ) {",
  169. " spotEffect = max( pow( max( spotEffect, 0.0 ), spotLightExponent[ i ] ), 0.0 );",
  170. // diffuse
  171. " #ifdef WRAP_AROUND",
  172. " float spotDiffuseWeightFull = max( dot( normal, spotVector ), 0.0 );",
  173. " float spotDiffuseWeightHalf = max( 0.5 * dot( normal, spotVector ) + 0.5, 0.0 );",
  174. " vec3 spotDiffuseWeight = mix( vec3( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );",
  175. " #else",
  176. " float spotDiffuseWeight = max( dot( normal, spotVector ), 0.0 );",
  177. " #endif",
  178. " spotDiffuse += spotDistance * spotLightColor[ i ] * diffuse * spotDiffuseWeight * spotEffect;",
  179. // specular
  180. " vec3 spotHalfVector = normalize( spotVector + viewPosition );",
  181. " float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
  182. " float spotSpecularWeight = specularTex.r * max( pow( spotDotNormalHalf, shininess ), 0.0 );",
  183. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  184. " vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( spotVector, spotHalfVector ), 0.0 ), 5.0 );",
  185. " spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * spotDistance * specularNormalization * spotEffect;",
  186. " }",
  187. " }",
  188. " #endif",
  189. // directional lights
  190. " #if MAX_DIR_LIGHTS > 0",
  191. " vec3 dirDiffuse = vec3( 0.0 );",
  192. " vec3 dirSpecular = vec3( 0.0 );",
  193. " for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  194. " vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  195. " vec3 dirVector = normalize( lDirection.xyz );",
  196. // diffuse
  197. " #ifdef WRAP_AROUND",
  198. " float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",
  199. " float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
  200. " vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",
  201. " #else",
  202. " float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  203. " #endif",
  204. " dirDiffuse += directionalLightColor[ i ] * diffuse * dirDiffuseWeight;",
  205. // specular
  206. " vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  207. " float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  208. " float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  209. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  210. " vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( dirVector, dirHalfVector ), 0.0 ), 5.0 );",
  211. " dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  212. " }",
  213. " #endif",
  214. // hemisphere lights
  215. " #if MAX_HEMI_LIGHTS > 0",
  216. " vec3 hemiDiffuse = vec3( 0.0 );",
  217. " vec3 hemiSpecular = vec3( 0.0 );" ,
  218. " for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  219. " vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
  220. " vec3 lVector = normalize( lDirection.xyz );",
  221. // diffuse
  222. " float dotProduct = dot( normal, lVector );",
  223. " float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  224. " vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  225. " hemiDiffuse += diffuse * hemiColor;",
  226. // specular (sky light)
  227. " vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  228. " float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  229. " float hemiSpecularWeightSky = specularTex.r * max( pow( max( hemiDotNormalHalfSky, 0.0 ), shininess ), 0.0 );",
  230. // specular (ground light)
  231. " vec3 lVectorGround = -lVector;",
  232. " vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
  233. " float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  234. " float hemiSpecularWeightGround = specularTex.r * max( pow( max( hemiDotNormalHalfGround, 0.0 ), shininess ), 0.0 );",
  235. " float dotProductGround = dot( normal, lVectorGround );",
  236. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  237. " vec3 schlickSky = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( lVector, hemiHalfVectorSky ), 0.0 ), 5.0 );",
  238. " vec3 schlickGround = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 0.0 ), 5.0 );",
  239. " hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
  240. " }",
  241. " #endif",
  242. // all lights contribution summation
  243. " vec3 totalDiffuse = vec3( 0.0 );",
  244. " vec3 totalSpecular = vec3( 0.0 );",
  245. " #if MAX_DIR_LIGHTS > 0",
  246. " totalDiffuse += dirDiffuse;",
  247. " totalSpecular += dirSpecular;",
  248. " #endif",
  249. " #if MAX_HEMI_LIGHTS > 0",
  250. " totalDiffuse += hemiDiffuse;",
  251. " totalSpecular += hemiSpecular;",
  252. " #endif",
  253. " #if MAX_POINT_LIGHTS > 0",
  254. " totalDiffuse += pointDiffuse;",
  255. " totalSpecular += pointSpecular;",
  256. " #endif",
  257. " #if MAX_SPOT_LIGHTS > 0",
  258. " totalDiffuse += spotDiffuse;",
  259. " totalSpecular += spotSpecular;",
  260. " #endif",
  261. " #ifdef METAL",
  262. " gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * diffuse + totalSpecular );",
  263. " #else",
  264. " gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * diffuse ) + totalSpecular;",
  265. " #endif",
  266. " if ( enableReflection ) {",
  267. " vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );",
  268. " #ifdef ENVMAP_MODE_REFLECTION",
  269. " vec3 vReflect = reflect( cameraToVertex, normal );",
  270. " #else",
  271. " vec3 vReflect = refract( cameraToVertex, normal, refractionRatio );",
  272. " #endif",
  273. " vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  274. " #ifdef GAMMA_INPUT",
  275. " cubeColor.xyz *= cubeColor.xyz;",
  276. " #endif",
  277. " gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * reflectivity );",
  278. " }",
  279. THREE.ShaderChunk[ "shadowmap_fragment" ],
  280. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  281. THREE.ShaderChunk[ "fog_fragment" ],
  282. "}"
  283. ].join("\n"),
  284. vertexShader: [
  285. "attribute vec4 tangent;",
  286. "uniform vec2 uOffset;",
  287. "uniform vec2 uRepeat;",
  288. "uniform bool enableDisplacement;",
  289. "#ifdef VERTEX_TEXTURES",
  290. " uniform sampler2D tDisplacement;",
  291. " uniform float uDisplacementScale;",
  292. " uniform float uDisplacementBias;",
  293. "#endif",
  294. "varying vec3 vTangent;",
  295. "varying vec3 vBinormal;",
  296. "varying vec3 vNormal;",
  297. "varying vec2 vUv;",
  298. "varying vec3 vWorldPosition;",
  299. "varying vec3 vViewPosition;",
  300. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  301. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  302. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  303. "void main() {",
  304. THREE.ShaderChunk[ "skinbase_vertex" ],
  305. THREE.ShaderChunk[ "skinnormal_vertex" ],
  306. // normal, tangent and binormal vectors
  307. " #ifdef USE_SKINNING",
  308. " vNormal = normalize( normalMatrix * skinnedNormal.xyz );",
  309. " vec4 skinnedTangent = skinMatrix * vec4( tangent.xyz, 0.0 );",
  310. " vTangent = normalize( normalMatrix * skinnedTangent.xyz );",
  311. " #else",
  312. " vNormal = normalize( normalMatrix * normal );",
  313. " vTangent = normalize( normalMatrix * tangent.xyz );",
  314. " #endif",
  315. " vBinormal = normalize( cross( vNormal, vTangent ) * tangent.w );",
  316. " vUv = uv * uRepeat + uOffset;",
  317. // displacement mapping
  318. " vec3 displacedPosition;",
  319. " #ifdef VERTEX_TEXTURES",
  320. " if ( enableDisplacement ) {",
  321. " vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  322. " float df = uDisplacementScale * dv.x + uDisplacementBias;",
  323. " displacedPosition = position + normalize( normal ) * df;",
  324. " } else {",
  325. " #ifdef USE_SKINNING",
  326. " vec4 skinVertex = bindMatrix * vec4( position, 1.0 );",
  327. " vec4 skinned = vec4( 0.0 );",
  328. " skinned += boneMatX * skinVertex * skinWeight.x;",
  329. " skinned += boneMatY * skinVertex * skinWeight.y;",
  330. " skinned += boneMatZ * skinVertex * skinWeight.z;",
  331. " skinned += boneMatW * skinVertex * skinWeight.w;",
  332. " skinned = bindMatrixInverse * skinned;",
  333. " displacedPosition = skinned.xyz;",
  334. " #else",
  335. " displacedPosition = position;",
  336. " #endif",
  337. " }",
  338. " #else",
  339. " #ifdef USE_SKINNING",
  340. " vec4 skinVertex = bindMatrix * vec4( position, 1.0 );",
  341. " vec4 skinned = vec4( 0.0 );",
  342. " skinned += boneMatX * skinVertex * skinWeight.x;",
  343. " skinned += boneMatY * skinVertex * skinWeight.y;",
  344. " skinned += boneMatZ * skinVertex * skinWeight.z;",
  345. " skinned += boneMatW * skinVertex * skinWeight.w;",
  346. " skinned = bindMatrixInverse * skinned;",
  347. " displacedPosition = skinned.xyz;",
  348. " #else",
  349. " displacedPosition = position;",
  350. " #endif",
  351. " #endif",
  352. //
  353. " vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
  354. " vec4 worldPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
  355. " gl_Position = projectionMatrix * mvPosition;",
  356. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  357. //
  358. " vWorldPosition = worldPosition.xyz;",
  359. " vViewPosition = -mvPosition.xyz;",
  360. // shadows
  361. " #ifdef USE_SHADOWMAP",
  362. " for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  363. " vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;",
  364. " }",
  365. " #endif",
  366. "}"
  367. ].join("\n")
  368. };