Hi everyone! Could anyone give me advice on how to get the values of material.lightMap in order to calculate the average value on one side. The task is related to determining the illumination of solar panels. The code uses ProgressiveLightMap, thanks to a tip from this topic:
https://discourse.threejs.org/t/need-an-advise-how-to-make-a-heatmap-from-a-light-source/85305/7
I have asked Three.js Mentor on ChatGPT and it suggest this approach. Read from RenderTarget returned from PLM.progressiveLightMap2 and read in PixelBuffer, but cant make it work good. It return small same values for all panels:
function estimatePanelIllum(panelMesh, renderTarget) {
const uv2 = panelMesh.geometry.attributes.uv2;if (!uv2) return 0;
const width = renderTarget.width;
const height = renderTarget.height;
// const pixelBuffer = new Uint8Array(width * height * 4);
const pixelBuffer = new Float32Array(width * height * 4);
const renderer = rendererRef.current;
renderer.readRenderTargetPixels(renderTarget, 0, 0, width, height, pixelBuffer);
let sum = 0;let count = 0;
for (let i = 0; i < uv2.count; i++) {
const u = uv2.getX(i);
const v = uv2.getY(i);
if (u < 0 || u > 1 || v < 0 || v > 1) continue;
let x = Math.floor(u * width);
let y = Math.floor((1 - v) * height);
x = Math.min(Math.max(x, 0), width - 1);
y = Math.min(Math.max(y, 0), height - 1);
const idx = (y * width + x) * 4;
if (idx < 0 || idx + 2 >= pixelBuffer.length) continue;
const r = pixelBuffer[idx];
const g = pixelBuffer[idx + 1];
const b = pixelBuffer[idx + 2];
const brightness = (r + g + b) / 3;
sum += brightness;
count++;
}
return count > 0 ? sum / count : 0;
}