Skip to content

Commit

Permalink
Move aggregation of intensities to main.ts #47
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianK13 committed Oct 25, 2024
1 parent 9d6b581 commit f56a704
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
13 changes: 7 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,15 @@ export class ShadingScene {
sun.shadeIrradianceFromElevation(irradiance, shadingElevationAngles);
}
normals = normals.filter((_, index) => index % 9 < 3);
let intensities = await rayTracingWebGL(midpoints, normals, meshArray, irradiance, progressCallback);

if (intensities === null) {
const shadedIrradianceScenes = await rayTracingWebGL(midpoints, normals, meshArray, irradiance, progressCallback);
if (shadedIrradianceScenes === null) {
throw new Error('Error occured when running the Raytracing in WebGL.');
}

for (let i = 0; i < intensities.length; i++) {
intensities[i] /= irradiance.length;
let intensities = shadedIrradianceScenes[0];
for (let i = 1; i < shadedIrradianceScenes.length; i++) {
for (let j = 0; j < intensities.length; j++) {
intensities[j] += shadedIrradianceScenes[i][j];
}
}

return intensities;
Expand Down
20 changes: 7 additions & 13 deletions src/rayTracingWebGL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function rayTracingWebGL(
trianglesArray: TypedArray,
irradiance: SunVector[],
progressCallback: (progress: number, total: number) => void,
): Promise<Float32Array | null> {
): Promise<Float32Array[] | null> {
const N_TRIANGLES = trianglesArray.length / 9;
const width = pointsArray.length / 3; // Change this to the number of horizontal points in the grid
const N_POINTS = width;
Expand Down Expand Up @@ -179,7 +179,9 @@ export async function rayTracingWebGL(
const normalBuffer = makeBufferAndSetAttribute(gl, normals, normalAttributeLocation);

var colorCodedArray = null;
var isShadowedArray: Float32Array | null = null;
var shadedIrradianceScenes: Float32Array[] = [];
// each element of this shadedIrradianceScenes represents the shading
// caused by one ray of the irradiance list

await timeoutForLoop(0, irradiance.length, (i) => {
if (!irradiance[i].isShadedByElevation) {
Expand All @@ -195,16 +197,8 @@ export async function rayTracingWebGL(

drawArraysWithTransformFeedback(gl, tf, gl.POINTS, N_POINTS);

if (isShadowedArray == null) {
colorCodedArray = getResults(gl, colorBuffer, N_POINTS);
isShadowedArray = colorCodedArray.filter((_, index) => (index + 1) % 4 === 0);
} else {
colorCodedArray = getResults(gl, colorBuffer, N_POINTS);
addToArray(
isShadowedArray,
colorCodedArray.filter((_, index) => (index + 1) % 4 === 0),
);
}
colorCodedArray = getResults(gl, colorBuffer, N_POINTS);
shadedIrradianceScenes[i] = colorCodedArray.filter((_, index) => (index + 1) % 4 === 0);
}
});
gl.deleteTexture(texture);
Expand All @@ -215,7 +209,7 @@ export async function rayTracingWebGL(
gl.deleteBuffer(normalBuffer);
gl.deleteTransformFeedback(tf);
gl.deleteBuffer(colorBuffer);
return isShadowedArray;
return shadedIrradianceScenes;
}

function getResults(gl: WebGL2RenderingContext, buffer: WebGLBuffer | null, N_POINTS: number) {
Expand Down

0 comments on commit f56a704

Please sign in to comment.