rnd-team-dev / plotoptix

Data visualisation and ray tracing in Python based on OptiX 7.7 framework.
https://rnd.team/plotoptix
Other
499 stars 26 forks source link

Meaning of max_accumulation_frames parameter #38

Closed vitalitylearning2021 closed 1 year ago

vitalitylearning2021 commented 1 year ago

Good day, I'm using the following code:

import  numpy               as      np
from    plotoptix           import  NpOptiX
import  matplotlib.pyplot   as      plt

def displayResults(rt):

    print("Launch finished.")

    hitPositionsData = rt._hit_pos
    xHitPositions = hitPositionsData[:, :, 0]
    yHitPositions = hitPositionsData[:, :, 1]
    zHitPositions = hitPositionsData[:, :, 2]
    dHitPositions = hitPositionsData[:, :, 3]

    hitTriangle   = rt._geo_id[:, :, 1].reshape(rt._height, rt._width)

    print("Shape of rays array is {}.".format(xHitPositions.shape))

    xHitPositions = xHitPositions[hitTriangle < 0xFFFFFFFF]
    yHitPositions = yHitPositions[hitTriangle < 0xFFFFFFFF]

    print("Shape of hitting rays array is {}.".format(xHitPositions.shape))

    plt.plot(xHitPositions, yHitPositions, 'bo')
    plt.show()

    plt.imshow(dHitPositions)
    plt.colorbar()
    plt.show()

    plt.draw()

verticesTriangles   = np.array([[-1, -1, 0], [1, -1, 0], [-1, 1, 0], [1, -1, 0], [1, 1, 0], [-1, 1, 0]])
faceTriangles       = np.array([[0, 1, 2], [3, 4, 5]])

rt                  = NpOptiX(on_rt_accum_done = displayResults, width = 20, height = 20)

rt.set_mesh("Mesh", verticesTriangles, faceTriangles)

rt.setup_camera("Cam", eye = [0, 0, 2], fov = 25)

rt.set_param(max_accumulation_frames = 1)
rt.start()

The code uses the max_accumulation_frames parameter which is initially set to 1. With such a setting, I the code returns the following image representing the intersection points over the scattering surface:

max_accumulation_frames_1

It seems that the impinging rays are launched in an ordered grid.

If the max_accumulation_frames parameter is set to 2, then the result is the following:

max_accumulation_frames_2

and randomization occurs, as expected. We have however lost the old, ordered intersection points.

I have three questions about this behavior:

  1. Does this mean that the ordered ray buffer is replaced by the randomized one?
  2. Any time one increases the max_accumulation_frames parameter above 2, is the ray buffer replaced by a new one?
  3. What is the reason for replacing the ray buffer and so what is the reason for dealing with the max_accumulation_frames parameter?

Thank you very much for your help.

robertsulej commented 1 year ago

Hi,

Only CustomProj... camera modes are using buffers with (various) ray info provided by the user. These buffers stay constant in all accumulation frames. Other modes are calculating rays on the fly with camera shader programs.

Rays calculated by shaders are crossing each pixel of the 2D image, 1 ray - 1 pixel. In the first accumulation frame rays are distributed regularly, each ray is crossing the center of its corresponding pixel.

Image ray traced with a single accumulation frame can be noisy - rays in neighbouring pixels can be scattered in different directions if they hit diffuse surface. This is the first motivation for the multiple accumulation frames. Shooting many rays in the same direction and averaging radiance calculated along their paths gradually smooths out the resulting image.

Second effect is aliasing on the object edges and potential interference of the regular distribution of rays with the pattern of objects in the scene, causing ugly (and not realistic!) moiré patterns. Correct solution is to sample the scene with multiple rays randomly distributed within the pixel. So starting from the second accumulation frame there is a +/-0.5x0x5 pixel jitter added to the calculated ray direction.

In custom camera projection modes, ray info is corresponding exactly to what you provide in buffers if your ray tracing resolution matches the buffers shape, but only in the first accumulation frame. In the next frames values are interpolated according to the random jitter. Normally you limit max_accumulation_frames to 1 in custom modes, but it can depend on your goals.

vitalitylearning2021 commented 1 year ago

Thank you very much for your answer.

My question is: when you use multiple accumulation frames, either when using shaders or a customized camera, and you access the hit points buffer by rt._hit_pos, to which accumulation frame do you have access? The first one? The latter one? A middle one?

robertsulej commented 1 year ago

All values are averaged, except the primitive id buffer. Id's of hits are from the last frame.

vitalitylearning2021 commented 1 year ago

Thank you very much. That closes the issue.