ShaderUtils.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. }
  98. ] ),
  99. fragmentShader: [
  100. "uniform vec3 uAmbientColor;",
  101. "uniform vec3 uDiffuseColor;",
  102. "uniform vec3 uSpecularColor;",
  103. "uniform float uShininess;",
  104. "uniform float uOpacity;",
  105. "uniform bool enableDiffuse;",
  106. "uniform bool enableSpecular;",
  107. "uniform bool enableAO;",
  108. "uniform bool enableReflection;",
  109. "uniform sampler2D tDiffuse;",
  110. "uniform sampler2D tNormal;",
  111. "uniform sampler2D tSpecular;",
  112. "uniform sampler2D tAO;",
  113. "uniform samplerCube tCube;",
  114. "uniform float uNormalScale;",
  115. "uniform float uReflectivity;",
  116. "varying vec3 vTangent;",
  117. "varying vec3 vBinormal;",
  118. "varying vec3 vNormal;",
  119. "varying vec2 vUv;",
  120. "uniform vec3 ambientLightColor;",
  121. "#if MAX_DIR_LIGHTS > 0",
  122. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  123. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  124. "#endif",
  125. "#if MAX_POINT_LIGHTS > 0",
  126. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  127. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  128. "#endif",
  129. "varying vec3 vViewPosition;",
  130. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  131. THREE.ShaderChunk[ "fog_pars_fragment" ],
  132. "void main() {",
  133. "gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
  134. "vec3 specularTex = vec3( 1.0 );",
  135. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  136. "normalTex.xy *= uNormalScale;",
  137. "normalTex = normalize( normalTex );",
  138. "if( enableDiffuse )",
  139. "gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
  140. "if( enableAO )",
  141. "gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",
  142. "if( enableSpecular )",
  143. "specularTex = texture2D( tSpecular, vUv ).xyz;",
  144. "mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
  145. "vec3 finalNormal = tsb * normalTex;",
  146. "vec3 normal = normalize( finalNormal );",
  147. "vec3 viewPosition = normalize( vViewPosition );",
  148. // point lights
  149. "#if MAX_POINT_LIGHTS > 0",
  150. "vec3 pointDiffuse = vec3( 0.0 );",
  151. "vec3 pointSpecular = vec3( 0.0 );",
  152. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  153. "vec3 pointVector = normalize( vPointLight[ i ].xyz );",
  154. "vec3 pointHalfVector = normalize( vPointLight[ i ].xyz + viewPosition );",
  155. "float pointDistance = vPointLight[ i ].w;",
  156. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  157. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  158. "float pointSpecularWeight = specularTex.r * pow( pointDotNormalHalf, uShininess );",
  159. "pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",
  160. "pointSpecular += pointDistance * pointLightColor[ i ] * uSpecularColor * pointSpecularWeight * pointDiffuseWeight;",
  161. "}",
  162. "#endif",
  163. // directional lights
  164. "#if MAX_DIR_LIGHTS > 0",
  165. "vec3 dirDiffuse = vec3( 0.0 );",
  166. "vec3 dirSpecular = vec3( 0.0 );",
  167. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  168. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  169. "vec3 dirVector = normalize( lDirection.xyz );",
  170. "vec3 dirHalfVector = normalize( lDirection.xyz + viewPosition );",
  171. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  172. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  173. "float dirSpecularWeight = specularTex.r * pow( dirDotNormalHalf, uShininess );",
  174. "dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",
  175. "dirSpecular += directionalLightColor[ i ] * uSpecularColor * dirSpecularWeight * dirDiffuseWeight;",
  176. "}",
  177. "#endif",
  178. // all lights contribution summation
  179. "vec3 totalDiffuse = vec3( 0.0 );",
  180. "vec3 totalSpecular = vec3( 0.0 );",
  181. "#if MAX_DIR_LIGHTS > 0",
  182. "totalDiffuse += dirDiffuse;",
  183. "totalSpecular += dirSpecular;",
  184. "#endif",
  185. "#if MAX_POINT_LIGHTS > 0",
  186. "totalDiffuse += pointDiffuse;",
  187. "totalSpecular += pointSpecular;",
  188. "#endif",
  189. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor) + totalSpecular;",
  190. "if ( enableReflection ) {",
  191. "vec3 wPos = cameraPosition - vViewPosition;",
  192. "vec3 vReflect = reflect( normalize( wPos ), normal );",
  193. "vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  194. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, uReflectivity );",
  195. "}",
  196. THREE.ShaderChunk[ "shadowmap_fragment" ],
  197. THREE.ShaderChunk[ "fog_fragment" ],
  198. "}"
  199. ].join("\n"),
  200. vertexShader: [
  201. "attribute vec4 tangent;",
  202. "uniform vec2 uOffset;",
  203. "uniform vec2 uRepeat;",
  204. "#ifdef VERTEX_TEXTURES",
  205. "uniform sampler2D tDisplacement;",
  206. "uniform float uDisplacementScale;",
  207. "uniform float uDisplacementBias;",
  208. "#endif",
  209. "varying vec3 vTangent;",
  210. "varying vec3 vBinormal;",
  211. "varying vec3 vNormal;",
  212. "varying vec2 vUv;",
  213. "#if MAX_POINT_LIGHTS > 0",
  214. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  215. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  216. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  217. "#endif",
  218. "varying vec3 vViewPosition;",
  219. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  220. "void main() {",
  221. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  222. "vViewPosition = -mvPosition.xyz;",
  223. "vNormal = normalize( normalMatrix * normal );",
  224. // tangent and binormal vectors
  225. "vTangent = normalize( normalMatrix * tangent.xyz );",
  226. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  227. "vBinormal = normalize( vBinormal );",
  228. "vUv = uv * uRepeat + uOffset;",
  229. // point lights
  230. "#if MAX_POINT_LIGHTS > 0",
  231. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  232. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  233. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  234. "float lDistance = 1.0;",
  235. "if ( pointLightDistance[ i ] > 0.0 )",
  236. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  237. "lVector = normalize( lVector );",
  238. "vPointLight[ i ] = vec4( lVector, lDistance );",
  239. "}",
  240. "#endif",
  241. // displacement mapping
  242. "#ifdef VERTEX_TEXTURES",
  243. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  244. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  245. "vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
  246. "gl_Position = projectionMatrix * displacedPosition;",
  247. "#else",
  248. "gl_Position = projectionMatrix * mvPosition;",
  249. "#endif",
  250. THREE.ShaderChunk[ "shadowmap_vertex" ],
  251. "}"
  252. ].join("\n")
  253. },
  254. /* -------------------------------------------------------------------------
  255. // Cube map shader
  256. ------------------------------------------------------------------------- */
  257. 'cube': {
  258. uniforms: { "tCube": { type: "t", value: 1, texture: null } },
  259. vertexShader: [
  260. "varying vec3 vViewPosition;",
  261. "void main() {",
  262. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  263. "vViewPosition = cameraPosition - mPosition.xyz;",
  264. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  265. "}"
  266. ].join("\n"),
  267. fragmentShader: [
  268. "uniform samplerCube tCube;",
  269. "varying vec3 vViewPosition;",
  270. "void main() {",
  271. "vec3 wPos = cameraPosition - vViewPosition;",
  272. "gl_FragColor = textureCube( tCube, vec3( - wPos.x, wPos.yz ) );",
  273. "}"
  274. ].join("\n")
  275. }
  276. }
  277. };
  278. };
粤ICP备19079148号