ShaderUtils.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. "uniform float mRefractionRatio;",
  27. "uniform float mFresnelBias;",
  28. "uniform float mFresnelScale;",
  29. "uniform float mFresnelPower;",
  30. "varying vec3 vReflect;",
  31. "varying vec3 vRefract[3];",
  32. "varying float vReflectionFactor;",
  33. "void main(void) {",
  34. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  35. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  36. "vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );",
  37. "vec3 I = mPosition.xyz - cameraPosition;",
  38. "vReflect = reflect( I, nWorld );",
  39. "vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );",
  40. "vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );",
  41. "vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );",
  42. "vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );",
  43. "gl_Position = projectionMatrix * mvPosition;",
  44. "}"
  45. ].join("\n")
  46. }
  47. }
  48. };