FresnelShader.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/#manual/en/introduction/Installation." );
  2. /**
  3. * Based on Nvidia Cg tutorial
  4. */
  5. THREE.FresnelShader = {
  6. uniforms: {
  7. "mRefractionRatio": { value: 1.02 },
  8. "mFresnelBias": { value: 0.1 },
  9. "mFresnelPower": { value: 2.0 },
  10. "mFresnelScale": { value: 1.0 },
  11. "tCube": { value: null }
  12. },
  13. vertexShader: [
  14. "uniform float mRefractionRatio;",
  15. "uniform float mFresnelBias;",
  16. "uniform float mFresnelScale;",
  17. "uniform float mFresnelPower;",
  18. "varying vec3 vReflect;",
  19. "varying vec3 vRefract[3];",
  20. "varying float vReflectionFactor;",
  21. "void main() {",
  22. " vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  23. " vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  24. " vec3 worldNormal = normalize( mat3( modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[2].xyz ) * normal );",
  25. " vec3 I = worldPosition.xyz - cameraPosition;",
  26. " vReflect = reflect( I, worldNormal );",
  27. " vRefract[0] = refract( normalize( I ), worldNormal, mRefractionRatio );",
  28. " vRefract[1] = refract( normalize( I ), worldNormal, mRefractionRatio * 0.99 );",
  29. " vRefract[2] = refract( normalize( I ), worldNormal, mRefractionRatio * 0.98 );",
  30. " vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), worldNormal ), mFresnelPower );",
  31. " gl_Position = projectionMatrix * mvPosition;",
  32. "}"
  33. ].join( "\n" ),
  34. fragmentShader: [
  35. "uniform samplerCube tCube;",
  36. "varying vec3 vReflect;",
  37. "varying vec3 vRefract[3];",
  38. "varying float vReflectionFactor;",
  39. "void main() {",
  40. " vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  41. " vec4 refractedColor = vec4( 1.0 );",
  42. " refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;",
  43. " refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;",
  44. " refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;",
  45. " gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
  46. "}"
  47. ].join( "\n" )
  48. };