gaussian_surface.frag 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (C) 2023, Inria
  3. * GRAPHDECO research group, https://team.inria.fr/graphdeco
  4. * All rights reserved.
  5. *
  6. * This software is free for non-commercial, research and evaluation use
  7. * under the terms of the LICENSE.md file.
  8. *
  9. * For inquiries contact sibr@inria.fr and/or George.Drettakis@inria.fr
  10. */
  11. #version 430
  12. uniform mat4 MVP;
  13. uniform float alpha_limit;
  14. uniform int stage;
  15. uniform vec3 rayOrigin;
  16. in vec3 worldPos;
  17. in vec3 ellipsoidCenter;
  18. in vec3 ellipsoidScale;
  19. in mat3 ellipsoidRotation;
  20. in vec3 colorVert;
  21. in float alphaVert;
  22. in flat int boxID;
  23. layout (location = 0) out vec4 out_color;
  24. layout (location = 1) out uint out_id;
  25. vec3 closestEllipsoidIntersection(vec3 rayDirection, out vec3 normal) {
  26. // Convert ray to ellipsoid space
  27. dvec3 localRayOrigin = (rayOrigin - ellipsoidCenter) * ellipsoidRotation;
  28. dvec3 localRayDirection = normalize(rayDirection * ellipsoidRotation);
  29. dvec3 oneover = double(1) / dvec3(ellipsoidScale);
  30. // Compute coefficients of quadratic equation
  31. double a = dot(localRayDirection * oneover, localRayDirection * oneover);
  32. double b = 2.0 * dot(localRayDirection * oneover, localRayOrigin * oneover);
  33. double c = dot(localRayOrigin * oneover, localRayOrigin * oneover) - 1.0;
  34. // Compute discriminant
  35. double discriminant = b * b - 4.0 * a * c;
  36. // If discriminant is negative, there is no intersection
  37. if (discriminant < 0.0) {
  38. return vec3(0.0);
  39. }
  40. // Compute two possible solutions for t
  41. float t1 = float((-b - sqrt(discriminant)) / (2.0 * a));
  42. float t2 = float((-b + sqrt(discriminant)) / (2.0 * a));
  43. // Take the smaller positive solution as the closest intersection
  44. float t = min(t1, t2);
  45. // Compute intersection point in ellipsoid space
  46. vec3 localIntersection = vec3(localRayOrigin + t * localRayDirection);
  47. // Compute normal vector in ellipsoid space
  48. vec3 localNormal = normalize(localIntersection / ellipsoidScale);
  49. // Convert normal vector to world space
  50. normal = normalize(ellipsoidRotation * localNormal);
  51. // Convert intersection point back to world space
  52. vec3 intersection = ellipsoidRotation * localIntersection + ellipsoidCenter;
  53. return intersection;
  54. }
  55. void main(void) {
  56. vec3 dir = normalize(worldPos - rayOrigin);
  57. vec3 normal;
  58. vec3 intersection = closestEllipsoidIntersection(dir, normal);
  59. float align = max(0.4, dot(-dir, normal));
  60. out_color = vec4(1, 0, 0, 1);
  61. if(intersection == vec3(0))
  62. discard;
  63. vec4 newPos = MVP * vec4(intersection, 1);
  64. newPos /= newPos.w;
  65. gl_FragDepth = newPos.z;
  66. float a = stage == 0 ? 1.0 : 0.05f;
  67. out_color = vec4(align * colorVert, a);
  68. out_id = boxID;
  69. }