0

I have been wondering how I can combine two methods of rendering in a way that the rasterized on-screen shape serves as a canvas for ray-march based rendering in fragment shader.

Take these beautiful examples: https://www.shadertoy.com/view/XsjXRm or https://www.shadertoy.com/view/MtXSzS The visible part of them can be roughly represented as sphere. Now what I'd like to do is to put say two spheres into some place in the world and run the regular rasterization pass. The rasterization will yield which pixels are occupied by the models and for those pixels I'd like to actually run the shadertoy ray-marching algorithms to get the desired look (my two spheres look like shadertoy "spheres" in the examples above).

Is this something doable?

P.S. I know rasterization and matrix/spaces transformation quite well, but I have very vague understanding of how ray-marching works. Pardon my ignorance.

genpfault
  • 51,148
  • 11
  • 85
  • 139
lhog
  • 105
  • 1
  • 8
  • You simply run that ray-marching code in the fragment shader when rasterizing your spheres. Just make sure to calculate the ray direction correctly (based off `gl_FragCoord` and MVP matrices). Does it answer your question, or did I misunderstand your problem? – Yakov Galka May 22 '20 at 15:15
  • @ybungalobill Thanks, but I'd appreciate some trivial example, that would run several raymarching steps on the rasterized pixels, with `ray origin`/`ray direction` defined in rasterization frag shader. I guess I'm struggling with how to align math usually used in ray marching passes with what I have as a result of rasterization. Long story short - I'd appreciate trivial, yet generalize-able example. – lhog May 24 '20 at 09:24

1 Answers1

1

This is definitely possible.

The idea is to use the same camera for ray tracing and for rasterization. You can get the camera's position from the camera matrix in the fragment shader and you can get the camera's direction by subtracting the fragments position from the camera's position and normalizing it.

This way the rays are only cast from the camera to the visible fragments.

Leon van Noord
  • 868
  • 1
  • 7
  • 24