Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntersectCube algorithm???? #4

Open
MythLQ opened this issue Aug 12, 2016 · 6 comments
Open

IntersectCube algorithm???? #4

MythLQ opened this issue Aug 12, 2016 · 6 comments

Comments

@MythLQ
Copy link

MythLQ commented Aug 12, 2016

This is a very beautiful example. I do not understand the algorithm of IntersectCube, when I read this example source code, can you give me some ideas?

vec2 intersectCube(vec3 origin, vec3 ray, vec3 cubeMin, vec3 cubeMax) {
vec3 tMin = (cubeMin - origin) / ray;
vec3 tMax = (cubeMax - origin) / ray;
vec3 t1 = min(tMin, tMax);
vec3 t2 = max(tMin, tMax);
float tNear = max(max(t1.x, t1.y), t1.z);
float tFar = min(min(t2.x, t2.y), t2.z);
return vec2(tNear, tFar);
}

@evanw
Copy link
Owner

evanw commented Aug 12, 2016

Good question! That uses something called the "slab intersection" method. Here's one article I found: https://tavianator.com/fast-branchless-raybounding-box-intersections/. That's for 2D, not 3D, but the idea is the same.

@MythLQ
Copy link
Author

MythLQ commented Aug 13, 2016

Thank you very much!

@MythLQ
Copy link
Author

MythLQ commented Aug 14, 2016

When I read the code down, I had a new problem. What did you use the algorithm or ideas in causticsShader, I am confused. Can you give me some ideas or information?Hope to get your answer, thank you! Email :[email protected]

@evanw
Copy link
Owner

evanw commented Aug 14, 2016

I already wrote an article that talks about how the caustics are rendered. Is that what you are looking for?

@MythLQ
Copy link
Author

MythLQ commented Aug 21, 2016

Thank you very much for your reply! This is a very good way to render the caustics!

@MythLQ
Copy link
Author

MythLQ commented Aug 21, 2016

Your answers give me a lot of inspiration, but I still have a lot of problems. I hope you can give me some advice. Thank you

void main() {
    vec4 info = texture2D(water, LIGHTGLgl_Vertex.xy * 0.5 + 0.5);
    info.ba *= 0.5;
    vec3 normal = vec3(info.b, sqrt(1.0 - dot(info.ba, info.ba)), info.a);
    // /* project the vertices along the refracted vertex ray */                        
    vec3 refractedLight = refract(-light, vec3(0.0, 1.0, 0.0), IOR_AIR / IOR_WATER);
    ray = refract(-light, normal, IOR_AIR / IOR_WATER); 
    oldPos = project(LIGHTGLgl_Vertex.xzy, refractedLight, refractedLight);
    newPos = project(LIGHTGLgl_Vertex.xzy + vec3(0.0, info.r, 0.0), ray, refractedLight);
    gl_Position = vec4((newPos.xz + refractedLight.xz / refractedLight.y), 0.0, 1.0);
}
info.ba *= 0.5;    

Why "info.ba" is multiplied by 0.5?

gl_Position = vec4((newPos.xz + refractedLight.xz / refractedLight.y), 0.0, 1.0); 

What algorithm is used to calculate "gl_Position" over here?

void main() {
     /* if the triangle gets smaller, it gets brighter, and vice versa */        
    float oldArea = length(dFdx(oldPos)) * length(dFdy(oldPos));
    float newArea = length(dFdx(newPos)) * length(dFdy(newPos));
    gl_FragColor = vec4(oldArea / newArea * 0.2, 1.0, 0.0, 0.0);
    vec3 refractedLight = refract(-light, vec3(0.0, 1.0, 0.0), IOR_AIR / IOR_WATER);
    /* compute a blob shadow and make sure we only draw a shadow if the player is blocking the light */ 
    vec3 dir = (sphereCenter - newPos) / sphereRadius;
    vec3 area = cross(dir, refractedLight);
    float shadow = dot(area, area);
    float dist = dot(dir, -refractedLight);
    shadow = 1.0 + (shadow - 1.0) / (0.05 + dist * 0.025);
    shadow = clamp(1.0 / (1.0 + exp(-shadow)), 0.0, 1.0);
    shadow = mix(1.0, shadow, clamp(dist * 2.0, 0.0, 1.0));
    gl_FragColor.g = shadow;
    /* shadow for the rim of the pool */      
    vec2 t = intersectCube(newPos, -refractedLight, vec3(-1.0, -poolHeight, -1.0), vec3(1.0, 2.0, 1.0));
    gl_FragColor.r *= 1.0 / (1.0 + exp(-200.0 / (1.0 + 10.0 * (t.y - t.x)) * (newPos.y - refractedLight.y * t.y - 2.0 / 12.0)));
}

What algorithm is used to compute a blob shadow in this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants