ShaderUtils.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var ShaderUtils = {
  2. lib: { 'fresnel': {
  3. uniforms: {
  4. "mRefractionRatio": { type: "f", value: 1.02 },
  5. "mFresnelBias": { type: "f", value: 0.1 },
  6. "mFresnelPower": { type: "f", value: 2.0 },
  7. "mFresnelScale": { type: "f", value: 1.0 },
  8. "tCube": { type: "t", value: 1, texture: null }
  9. },
  10. fragment_shader: [
  11. "uniform samplerCube tCube;",
  12. "varying vec3 vReflect;",
  13. "varying vec3 vRefract[3];",
  14. "varying float vReflectionFactor;",
  15. "void main() {",
  16. "vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  17. "vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
  18. "refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;",
  19. "refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;",
  20. "refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;",
  21. "refractedColor.a = 1.0;",
  22. "gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
  23. "}"
  24. ].join("\n"),
  25. vertex_shader: [
  26. "attribute vec3 position;",
  27. "attribute vec3 normal;",
  28. "attribute vec3 uv;",
  29. "uniform mat4 objMatrix;",
  30. "uniform mat4 modelViewMatrix;",
  31. "uniform mat4 projectionMatrix;",
  32. "uniform vec3 cameraPosition;",
  33. "uniform float mRefractionRatio;",
  34. "uniform float mFresnelBias;",
  35. "uniform float mFresnelScale;",
  36. "uniform float mFresnelPower;",
  37. "varying vec3 vReflect;",
  38. "varying vec3 vRefract[3];",
  39. "varying float vReflectionFactor;",
  40. "void main(void) {",
  41. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  42. "vec4 mPosition = objMatrix * vec4( position, 1.0 );",
  43. "vec3 nWorld = normalize ( mat3( objMatrix[0].xyz, objMatrix[1].xyz, objMatrix[2].xyz ) * normal );",
  44. "vec3 I = mPosition.xyz - cameraPosition;",
  45. "vReflect = reflect( I, nWorld );",
  46. "vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );",
  47. "vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );",
  48. "vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );",
  49. "vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );",
  50. "gl_Position = projectionMatrix * mvPosition;",
  51. "}"
  52. ].join("\n")
  53. }
  54. }
  55. };