reflectionFunction.fx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = normalize(vDirectionW);
  22. float lon = atan(direction.z, direction.x);
  23. float lat = acos(direction.y);
  24. vec2 sphereCoords = vec2(lon, lat) * RECIPROCAL_PI2 * 2.0;
  25. float s = sphereCoords.x * 0.5 + 0.5;
  26. float t = sphereCoords.y;
  27. #ifdef REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED
  28. return vec3(1.0 - s, t, 0);
  29. #else
  30. return vec3(s, t, 0);
  31. #endif
  32. #endif
  33. #ifdef REFLECTIONMAP_EQUIRECTANGULAR
  34. vec3 cameraToVertex = normalize(worldPos.xyz - vEyePosition.xyz);
  35. vec3 r = normalize(reflect(cameraToVertex, worldNormal));
  36. r = vec3(reflectionMatrix * vec4(r, 0));
  37. float lon = atan(r.z, r.x);
  38. float lat = acos(r.y);
  39. vec2 sphereCoords = vec2(lon, lat) * RECIPROCAL_PI2 * 2.0;
  40. float s = sphereCoords.x * 0.5 + 0.5;
  41. float t = sphereCoords.y;
  42. return vec3(s, t, 0);
  43. #endif
  44. #ifdef REFLECTIONMAP_SPHERICAL
  45. vec3 viewDir = normalize(vec3(view * worldPos));
  46. vec3 viewNormal = normalize(vec3(view * vec4(worldNormal, 0.0)));
  47. vec3 r = reflect(viewDir, viewNormal);
  48. r = vec3(reflectionMatrix * vec4(r, 0));
  49. r.z = r.z - 1.0;
  50. float m = 2.0 * length(r);
  51. return vec3(r.x / m + 0.5, 1.0 - r.y / m - 0.5, 0);
  52. #endif
  53. #ifdef REFLECTIONMAP_PLANAR
  54. vec3 viewDir = worldPos.xyz - vEyePosition.xyz;
  55. vec3 coords = normalize(reflect(viewDir, worldNormal));
  56. return vec3(reflectionMatrix * vec4(coords, 1));
  57. #endif
  58. #ifdef REFLECTIONMAP_CUBIC
  59. vec3 viewDir = normalize(worldPos.xyz - vEyePosition.xyz);
  60. // worldNormal has already been normalized.
  61. vec3 coords = reflect(viewDir, worldNormal);
  62. #ifdef USE_LOCAL_REFLECTIONMAP_CUBIC
  63. coords = parallaxCorrectNormal(worldPos.xyz, coords, vReflectionSize, vReflectionPosition);
  64. #endif
  65. coords = vec3(reflectionMatrix * vec4(coords, 0));
  66. #ifdef INVERTCUBICMAP
  67. coords.y *= -1.0;
  68. #endif
  69. return coords;
  70. #endif
  71. #ifdef REFLECTIONMAP_PROJECTION
  72. return vec3(reflectionMatrix * (view * worldPos));
  73. #endif
  74. #ifdef REFLECTIONMAP_SKYBOX
  75. return vec3(reflectionMatrix * vec4(vPositionUVW, 0));
  76. #endif
  77. #ifdef REFLECTIONMAP_EXPLICIT
  78. return vec3(0, 0, 0);
  79. #endif
  80. }