Skip to content

Commit

Permalink
LightProbeGenerator: Add support for half float render targets. (#26773)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Sep 18, 2023
1 parent e74c655 commit f4488d8
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions examples/jsm/lights/LightProbeGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
SphericalHarmonics3,
Vector3,
SRGBColorSpace,
NoColorSpace
NoColorSpace,
HalfFloatType,
DataUtils
} from 'three';

class LightProbeGenerator {
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit f4488d8

Please sign in to comment.