ShaderUtils.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mr.doob / http://mrdoob.com/
  4. *
  5. * ShaderUtils currently contains
  6. * fresnel
  7. * normal
  8. * cube
  9. * convolution
  10. * film
  11. * screen
  12. * basic
  13. */
  14. if ( THREE.WebGLRenderer ) {
  15. THREE.ShaderUtils = {
  16. lib: {
  17. /* -------------------------------------------------------------------------
  18. // Fresnel shader
  19. // - based on Nvidia Cg tutorial
  20. ------------------------------------------------------------------------- */
  21. 'fresnel': {
  22. uniforms: {
  23. "mRefractionRatio": { type: "f", value: 1.02 },
  24. "mFresnelBias": { type: "f", value: 0.1 },
  25. "mFresnelPower": { type: "f", value: 2.0 },
  26. "mFresnelScale": { type: "f", value: 1.0 },
  27. "tCube": { type: "t", value: 1, texture: null }
  28. },
  29. fragmentShader: [
  30. "uniform samplerCube tCube;",
  31. "varying vec3 vReflect;",
  32. "varying vec3 vRefract[3];",
  33. "varying float vReflectionFactor;",
  34. "void main() {",
  35. "vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  36. "vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
  37. "refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;",
  38. "refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;",
  39. "refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;",
  40. "refractedColor.a = 1.0;",
  41. "gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
  42. "}"
  43. ].join("\n"),
  44. vertexShader: [
  45. "uniform float mRefractionRatio;",
  46. "uniform float mFresnelBias;",
  47. "uniform float mFresnelScale;",
  48. "uniform float mFresnelPower;",
  49. "varying vec3 vReflect;",
  50. "varying vec3 vRefract[3];",
  51. "varying float vReflectionFactor;",
  52. "void main() {",
  53. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  54. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  55. "vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );",
  56. "vec3 I = mPosition.xyz - cameraPosition;",
  57. "vReflect = reflect( I, nWorld );",
  58. "vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );",
  59. "vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );",
  60. "vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );",
  61. "vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );",
  62. "gl_Position = projectionMatrix * mvPosition;",
  63. "}"
  64. ].join("\n")
  65. },
  66. /* -------------------------------------------------------------------------
  67. // Normal map shader
  68. // - Blinn-Phong
  69. // - normal + diffuse + specular + AO + displacement maps
  70. // - point and directional lights (use with "lights: true" material option)
  71. ------------------------------------------------------------------------- */
  72. 'normal' : {
  73. uniforms: THREE.UniformsUtils.merge( [
  74. THREE.UniformsLib[ "fog" ],
  75. THREE.UniformsLib[ "lights" ],
  76. {
  77. "enableAO" : { type: "i", value: 0 },
  78. "enableDiffuse" : { type: "i", value: 0 },
  79. "enableSpecular": { type: "i", value: 0 },
  80. "tDiffuse" : { type: "t", value: 0, texture: null },
  81. "tNormal" : { type: "t", value: 2, texture: null },
  82. "tSpecular" : { type: "t", value: 3, texture: null },
  83. "tAO" : { type: "t", value: 4, texture: null },
  84. "uNormalScale": { type: "f", value: 1.0 },
  85. "tDisplacement": { type: "t", value: 5, texture: null },
  86. "uDisplacementBias": { type: "f", value: 0.0 },
  87. "uDisplacementScale": { type: "f", value: 1.0 },
  88. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  89. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  90. "uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
  91. "uShininess": { type: "f", value: 30 },
  92. "uOpacity": { type: "f", value: 1 }
  93. }
  94. ] ),
  95. fragmentShader: [
  96. "uniform vec3 uAmbientColor;",
  97. "uniform vec3 uDiffuseColor;",
  98. "uniform vec3 uSpecularColor;",
  99. "uniform float uShininess;",
  100. "uniform float uOpacity;",
  101. "uniform bool enableDiffuse;",
  102. "uniform bool enableSpecular;",
  103. "uniform bool enableAO;",
  104. "uniform sampler2D tDiffuse;",
  105. "uniform sampler2D tNormal;",
  106. "uniform sampler2D tSpecular;",
  107. "uniform sampler2D tAO;",
  108. "uniform float uNormalScale;",
  109. "varying vec3 vTangent;",
  110. "varying vec3 vBinormal;",
  111. "varying vec3 vNormal;",
  112. "varying vec2 vUv;",
  113. "uniform vec3 ambientLightColor;",
  114. "#if MAX_DIR_LIGHTS > 0",
  115. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  116. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  117. "#endif",
  118. "#if MAX_POINT_LIGHTS > 0",
  119. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  120. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  121. "#endif",
  122. "varying vec3 vViewPosition;",
  123. THREE.ShaderChunk[ "fog_pars_fragment" ],
  124. "void main() {",
  125. "gl_FragColor = vec4( 1.0 );",
  126. "vec4 mColor = vec4( uDiffuseColor, uOpacity );",
  127. "vec4 mSpecular = vec4( uSpecularColor, uOpacity );",
  128. "vec3 specularTex = vec3( 1.0 );",
  129. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  130. "normalTex.xy *= uNormalScale;",
  131. "normalTex = normalize( normalTex );",
  132. "if( enableDiffuse )",
  133. "gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
  134. "if( enableAO )",
  135. "gl_FragColor = gl_FragColor * texture2D( tAO, vUv );",
  136. "if( enableSpecular )",
  137. "specularTex = texture2D( tSpecular, vUv ).xyz;",
  138. "mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
  139. "vec3 finalNormal = tsb * normalTex;",
  140. "vec3 normal = normalize( finalNormal );",
  141. "vec3 viewPosition = normalize( vViewPosition );",
  142. // point lights
  143. "#if MAX_POINT_LIGHTS > 0",
  144. "vec4 pointTotal = vec4( 0.0 );",
  145. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  146. "vec3 pointVector = normalize( vPointLight[ i ].xyz );",
  147. "vec3 pointHalfVector = normalize( vPointLight[ i ].xyz + viewPosition );",
  148. "float pointDistance = vPointLight[ i ].w;",
  149. "float pointDotNormalHalf = dot( normal, pointHalfVector );",
  150. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  151. "float pointSpecularWeight = 0.0;",
  152. "if ( pointDotNormalHalf >= 0.0 )",
  153. "pointSpecularWeight = specularTex.r * pow( pointDotNormalHalf, uShininess );",
  154. "pointTotal += pointDistance * vec4( pointLightColor[ i ], 1.0 ) * ( mColor * pointDiffuseWeight + mSpecular * pointSpecularWeight * pointDiffuseWeight );",
  155. "}",
  156. "#endif",
  157. // directional lights
  158. "#if MAX_DIR_LIGHTS > 0",
  159. "vec4 dirTotal = vec4( 0.0 );",
  160. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  161. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  162. "vec3 dirVector = normalize( lDirection.xyz );",
  163. "vec3 dirHalfVector = normalize( lDirection.xyz + viewPosition );",
  164. "float dirDotNormalHalf = dot( normal, dirHalfVector );",
  165. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  166. "float dirSpecularWeight = 0.0;",
  167. "if ( dirDotNormalHalf >= 0.0 )",
  168. "dirSpecularWeight = specularTex.r * pow( dirDotNormalHalf, uShininess );",
  169. "dirTotal += vec4( directionalLightColor[ i ], 1.0 ) * ( mColor * dirDiffuseWeight + mSpecular * dirSpecularWeight * dirDiffuseWeight );",
  170. "}",
  171. "#endif",
  172. // all lights contribution summation
  173. "vec4 totalLight = vec4( ambientLightColor * uAmbientColor, uOpacity );",
  174. "#if MAX_DIR_LIGHTS > 0",
  175. "totalLight += dirTotal;",
  176. "#endif",
  177. "#if MAX_POINT_LIGHTS > 0",
  178. "totalLight += pointTotal;",
  179. "#endif",
  180. "gl_FragColor = gl_FragColor * totalLight;",
  181. THREE.ShaderChunk[ "fog_fragment" ],
  182. "}"
  183. ].join("\n"),
  184. vertexShader: [
  185. "attribute vec4 tangent;",
  186. "#ifdef VERTEX_TEXTURES",
  187. "uniform sampler2D tDisplacement;",
  188. "uniform float uDisplacementScale;",
  189. "uniform float uDisplacementBias;",
  190. "#endif",
  191. "varying vec3 vTangent;",
  192. "varying vec3 vBinormal;",
  193. "varying vec3 vNormal;",
  194. "varying vec2 vUv;",
  195. "#if MAX_POINT_LIGHTS > 0",
  196. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  197. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  198. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  199. "#endif",
  200. "varying vec3 vViewPosition;",
  201. "void main() {",
  202. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  203. "vViewPosition = cameraPosition - mPosition.xyz;",
  204. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  205. "vNormal = normalize( normalMatrix * normal );",
  206. // tangent and binormal vectors
  207. "vTangent = normalize( normalMatrix * tangent.xyz );",
  208. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  209. "vBinormal = normalize( vBinormal );",
  210. "vUv = uv;",
  211. // point lights
  212. "#if MAX_POINT_LIGHTS > 0",
  213. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  214. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  215. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  216. "float lDistance = 1.0;",
  217. "if ( pointLightDistance[ i ] > 0.0 )",
  218. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  219. "lVector = normalize( lVector );",
  220. "vPointLight[ i ] = vec4( lVector, lDistance );",
  221. "}",
  222. "#endif",
  223. // displacement mapping
  224. "#ifdef VERTEX_TEXTURES",
  225. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  226. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  227. "vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
  228. "gl_Position = projectionMatrix * displacedPosition;",
  229. "#else",
  230. "gl_Position = projectionMatrix * mvPosition;",
  231. "#endif",
  232. "}"
  233. ].join("\n")
  234. },
  235. /* -------------------------------------------------------------------------
  236. // Cube map shader
  237. ------------------------------------------------------------------------- */
  238. 'cube': {
  239. uniforms: { "tCube": { type: "t", value: 1, texture: null } },
  240. vertexShader: [
  241. "varying vec3 vViewPosition;",
  242. "void main() {",
  243. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  244. "vViewPosition = cameraPosition - mPosition.xyz;",
  245. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  246. "}"
  247. ].join("\n"),
  248. fragmentShader: [
  249. "uniform samplerCube tCube;",
  250. "varying vec3 vViewPosition;",
  251. "void main() {",
  252. "vec3 wPos = cameraPosition - vViewPosition;",
  253. "gl_FragColor = textureCube( tCube, vec3( - wPos.x, wPos.yz ) );",
  254. "}"
  255. ].join("\n")
  256. },
  257. /* ------------------------------------------------------------------------
  258. // Convolution shader
  259. // - ported from o3d sample to WebGL / GLSL
  260. // http://o3d.googlecode.com/svn/trunk/samples/convolution.html
  261. ------------------------------------------------------------------------ */
  262. 'convolution': {
  263. uniforms: {
  264. "tDiffuse" : { type: "t", value: 0, texture: null },
  265. "uImageIncrement" : { type: "v2", value: new THREE.Vector2( 0.001953125, 0.0 ) },
  266. "cKernel" : { type: "fv1", value: [] }
  267. },
  268. vertexShader: [
  269. "varying vec2 vUv;",
  270. "uniform vec2 uImageIncrement;",
  271. //"#define KERNEL_SIZE 25.0",
  272. "void main(void) {",
  273. "vUv = uv - ((KERNEL_SIZE - 1.0) / 2.0) * uImageIncrement;",
  274. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  275. "}"
  276. ].join("\n"),
  277. fragmentShader: [
  278. "varying vec2 vUv;",
  279. "uniform sampler2D tDiffuse;",
  280. "uniform vec2 uImageIncrement;",
  281. //"#define KERNEL_SIZE 25",
  282. "uniform float cKernel[KERNEL_SIZE];",
  283. "void main(void) {",
  284. "vec2 imageCoord = vUv;",
  285. "vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );",
  286. "for( int i=0; i<KERNEL_SIZE; ++i ) {",
  287. "sum += texture2D( tDiffuse, imageCoord ) * cKernel[i];",
  288. "imageCoord += uImageIncrement;",
  289. "}",
  290. "gl_FragColor = sum;",
  291. "}"
  292. ].join("\n")
  293. },
  294. /* -------------------------------------------------------------------------
  295. // Film grain & scanlines shader
  296. // - ported from HLSL to WebGL / GLSL
  297. // http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html
  298. // Screen Space Static Postprocessor
  299. //
  300. // Produces an analogue noise overlay similar to a film grain / TV static
  301. //
  302. // Original implementation and noise algorithm
  303. // Pat 'Hawthorne' Shearon
  304. //
  305. // Optimized scanlines + noise version with intensity scaling
  306. // Georg 'Leviathan' Steinrohder
  307. // This version is provided under a Creative Commons Attribution 3.0 License
  308. // http://creativecommons.org/licenses/by/3.0/
  309. ------------------------------------------------------------------------- */
  310. 'film': {
  311. uniforms: {
  312. tDiffuse: { type: "t", value: 0, texture: null },
  313. time: { type: "f", value: 0.0 },
  314. nIntensity: { type: "f", value: 0.5 },
  315. sIntensity: { type: "f", value: 0.05 },
  316. sCount: { type: "f", value: 4096 },
  317. grayscale: { type: "i", value: 1 }
  318. },
  319. vertexShader: [
  320. "varying vec2 vUv;",
  321. "void main() {",
  322. "vUv = vec2( uv.x, 1.0 - uv.y );",
  323. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  324. "}"
  325. ].join("\n"),
  326. fragmentShader: [
  327. "varying vec2 vUv;",
  328. "uniform sampler2D tDiffuse;",
  329. // control parameter
  330. "uniform float time;",
  331. "uniform bool grayscale;",
  332. // noise effect intensity value (0 = no effect, 1 = full effect)
  333. "uniform float nIntensity;",
  334. // scanlines effect intensity value (0 = no effect, 1 = full effect)
  335. "uniform float sIntensity;",
  336. // scanlines effect count value (0 = no effect, 4096 = full effect)
  337. "uniform float sCount;",
  338. "void main() {",
  339. // sample the source
  340. "vec4 cTextureScreen = texture2D( tDiffuse, vUv );",
  341. // make some noise
  342. "float x = vUv.x * vUv.y * time * 1000.0;",
  343. "x = mod( x, 13.0 ) * mod( x, 123.0 );",
  344. "float dx = mod( x, 0.01 );",
  345. // add noise
  346. "vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );",
  347. // get us a sine and cosine
  348. "vec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );",
  349. // add scanlines
  350. "cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;",
  351. // interpolate between source and result by intensity
  352. "cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );",
  353. // convert to grayscale if desired
  354. "if( grayscale ) {",
  355. "cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );",
  356. "}",
  357. "gl_FragColor = vec4( cResult, cTextureScreen.a );",
  358. "}"
  359. ].join("\n")
  360. },
  361. /* -------------------------------------------------------------------------
  362. // Full-screen textured quad shader
  363. ------------------------------------------------------------------------- */
  364. 'screen': {
  365. uniforms: {
  366. tDiffuse: { type: "t", value: 0, texture: null },
  367. opacity: { type: "f", value: 1.0 }
  368. },
  369. vertexShader: [
  370. "varying vec2 vUv;",
  371. "void main() {",
  372. "vUv = vec2( uv.x, 1.0 - uv.y );",
  373. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  374. "}"
  375. ].join("\n"),
  376. fragmentShader: [
  377. "varying vec2 vUv;",
  378. "uniform sampler2D tDiffuse;",
  379. "uniform float opacity;",
  380. "void main() {",
  381. "vec4 texel = texture2D( tDiffuse, vUv );",
  382. "gl_FragColor = opacity * texel;",
  383. "}"
  384. ].join("\n")
  385. },
  386. /* -------------------------------------------------------------------------
  387. // Simple test shader
  388. ------------------------------------------------------------------------- */
  389. 'basic': {
  390. uniforms: {},
  391. vertexShader: [
  392. "void main() {",
  393. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  394. "}"
  395. ].join("\n"),
  396. fragmentShader: [
  397. "void main() {",
  398. "gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );",
  399. "}"
  400. ].join("\n")
  401. }
  402. },
  403. buildKernel: function( sigma ) {
  404. // We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.
  405. function gauss( x, sigma ) {
  406. return Math.exp( - ( x * x ) / ( 2.0 * sigma * sigma ) );
  407. }
  408. var i, values, sum, halfWidth, kMaxKernelSize = 25, kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;
  409. if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
  410. halfWidth = ( kernelSize - 1 ) * 0.5
  411. values = new Array( kernelSize );
  412. sum = 0.0;
  413. for ( i = 0; i < kernelSize; ++i ) {
  414. values[ i ] = gauss( i - halfWidth, sigma );
  415. sum += values[ i ];
  416. }
  417. // normalize the kernel
  418. for ( i = 0; i < kernelSize; ++i ) values[ i ] /= sum;
  419. return values;
  420. }
  421. };
  422. };
粤ICP备19079148号