Interacting with the 3D scene
Shaders are rendered on top of polyscope. But you can make the two work together! For instance, if you are doing any form of raytracing, by using the Shader STL's "camera.glsl", you can generate rays that follow the polyscope camera! (If you want to do custom stuff, polyscope camera is passed as uniforms).
#include <camera.glsl>
void main() {
vec3 ro, rd;
polyscopeRay(ro, rd);
// trace against ro/rd here
}
Sharing the screen instead of the scene
A flat shader has the same problem one dimension down: it draws into its own rectangle, so
gl_FragCoord.xy / iResolution means nothing to anything outside it. screenPoint() is the
answer polyscopeRay gives for the 3D case, the position of this fragment on the window,
0..1 with y up, wherever the shader is placed and whatever its resolution is.
That is the space vec2 parameters live in, so a point dragged in
the panel and the object the shader draws for it stay together.
Using polyscope depth buffer
To make polyscope objects coherent with a shader, you can use its depth buffer. Reach for visibleOverScene (or any of the other scene* functions in camera.glsl) and the buffer is passed on automatically:
Minimal example
#include <camera.glsl>
#include <sdf.glsl>
#include <raymarch.glsl>
float sceneSDF(vec3 p) {
vec3 q = p - vec3(0.9, 0.3, 0.0);
float a = sdSphere(q, 0.22);
vec3 orbit = vec3(0.22 * sin(iTime), 0.22 * cos(iTime), 0.0);
float b = sdSphere(q - orbit, 0.13);
return opSmoothUnion(a, b, 0.15);
}
void main() {
vec3 ro, rd; polyscopeRay(ro, rd);
vec3 pos;
if (marchScene(ro, rd, pos) && visibleOverScene(pos)) {
vec3 n = sceneNormal(pos);
fragColor = vec4(shadeDefault(pos, n, rd, vec3(0.85, 0.25, 0.25), vec3(1.0)), 1.0);
} else {
discard; // let the mesh show through instead
}
}
When you need to activate the depth buffer
Currently, the scanner that checks if you need the depth buffer only reads the shader's own text, not what it pulls in through #include. A helper header of your own that wraps visibleOverScene internally won't be picked up, the shader that includes it has to call shader->useSceneDepth() from C++ instead, once.