From f4488d8b2318acd9bf8a9b644a28a2ca051d65b1 Mon Sep 17 00:00:00 2001 From: Michael Herzog Date: Mon, 18 Sep 2023 19:41:00 +0200 Subject: [PATCH] LightProbeGenerator: Add support for half float render targets. (#26773) --- examples/jsm/lights/LightProbeGenerator.js | 40 ++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/examples/jsm/lights/LightProbeGenerator.js b/examples/jsm/lights/LightProbeGenerator.js index 3fcc0a67e2c82d..4bd9896c8cd3fd 100644 --- a/examples/jsm/lights/LightProbeGenerator.js +++ b/examples/jsm/lights/LightProbeGenerator.js @@ -5,7 +5,9 @@ import { SphericalHarmonics3, Vector3, SRGBColorSpace, - NoColorSpace + NoColorSpace, + HalfFloatType, + DataUtils } from 'three'; class LightProbeGenerator { @@ -140,18 +142,50 @@ class LightProbeGenerator { const sh = new SphericalHarmonics3(); const shCoefficients = sh.coefficients; + const dataType = cubeRenderTarget.texture.type; + for ( let faceIndex = 0; faceIndex < 6; faceIndex ++ ) { const imageWidth = cubeRenderTarget.width; // assumed to be square - const data = new Uint8Array( imageWidth * imageWidth * 4 ); + + let data; + + if ( dataType === HalfFloatType ) { + + data = new Uint16Array( imageWidth * imageWidth * 4 ); + + } else { + + // assuming UnsignedByteType + + data = new Uint8Array( imageWidth * imageWidth * 4 ); + + } + renderer.readRenderTargetPixels( cubeRenderTarget, 0, 0, imageWidth, imageWidth, data, faceIndex ); const pixelSize = 2 / imageWidth; for ( let i = 0, il = data.length; i < il; i += 4 ) { // RGBA assumed + let r, g, b; + + if ( dataType === HalfFloatType ) { + + r = DataUtils.fromHalfFloat( data[ i ] ); + g = DataUtils.fromHalfFloat( data[ i + 1 ] ); + b = DataUtils.fromHalfFloat( data[ i + 2 ] ); + + } else { + + r = data[ i ] / 255; + g = data[ i + 1 ] / 255; + b = data[ i + 2 ] / 255; + + } + // pixel color - color.setRGB( data[ i ] / 255, data[ i + 1 ] / 255, data[ i + 2 ] / 255 ); + color.setRGB( r, g, b ); // convert to linear color space convertColorToLinear( color, cubeRenderTarget.texture.colorSpace );