FresnelShader.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. console.warn( "THREE.FresnelShader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author alteredq / http://alteredqualia.com/
  4. *
  5. * Based on Nvidia Cg tutorial
  6. */
  7. THREE.FresnelShader = {
  8. uniforms: {
  9. "mRefractionRatio": { value: 1.02 },
  10. "mFresnelBias": { value: 0.1 },
  11. "mFresnelPower": { value: 2.0 },
  12. "mFresnelScale": { value: 1.0 },
  13. "tCube": { value: null }
  14. },
  15. vertexShader: [
  16. "uniform float mRefractionRatio;",
  17. "uniform float mFresnelBias;",
  18. "uniform float mFresnelScale;",
  19. "uniform float mFresnelPower;",
  20. "varying vec3 vReflect;",
  21. "varying vec3 vRefract[3];",
  22. "varying float vReflectionFactor;",
  23. "void main() {",
  24. " vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  25. " vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  26. " vec3 worldNormal = normalize( mat3( modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[2].xyz ) * normal );",
  27. " vec3 I = worldPosition.xyz - cameraPosition;",
  28. " vReflect = reflect( I, worldNormal );",
  29. " vRefract[0] = refract( normalize( I ), worldNormal, mRefractionRatio );",
  30. " vRefract[1] = refract( normalize( I ), worldNormal, mRefractionRatio * 0.99 );",
  31. " vRefract[2] = refract( normalize( I ), worldNormal, mRefractionRatio * 0.98 );",
  32. " vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), worldNormal ), mFresnelPower );",
  33. " gl_Position = projectionMatrix * mvPosition;",
  34. "}"
  35. ].join( "\n" ),
  36. fragmentShader: [
  37. "uniform samplerCube tCube;",
  38. "varying vec3 vReflect;",
  39. "varying vec3 vRefract[3];",
  40. "varying float vReflectionFactor;",
  41. "void main() {",
  42. " vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  43. " vec4 refractedColor = vec4( 1.0 );",
  44. " refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;",
  45. " refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;",
  46. " refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;",
  47. " gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
  48. "}"
  49. ].join( "\n" )
  50. };