ShaderUtils.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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( 0xffffff ) },
  90. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  91. "uAmbientColor": { type: "c", value: new THREE.Color( 0xffffff ) },
  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. "#ifdef PHYSICALLY_BASED_SHADING",
  187. // 2.0 => 2.0001 is hack to work around ANGLE bug
  188. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  189. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( pointVector, pointHalfVector ), 5.0 );",
  190. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",
  191. "#else",
  192. "pointSpecular += pointDistance * pointLightColor[ i ] * uSpecularColor * pointSpecularWeight * pointDiffuseWeight;",
  193. "#endif",
  194. "}",
  195. "#endif",
  196. // directional lights
  197. "#if MAX_DIR_LIGHTS > 0",
  198. "vec3 dirDiffuse = vec3( 0.0 );",
  199. "vec3 dirSpecular = vec3( 0.0 );",
  200. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  201. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  202. "vec3 dirVector = normalize( lDirection.xyz );",
  203. // diffuse
  204. "#ifdef WRAP_AROUND",
  205. "float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",
  206. "float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
  207. "vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",
  208. "#else",
  209. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  210. "#endif",
  211. "dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",
  212. // specular
  213. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  214. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  215. "float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, uShininess ), 0.0 );",
  216. "#ifdef PHYSICALLY_BASED_SHADING",
  217. // 2.0 => 2.0001 is hack to work around ANGLE bug
  218. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  219. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
  220. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  221. "#else",
  222. "dirSpecular += directionalLightColor[ i ] * uSpecularColor * dirSpecularWeight * dirDiffuseWeight;",
  223. "#endif",
  224. "}",
  225. "#endif",
  226. // all lights contribution summation
  227. "vec3 totalDiffuse = vec3( 0.0 );",
  228. "vec3 totalSpecular = vec3( 0.0 );",
  229. "#if MAX_DIR_LIGHTS > 0",
  230. "totalDiffuse += dirDiffuse;",
  231. "totalSpecular += dirSpecular;",
  232. "#endif",
  233. "#if MAX_POINT_LIGHTS > 0",
  234. "totalDiffuse += pointDiffuse;",
  235. "totalSpecular += pointSpecular;",
  236. "#endif",
  237. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor) + totalSpecular;",
  238. "if ( enableReflection ) {",
  239. "vec3 wPos = cameraPosition - vViewPosition;",
  240. "vec3 vReflect = reflect( normalize( wPos ), normal );",
  241. "vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  242. "#ifdef GAMMA_INPUT",
  243. "cubeColor.xyz *= cubeColor.xyz;",
  244. "#endif",
  245. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",
  246. "}",
  247. THREE.ShaderChunk[ "shadowmap_fragment" ],
  248. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  249. THREE.ShaderChunk[ "fog_fragment" ],
  250. "}"
  251. ].join("\n"),
  252. vertexShader: [
  253. "attribute vec4 tangent;",
  254. "uniform vec2 uOffset;",
  255. "uniform vec2 uRepeat;",
  256. "#ifdef VERTEX_TEXTURES",
  257. "uniform sampler2D tDisplacement;",
  258. "uniform float uDisplacementScale;",
  259. "uniform float uDisplacementBias;",
  260. "#endif",
  261. "varying vec3 vTangent;",
  262. "varying vec3 vBinormal;",
  263. "varying vec3 vNormal;",
  264. "varying vec2 vUv;",
  265. "#if MAX_POINT_LIGHTS > 0",
  266. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  267. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  268. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  269. "#endif",
  270. "varying vec3 vViewPosition;",
  271. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  272. "void main() {",
  273. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  274. "vViewPosition = -mvPosition.xyz;",
  275. // normal, tangent and binormal vectors
  276. "vNormal = normalMatrix * normal;",
  277. "vTangent = normalMatrix * tangent.xyz;",
  278. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  279. "vUv = uv * uRepeat + uOffset;",
  280. // point lights
  281. "#if MAX_POINT_LIGHTS > 0",
  282. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  283. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  284. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  285. "float lDistance = 1.0;",
  286. "if ( pointLightDistance[ i ] > 0.0 )",
  287. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  288. "lVector = normalize( lVector );",
  289. "vPointLight[ i ] = vec4( lVector, lDistance );",
  290. "}",
  291. "#endif",
  292. // displacement mapping
  293. "#ifdef VERTEX_TEXTURES",
  294. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  295. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  296. "vec4 displacedPosition = vec4( normalize( vNormal.xyz ) * df, 0.0 ) + mvPosition;",
  297. "gl_Position = projectionMatrix * displacedPosition;",
  298. "#else",
  299. "gl_Position = projectionMatrix * mvPosition;",
  300. "#endif",
  301. THREE.ShaderChunk[ "shadowmap_vertex" ],
  302. "}"
  303. ].join("\n")
  304. },
  305. /* -------------------------------------------------------------------------
  306. // Cube map shader
  307. ------------------------------------------------------------------------- */
  308. 'cube': {
  309. uniforms: { "tCube": { type: "t", value: 1, texture: null },
  310. "tFlip": { type: "f", value: -1 } },
  311. vertexShader: [
  312. "varying vec3 vViewPosition;",
  313. "void main() {",
  314. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  315. "vViewPosition = cameraPosition - mPosition.xyz;",
  316. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  317. "}"
  318. ].join("\n"),
  319. fragmentShader: [
  320. "uniform samplerCube tCube;",
  321. "uniform float tFlip;",
  322. "varying vec3 vViewPosition;",
  323. "void main() {",
  324. "vec3 wPos = cameraPosition - vViewPosition;",
  325. "gl_FragColor = textureCube( tCube, vec3( tFlip * wPos.x, wPos.yz ) );",
  326. "}"
  327. ].join("\n")
  328. }
  329. }
  330. };
  331. };
粤ICP备19079148号