reflectionFunction.fx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifdef USE_LOCAL_REFLECTIONMAP_CUBIC
  2. vec3 parallaxCorrectNormal( vec3 vertexPos, vec3 origVec, vec3 cubeSize, vec3 cubePos ) {
  3. // Find the ray intersection with box plane
  4. vec3 invOrigVec = vec3(1.0,1.0,1.0) / origVec;
  5. vec3 halfSize = cubeSize * 0.5;
  6. vec3 intersecAtMaxPlane = (cubePos + halfSize - vertexPos) * invOrigVec;
  7. vec3 intersecAtMinPlane = (cubePos - halfSize - vertexPos) * invOrigVec;
  8. // Get the largest intersection values (we are not intersted in negative values)
  9. vec3 largestIntersec = max(intersecAtMaxPlane, intersecAtMinPlane);
  10. // Get the closest of all solutions
  11. float distance = min(min(largestIntersec.x, largestIntersec.y), largestIntersec.z);
  12. // Get the intersection position
  13. vec3 intersectPositionWS = vertexPos + origVec * distance;
  14. // Get corrected vector
  15. return intersectPositionWS - cubePos;
  16. }
  17. #endif
  18. vec3 computeReflectionCoords(vec4 worldPos, vec3 worldNormal)
  19. {
  20. #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
  21. vec3 direction = vDirectionW;
  22. float t = clamp(direction.y * -0.5 + 0.5, 0., 1.0);
  23. float s = atan(direction.z, direction.x) * RECIPROCAL_PI2 + 0.5;
  24. #ifdef REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED
  25. return vec3(1.0 - s, t, 0);
  26. #else
  27. return vec3(s, t, 0);
  28. #endif
  29. #endif
  30. #ifdef REFLECTIONMAP_EQUIRECTANGULAR
  31. vec3 cameraToVertex = normalize(worldPos.xyz - vEyePosition.xyz);
  32. vec3 r = reflect(cameraToVertex, worldNormal);
  33. float t = clamp(r.y * -0.5 + 0.5, 0., 1.0);
  34. float s = atan(r.z, r.x) * RECIPROCAL_PI2 + 0.5;
  35. return vec3(s, t, 0);
  36. #endif
  37. #ifdef REFLECTIONMAP_SPHERICAL
  38. vec3 viewDir = normalize(vec3(view * worldPos));
  39. vec3 viewNormal = normalize(vec3(view * vec4(worldNormal, 0.0)));
  40. vec3 r = reflect(viewDir, viewNormal);
  41. r.z = r.z - 1.0;
  42. float m = 2.0 * length(r);
  43. return vec3(r.x / m + 0.5, 1.0 - r.y / m - 0.5, 0);
  44. #endif
  45. #ifdef REFLECTIONMAP_PLANAR
  46. vec3 viewDir = worldPos.xyz - vEyePosition.xyz;
  47. vec3 coords = normalize(reflect(viewDir, worldNormal));
  48. return vec3(reflectionMatrix * vec4(coords, 1));
  49. #endif
  50. #ifdef REFLECTIONMAP_CUBIC
  51. vec3 viewDir = normalize(worldPos.xyz - vEyePosition.xyz);
  52. // worldNormal has already been normalized.
  53. vec3 coords = reflect(viewDir, worldNormal);
  54. #ifdef USE_LOCAL_REFLECTIONMAP_CUBIC
  55. coords = parallaxCorrectNormal(worldPos.xyz, coords, vReflectionSize, vReflectionPosition);
  56. #endif
  57. coords = vec3(reflectionMatrix * vec4(coords, 0));
  58. #ifdef INVERTCUBICMAP
  59. coords.y *= -1.0;
  60. #endif
  61. return coords;
  62. #endif
  63. #ifdef REFLECTIONMAP_PROJECTION
  64. return vec3(reflectionMatrix * (view * worldPos));
  65. #endif
  66. #ifdef REFLECTIONMAP_SKYBOX
  67. return vPositionUVW;
  68. #endif
  69. #ifdef REFLECTIONMAP_EXPLICIT
  70. return vec3(0, 0, 0);
  71. #endif
  72. }