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

Question: What is the internal basis of the "generatePixelAreaLookup" function #18

Closed
OliverXH opened this issue Jun 5, 2022 · 3 comments

Comments

@OliverXH
Copy link

OliverXH commented Jun 5, 2022

I have been reading the source code of this repo recently and have some questions about the function used to generate probePixelAreaLookup data. What is the internal basis of the generatePixelAreaLookup function?

export function generatePixelAreaLookup(probeTargetSize: number) {
  const probePixelCount = probeTargetSize * probeTargetSize;
  const lookup = new Array(probePixelCount) as number[];

  const probePixelBias = 0.5 / probeTargetSize;

  for (let py = 0; py < probeTargetSize; py += 1) {
    // compute offset from center (with a bias for target pixel size)
    const dy = py / probeTargetSize - 0.5 + probePixelBias;

    for (let px = 0; px < probeTargetSize; px += 1) {
      // compute offset from center (with a bias for target pixel size)
      const dx = px / probeTargetSize - 0.5 + probePixelBias;

      // compute multiplier as affected by inclination of corresponding ray
      const span = Math.hypot(dx * 2, dy * 2);
      const hypo = Math.hypot(span, 1);
      const area = 1 / hypo;

      lookup[py * probeTargetSize + px] = area;
    }
  }

  return lookup;
}

Is there any calculation basis?

@unframework
Copy link
Collaborator

Hi Oliver, this function computes the multiplier for each pixel of the light probe half-cube (well, of each side of the half-cube). The pixels near corners/edges of the cube are weighted less than center pixels in the resulting average. In other words, this is mapping cube pixels onto a sphere before combining them into a final sum. Actual mathematics here are very much approximated, I actually did not test this for physical precision or anything - the aim of the whole project is to be a quick convincing hack instead of a full mathematically correct solution. Hope this helps!

@OliverXH
Copy link
Author

OliverXH commented Jun 7, 2022

@unframework
Thank you for your answer
I drew the following figure based on the source code and your answer, which is very useful for me to understand how it works:
img

This also makes me understand the following program:

const area = probePixelAreaLookup[py * probeTargetSize + px];

r += area * probeData[i];
g += area * probeData[i + 1];
b += area * probeData[i + 2];

@unframework
Copy link
Collaborator

Yep that describes it nicely, thanks for the diagram!

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