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

Add progress bar callback function #7 #8

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ export default class Scene {
* @param numberSimulations Number of random sun positions that are used to calculate the PV yield
* @returns
*/
async calculate(numberSimulations: number = 80) {
async calculate(
numberSimulations: number = 80,
progressCallback: (progress: number, total: number) => void = (progress, total) =>
console.log(`Progress: ${progress}/${total}%`),
) {
console.log('Simulation package was called to calculate');
let simulationGeometry = BufferGeometryUtils.mergeGeometries(this.simulationGeometries);
let shadingGeometry = BufferGeometryUtils.mergeGeometries(this.shadingGeometries);
Expand Down Expand Up @@ -130,7 +134,7 @@ export default class Scene {
}
}
// Compute unique intensities
const intensities = await this.rayTrace(midpointsArray, normalsArray, meshArray, numberSimulations);
const intensities = await this.rayTrace(midpointsArray, normalsArray, meshArray, numberSimulations, progressCallback);

if (intensities === null) {
throw new Error('Error raytracing in WebGL.');
Expand Down Expand Up @@ -186,8 +190,14 @@ export default class Scene {
* @return
* @memberof Scene
*/
async rayTrace(midpoints: Float32Array, normals: TypedArray, meshArray: Float32Array, numberSimulations: number) {
async rayTrace(
midpoints: Float32Array,
normals: TypedArray,
meshArray: Float32Array,
numberSimulations: number,
progressCallback: (progress: number, total: number) => void,
) {
let sunDirections = getRandomSunVectors(numberSimulations, this.latitude, this.longitude);
return rayTracingWebGL(midpoints, normals, meshArray, sunDirections);
return rayTracingWebGL(midpoints, normals, meshArray, sunDirections, progressCallback);
}
}
3 changes: 2 additions & 1 deletion src/rayTracingWebGL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function rayTracingWebGL(
normals: TypedArray,
trianglesArray: TypedArray,
sunDirections: Float32Array,
progressCallback: (progress: number, total: number) => void,
): Float32Array | null {
const N_TRIANGLES = trianglesArray.length / 9;
const width = pointsArray.length / 3; // Change this to the number of horizontal points in the grid
Expand Down Expand Up @@ -179,7 +180,7 @@ export function rayTracingWebGL(
var colorCodedArray = null;
var isShadowedArray = null;
for (var i = 0; i < sunDirections.length; i += 3) {
console.log('Simulating sun position #', i / 3, '/', sunDirections.length / 3);
progressCallback(i/3, sunDirections.length/3);
// TODO: Iterate over sunDirection
let sunDirectionUniformLocation = gl.getUniformLocation(program, 'u_sun_direction');
gl.uniform3fv(sunDirectionUniformLocation, [sunDirections[i], sunDirections[i + 1], sunDirections[i + 2]]);
Expand Down