admin管理员组文章数量:1279175
I'm trying to calculate lighting in a fragment shader, and I've defined my light sources as axis-aligned bounding boxes (AABB) in order to simulate "area lights" -- light-emitting volumes, rather than simple point lights.
I found and adapted this code on StackOverflow which correctly calculates the distance from the point to the bounds. But I was wondering if there's a clever way to extract the directional vector from this calculation as well?
/**
* @return The distance from `p` to the bounding box.
* @see
*/
float distance_to_bounds(in vec3 mins, in vec3 maxs, in vec3 p) {
float dx = max(max(mins.x - p.x, 0.0), p.x - maxs.x);
float dy = max(max(mins.y - p.y, 0.0), p.y - maxs.y);
float dz = max(max(mins.z - p.z, 0.0), p.z - maxs.z);
return sqrt(dx * dx + dy * dy + dz * dz);
}
I tried refactoring this as:
/**
* @return The directional vector from `p` to the bounding box.
* @see
*/
vec3 direction_to_bounds(in vec3 mins, in vec3 maxs, in vec3 p) {
float dx = max(max(mins.x - p.x, 0.0), p.x - maxs.x);
float dy = max(max(mins.y - p.y, 0.0), p.y - maxs.y);
float dz = max(max(mins.z - p.z, 0.0), p.z - maxs.z);
return vec3(dx, dy, dz);
}
Thinking that the length of the vector would be distance, but this produces incorrect looking results for both distance and direction. I tried negating the vector as well.
本文标签: 3dDistance and direction from point to axisaligned bounding boxStack Overflow
版权声明:本文标题:3d - Distance and direction from point to axis-aligned bounding box - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741302619a2371179.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论