ShaderUtils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. "tDiffuse" : { type: "t", value: 0, texture: null },
  81. "tCube" : { type: "t", value: 1, texture: null },
  82. "tNormal" : { type: "t", value: 2, texture: null },
  83. "tSpecular" : { type: "t", value: 3, texture: null },
  84. "tAO" : { type: "t", value: 4, texture: null },
  85. "tDisplacement": { type: "t", value: 5, texture: null },
  86. "uNormalScale": { type: "f", value: 1.0 },
  87. "uDisplacementBias": { type: "f", value: 0.0 },
  88. "uDisplacementScale": { type: "f", value: 1.0 },
  89. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  90. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  91. "uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
  92. "uShininess": { type: "f", value: 30 },
  93. "uOpacity": { type: "f", value: 1 },
  94. "uReflectivity": { type: "f", value: 0.5 },
  95. "uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },
  96. "uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  97. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  98. }
  99. ] ),
  100. fragmentShader: [
  101. "uniform vec3 uAmbientColor;",
  102. "uniform vec3 uDiffuseColor;",
  103. "uniform vec3 uSpecularColor;",
  104. "uniform float uShininess;",
  105. "uniform float uOpacity;",
  106. "uniform bool enableDiffuse;",
  107. "uniform bool enableSpecular;",
  108. "uniform bool enableAO;",
  109. "uniform bool enableReflection;",
  110. "uniform sampler2D tDiffuse;",
  111. "uniform sampler2D tNormal;",
  112. "uniform sampler2D tSpecular;",
  113. "uniform sampler2D tAO;",
  114. "uniform samplerCube tCube;",
  115. "uniform float uNormalScale;",
  116. "uniform float uReflectivity;",
  117. "varying vec3 vTangent;",
  118. "varying vec3 vBinormal;",
  119. "varying vec3 vNormal;",
  120. "varying vec2 vUv;",
  121. "uniform vec3 ambientLightColor;",
  122. "#if MAX_DIR_LIGHTS > 0",
  123. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  124. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  125. "#endif",
  126. "#if MAX_POINT_LIGHTS > 0",
  127. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  128. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  129. "#endif",
  130. "#ifdef WRAP_AROUND",
  131. "uniform vec3 wrapRGB;",
  132. "#endif",
  133. "varying vec3 vViewPosition;",
  134. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  135. THREE.ShaderChunk[ "fog_pars_fragment" ],
  136. "void main() {",
  137. "gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
  138. "vec3 specularTex = vec3( 1.0 );",
  139. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  140. "normalTex.xy *= uNormalScale;",
  141. "normalTex = normalize( normalTex );",
  142. "if( enableDiffuse ) {",
  143. "#ifdef GAMMA_INPUT",
  144. "vec4 texelColor = texture2D( tDiffuse, vUv );",
  145. "texelColor.xyz *= texelColor.xyz;",
  146. "gl_FragColor = gl_FragColor * texelColor;",
  147. "#else",
  148. "gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
  149. "#endif",
  150. "}",
  151. "if( enableAO ) {",
  152. "#ifdef GAMMA_INPUT",
  153. "vec4 aoColor = texture2D( tAO, vUv );",
  154. "aoColor.xyz *= aoColor.xyz;",
  155. "gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",
  156. "#else",
  157. "gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",
  158. "#endif",
  159. "}",
  160. "if( enableSpecular )",
  161. "specularTex = texture2D( tSpecular, vUv ).xyz;",
  162. "mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",
  163. "vec3 finalNormal = tsb * normalTex;",
  164. "vec3 normal = normalize( finalNormal );",
  165. "vec3 viewPosition = normalize( vViewPosition );",
  166. // point lights
  167. "#if MAX_POINT_LIGHTS > 0",
  168. "vec3 pointDiffuse = vec3( 0.0 );",
  169. "vec3 pointSpecular = vec3( 0.0 );",
  170. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  171. "vec3 pointVector = normalize( vPointLight[ i ].xyz );",
  172. "float pointDistance = vPointLight[ i ].w;",
  173. // diffuse
  174. "#ifdef WRAP_AROUND",
  175. "float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",
  176. "float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",
  177. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  178. "#else",
  179. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  180. "#endif",
  181. "pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",
  182. // specular
  183. "vec3 pointHalfVector = normalize( pointVector + viewPosition );",
  184. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  185. "float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, uShininess ), 0.0 );",
  186. "pointSpecular += pointDistance * pointLightColor[ i ] * uSpecularColor * pointSpecularWeight * pointDiffuseWeight;",
  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 ] * uDiffuseColor * 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, uShininess ), 0.0 );",
  209. "dirSpecular += directionalLightColor[ i ] * uSpecularColor * dirSpecularWeight * dirDiffuseWeight;",
  210. "}",
  211. "#endif",
  212. // all lights contribution summation
  213. "vec3 totalDiffuse = vec3( 0.0 );",
  214. "vec3 totalSpecular = vec3( 0.0 );",
  215. "#if MAX_DIR_LIGHTS > 0",
  216. "totalDiffuse += dirDiffuse;",
  217. "totalSpecular += dirSpecular;",
  218. "#endif",
  219. "#if MAX_POINT_LIGHTS > 0",
  220. "totalDiffuse += pointDiffuse;",
  221. "totalSpecular += pointSpecular;",
  222. "#endif",
  223. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor) + totalSpecular;",
  224. "if ( enableReflection ) {",
  225. "vec3 wPos = cameraPosition - vViewPosition;",
  226. "vec3 vReflect = reflect( normalize( wPos ), normal );",
  227. "vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  228. "#ifdef GAMMA_INPUT",
  229. "cubeColor.xyz *= cubeColor.xyz;",
  230. "#endif",
  231. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",
  232. "}",
  233. THREE.ShaderChunk[ "shadowmap_fragment" ],
  234. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  235. THREE.ShaderChunk[ "fog_fragment" ],
  236. "}"
  237. ].join("\n"),
  238. vertexShader: [
  239. "attribute vec4 tangent;",
  240. "uniform vec2 uOffset;",
  241. "uniform vec2 uRepeat;",
  242. "#ifdef VERTEX_TEXTURES",
  243. "uniform sampler2D tDisplacement;",
  244. "uniform float uDisplacementScale;",
  245. "uniform float uDisplacementBias;",
  246. "#endif",
  247. "varying vec3 vTangent;",
  248. "varying vec3 vBinormal;",
  249. "varying vec3 vNormal;",
  250. "varying vec2 vUv;",
  251. "#if MAX_POINT_LIGHTS > 0",
  252. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  253. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  254. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  255. "#endif",
  256. "varying vec3 vViewPosition;",
  257. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  258. "void main() {",
  259. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  260. "vViewPosition = -mvPosition.xyz;",
  261. // normal, tangent and binormal vectors
  262. "vNormal = normalMatrix * normal;",
  263. "vTangent = normalMatrix * tangent.xyz;",
  264. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  265. "vUv = uv * uRepeat + uOffset;",
  266. // point lights
  267. "#if MAX_POINT_LIGHTS > 0",
  268. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  269. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  270. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  271. "float lDistance = 1.0;",
  272. "if ( pointLightDistance[ i ] > 0.0 )",
  273. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  274. "lVector = normalize( lVector );",
  275. "vPointLight[ i ] = vec4( lVector, lDistance );",
  276. "}",
  277. "#endif",
  278. // displacement mapping
  279. "#ifdef VERTEX_TEXTURES",
  280. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  281. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  282. "vec4 displacedPosition = vec4( normalize( vNormal.xyz ) * df, 0.0 ) + mvPosition;",
  283. "gl_Position = projectionMatrix * displacedPosition;",
  284. "#else",
  285. "gl_Position = projectionMatrix * mvPosition;",
  286. "#endif",
  287. THREE.ShaderChunk[ "shadowmap_vertex" ],
  288. "}"
  289. ].join("\n")
  290. },
  291. /* -------------------------------------------------------------------------
  292. // Cube map shader
  293. ------------------------------------------------------------------------- */
  294. 'cube': {
  295. uniforms: { "tCube": { type: "t", value: 1, texture: null },
  296. "tFlip": { type: "f", value: -1 } },
  297. vertexShader: [
  298. "varying vec3 vViewPosition;",
  299. "void main() {",
  300. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  301. "vViewPosition = cameraPosition - mPosition.xyz;",
  302. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  303. "}"
  304. ].join("\n"),
  305. fragmentShader: [
  306. "uniform samplerCube tCube;",
  307. "uniform float tFlip;",
  308. "varying vec3 vViewPosition;",
  309. "void main() {",
  310. "vec3 wPos = cameraPosition - vViewPosition;",
  311. "gl_FragColor = textureCube( tCube, vec3( tFlip * wPos.x, wPos.yz ) );",
  312. "}"
  313. ].join("\n")
  314. }
  315. }
  316. };
  317. };