ShaderUtils.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mr.doob / http://mrdoob.com/
  4. *
  5. * ShaderUtils currently contains:
  6. *
  7. * fresnel
  8. * normal
  9. * cube
  10. *
  11. */
  12. if ( THREE.WebGLRenderer ) {
  13. THREE.ShaderUtils = {
  14. lib: {
  15. /* -------------------------------------------------------------------------
  16. // Fresnel shader
  17. // - based on Nvidia Cg tutorial
  18. ------------------------------------------------------------------------- */
  19. 'fresnel': {
  20. uniforms: {
  21. "mRefractionRatio": { type: "f", value: 1.02 },
  22. "mFresnelBias": { type: "f", value: 0.1 },
  23. "mFresnelPower": { type: "f", value: 2.0 },
  24. "mFresnelScale": { type: "f", value: 1.0 },
  25. "tCube": { type: "t", value: 1, texture: null }
  26. },
  27. fragmentShader: [
  28. "uniform samplerCube tCube;",
  29. "varying vec3 vReflect;",
  30. "varying vec3 vRefract[3];",
  31. "varying float vReflectionFactor;",
  32. "void main() {",
  33. "vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  34. "vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
  35. "refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;",
  36. "refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;",
  37. "refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;",
  38. "refractedColor.a = 1.0;",
  39. "gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
  40. "}"
  41. ].join("\n"),
  42. vertexShader: [
  43. "uniform float mRefractionRatio;",
  44. "uniform float mFresnelBias;",
  45. "uniform float mFresnelScale;",
  46. "uniform float mFresnelPower;",
  47. "varying vec3 vReflect;",
  48. "varying vec3 vRefract[3];",
  49. "varying float vReflectionFactor;",
  50. "void main() {",
  51. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  52. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  53. "vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );",
  54. "vec3 I = mPosition.xyz - cameraPosition;",
  55. "vReflect = reflect( I, nWorld );",
  56. "vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );",
  57. "vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );",
  58. "vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );",
  59. "vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );",
  60. "gl_Position = projectionMatrix * mvPosition;",
  61. "}"
  62. ].join("\n")
  63. },
  64. /* -------------------------------------------------------------------------
  65. // Normal map shader
  66. // - Blinn-Phong
  67. // - normal + diffuse + specular + AO + displacement + reflection + shadow maps
  68. // - point and directional lights (use with "lights: true" material option)
  69. ------------------------------------------------------------------------- */
  70. 'normal' : {
  71. uniforms: THREE.UniformsUtils.merge( [
  72. THREE.UniformsLib[ "fog" ],
  73. THREE.UniformsLib[ "lights" ],
  74. THREE.UniformsLib[ "shadowmap" ],
  75. {
  76. "enableAO" : { type: "i", value: 0 },
  77. "enableDiffuse" : { type: "i", value: 0 },
  78. "enableSpecular" : { type: "i", value: 0 },
  79. "enableReflection": { type: "i", value: 0 },
  80. "enableDisplacement": { type: "i", value: 0 },
  81. "tDiffuse" : { type: "t", value: 0, texture: null },
  82. "tCube" : { type: "t", value: 1, texture: null },
  83. "tNormal" : { type: "t", value: 2, texture: null },
  84. "tSpecular" : { type: "t", value: 3, texture: null },
  85. "tAO" : { type: "t", value: 4, texture: null },
  86. "tDisplacement": { type: "t", value: 5, texture: null },
  87. "uNormalScale": { type: "f", value: 1.0 },
  88. "uDisplacementBias": { type: "f", value: 0.0 },
  89. "uDisplacementScale": { type: "f", value: 1.0 },
  90. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xffffff ) },
  91. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  92. "uAmbientColor": { type: "c", value: new THREE.Color( 0xffffff ) },
  93. "uShininess": { type: "f", value: 30 },
  94. "uOpacity": { type: "f", value: 1 },
  95. "uReflectivity": { type: "f", value: 0.5 },
  96. "uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },
  97. "uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  98. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  99. }
  100. ] ),
  101. fragmentShader: [
  102. "uniform vec3 uAmbientColor;",
  103. "uniform vec3 uDiffuseColor;",
  104. "uniform vec3 uSpecularColor;",
  105. "uniform float uShininess;",
  106. "uniform float uOpacity;",
  107. "uniform bool enableDiffuse;",
  108. "uniform bool enableSpecular;",
  109. "uniform bool enableAO;",
  110. "uniform bool enableReflection;",
  111. "uniform sampler2D tDiffuse;",
  112. "uniform sampler2D tNormal;",
  113. "uniform sampler2D tSpecular;",
  114. "uniform sampler2D tAO;",
  115. "uniform samplerCube tCube;",
  116. "uniform float uNormalScale;",
  117. "uniform float uReflectivity;",
  118. "varying vec3 vTangent;",
  119. "varying vec3 vBinormal;",
  120. "varying vec3 vNormal;",
  121. "varying vec2 vUv;",
  122. "uniform vec3 ambientLightColor;",
  123. "#if MAX_DIR_LIGHTS > 0",
  124. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  125. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  126. "#endif",
  127. "#if MAX_POINT_LIGHTS > 0",
  128. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  129. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  130. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  131. "#endif",
  132. "#if MAX_SPOT_LIGHTS > 0",
  133. "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  134. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  135. "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  136. "uniform float spotLightAngle[ MAX_SPOT_LIGHTS ];",
  137. "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  138. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  139. "#endif",
  140. "#ifdef WRAP_AROUND",
  141. "uniform vec3 wrapRGB;",
  142. "#endif",
  143. "varying vec3 vWorldPosition;",
  144. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  145. THREE.ShaderChunk[ "fog_pars_fragment" ],
  146. "void main() {",
  147. "vec3 vViewPosition = cameraPosition - vWorldPosition;",
  148. "gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
  149. "vec3 specularTex = vec3( 1.0 );",
  150. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  151. "normalTex.xy *= uNormalScale;",
  152. "normalTex = normalize( normalTex );",
  153. "if( enableDiffuse ) {",
  154. "#ifdef GAMMA_INPUT",
  155. "vec4 texelColor = texture2D( tDiffuse, vUv );",
  156. "texelColor.xyz *= texelColor.xyz;",
  157. "gl_FragColor = gl_FragColor * texelColor;",
  158. "#else",
  159. "gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
  160. "#endif",
  161. "}",
  162. "if( enableAO ) {",
  163. "#ifdef GAMMA_INPUT",
  164. "vec4 aoColor = texture2D( tAO, vUv );",
  165. "aoColor.xyz *= aoColor.xyz;",
  166. "gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",
  167. "#else",
  168. "gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",
  169. "#endif",
  170. "}",
  171. "if( enableSpecular )",
  172. "specularTex = texture2D( tSpecular, vUv ).xyz;",
  173. "mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",
  174. "vec3 finalNormal = tsb * normalTex;",
  175. "vec3 normal = normalize( finalNormal );",
  176. "vec3 viewPosition = normalize( vViewPosition );",
  177. // point lights
  178. "#if MAX_POINT_LIGHTS > 0",
  179. "vec3 pointDiffuse = vec3( 0.0 );",
  180. "vec3 pointSpecular = vec3( 0.0 );",
  181. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  182. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  183. "vec3 pointVector = lPosition.xyz + vViewPosition.xyz;",
  184. "float pointDistance = 1.0;",
  185. "if ( pointLightDistance[ i ] > 0.0 )",
  186. "pointDistance = 1.0 - min( ( length( pointVector ) / pointLightDistance[ i ] ), 1.0 );",
  187. "pointVector = normalize( pointVector );",
  188. // diffuse
  189. "#ifdef WRAP_AROUND",
  190. "float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",
  191. "float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",
  192. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  193. "#else",
  194. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  195. "#endif",
  196. "pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",
  197. // specular
  198. "vec3 pointHalfVector = normalize( pointVector + viewPosition );",
  199. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  200. "float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, uShininess ), 0.0 );",
  201. "#ifdef PHYSICALLY_BASED_SHADING",
  202. // 2.0 => 2.0001 is hack to work around ANGLE bug
  203. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  204. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( pointVector, pointHalfVector ), 5.0 );",
  205. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",
  206. "#else",
  207. "pointSpecular += pointDistance * pointLightColor[ i ] * uSpecularColor * pointSpecularWeight * pointDiffuseWeight;",
  208. "#endif",
  209. "}",
  210. "#endif",
  211. // spot lights
  212. "#if MAX_SPOT_LIGHTS > 0",
  213. "vec3 spotDiffuse = vec3( 0.0 );",
  214. "vec3 spotSpecular = vec3( 0.0 );",
  215. "for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  216. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  217. "vec3 spotVector = lPosition.xyz + vViewPosition.xyz;",
  218. "float spotDistance = 1.0;",
  219. "if ( spotLightDistance[ i ] > 0.0 )",
  220. "spotDistance = 1.0 - min( ( length( spotVector ) / spotLightDistance[ i ] ), 1.0 );",
  221. "spotVector = normalize( spotVector );",
  222. "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );",
  223. "if ( spotEffect > spotLightAngle[ i ] ) {",
  224. "spotEffect = pow( spotEffect, spotLightExponent[ i ] );",
  225. // diffuse
  226. "#ifdef WRAP_AROUND",
  227. "float spotDiffuseWeightFull = max( dot( normal, spotVector ), 0.0 );",
  228. "float spotDiffuseWeightHalf = max( 0.5 * dot( normal, spotVector ) + 0.5, 0.0 );",
  229. "vec3 spotDiffuseWeight = mix( vec3 ( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );",
  230. "#else",
  231. "float spotDiffuseWeight = max( dot( normal, spotVector ), 0.0 );",
  232. "#endif",
  233. "spotDiffuse += spotDistance * spotLightColor[ i ] * uDiffuseColor * spotDiffuseWeight * spotEffect;",
  234. // specular
  235. "vec3 spotHalfVector = normalize( spotVector + viewPosition );",
  236. "float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
  237. "float spotSpecularWeight = specularTex.r * max( pow( spotDotNormalHalf, uShininess ), 0.0 );",
  238. "#ifdef PHYSICALLY_BASED_SHADING",
  239. // 2.0 => 2.0001 is hack to work around ANGLE bug
  240. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  241. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( spotVector, spotHalfVector ), 5.0 );",
  242. "spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * spotDistance * specularNormalization * spotEffect;",
  243. "#else",
  244. "spotSpecular += spotDistance * spotLightColor[ i ] * uSpecularColor * spotSpecularWeight * spotDiffuseWeight * spotEffect;",
  245. "#endif",
  246. "}",
  247. "}",
  248. "#endif",
  249. // directional lights
  250. "#if MAX_DIR_LIGHTS > 0",
  251. "vec3 dirDiffuse = vec3( 0.0 );",
  252. "vec3 dirSpecular = vec3( 0.0 );",
  253. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  254. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  255. "vec3 dirVector = normalize( lDirection.xyz );",
  256. // diffuse
  257. "#ifdef WRAP_AROUND",
  258. "float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",
  259. "float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
  260. "vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",
  261. "#else",
  262. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  263. "#endif",
  264. "dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",
  265. // specular
  266. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  267. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  268. "float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, uShininess ), 0.0 );",
  269. "#ifdef PHYSICALLY_BASED_SHADING",
  270. // 2.0 => 2.0001 is hack to work around ANGLE bug
  271. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  272. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
  273. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  274. "#else",
  275. "dirSpecular += directionalLightColor[ i ] * uSpecularColor * dirSpecularWeight * dirDiffuseWeight;",
  276. "#endif",
  277. "}",
  278. "#endif",
  279. // all lights contribution summation
  280. "vec3 totalDiffuse = vec3( 0.0 );",
  281. "vec3 totalSpecular = vec3( 0.0 );",
  282. "#if MAX_DIR_LIGHTS > 0",
  283. "totalDiffuse += dirDiffuse;",
  284. "totalSpecular += dirSpecular;",
  285. "#endif",
  286. "#if MAX_POINT_LIGHTS > 0",
  287. "totalDiffuse += pointDiffuse;",
  288. "totalSpecular += pointSpecular;",
  289. "#endif",
  290. "#if MAX_SPOT_LIGHTS > 0",
  291. "totalDiffuse += spotDiffuse;",
  292. "totalSpecular += spotSpecular;",
  293. "#endif",
  294. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor) + totalSpecular;",
  295. "if ( enableReflection ) {",
  296. "vec3 vReflect = reflect( normalize( vWorldPosition ), normal );",
  297. "vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  298. "#ifdef GAMMA_INPUT",
  299. "cubeColor.xyz *= cubeColor.xyz;",
  300. "#endif",
  301. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",
  302. "}",
  303. THREE.ShaderChunk[ "shadowmap_fragment" ],
  304. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  305. THREE.ShaderChunk[ "fog_fragment" ],
  306. "}"
  307. ].join("\n"),
  308. vertexShader: [
  309. "attribute vec4 tangent;",
  310. "uniform vec2 uOffset;",
  311. "uniform vec2 uRepeat;",
  312. "uniform bool enableDisplacement;",
  313. "#ifdef VERTEX_TEXTURES",
  314. "uniform sampler2D tDisplacement;",
  315. "uniform float uDisplacementScale;",
  316. "uniform float uDisplacementBias;",
  317. "#endif",
  318. "varying vec3 vTangent;",
  319. "varying vec3 vBinormal;",
  320. "varying vec3 vNormal;",
  321. "varying vec2 vUv;",
  322. "varying vec3 vWorldPosition;",
  323. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  324. "void main() {",
  325. // normal, tangent and binormal vectors
  326. "vNormal = normalMatrix * normal;",
  327. "vTangent = normalMatrix * tangent.xyz;",
  328. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  329. "vUv = uv * uRepeat + uOffset;",
  330. // displacement mapping
  331. "vec3 displacedPosition;",
  332. "#ifdef VERTEX_TEXTURES",
  333. "if ( enableDisplacement ) {",
  334. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  335. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  336. "displacedPosition = position + normalize( normal ) * df;",
  337. "} else {",
  338. "displacedPosition = position;",
  339. "}",
  340. "#else",
  341. "displacedPosition = position;",
  342. "#endif",
  343. //
  344. "vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
  345. "vec4 wPosition = objectMatrix * vec4( displacedPosition, 1.0 );",
  346. "gl_Position = projectionMatrix * mvPosition;",
  347. //
  348. "vWorldPosition = wPosition.xyz;",
  349. // shadows
  350. "#ifdef USE_SHADOWMAP",
  351. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  352. "vShadowCoord[ i ] = shadowMatrix[ i ] * wPosition;",
  353. "}",
  354. "#endif",
  355. "}"
  356. ].join("\n")
  357. },
  358. /* -------------------------------------------------------------------------
  359. // Cube map shader
  360. ------------------------------------------------------------------------- */
  361. 'cube': {
  362. uniforms: { "tCube": { type: "t", value: 1, texture: null },
  363. "tFlip": { type: "f", value: -1 } },
  364. vertexShader: [
  365. "varying vec3 vViewPosition;",
  366. "void main() {",
  367. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  368. "vViewPosition = cameraPosition - mPosition.xyz;",
  369. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  370. "}"
  371. ].join("\n"),
  372. fragmentShader: [
  373. "uniform samplerCube tCube;",
  374. "uniform float tFlip;",
  375. "varying vec3 vViewPosition;",
  376. "void main() {",
  377. "vec3 wPos = cameraPosition - vViewPosition;",
  378. "gl_FragColor = textureCube( tCube, vec3( tFlip * wPos.x, wPos.yz ) );",
  379. "}"
  380. ].join("\n")
  381. }
  382. }
  383. };
  384. };