Skip to content

Commit f4488d8

Browse files
authored
LightProbeGenerator: Add support for half float render targets. (#26773)
1 parent e74c655 commit f4488d8

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

examples/jsm/lights/LightProbeGenerator.js

+37-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import {
55
SphericalHarmonics3,
66
Vector3,
77
SRGBColorSpace,
8-
NoColorSpace
8+
NoColorSpace,
9+
HalfFloatType,
10+
DataUtils
911
} from 'three';
1012

1113
class LightProbeGenerator {
@@ -140,18 +142,50 @@ class LightProbeGenerator {
140142
const sh = new SphericalHarmonics3();
141143
const shCoefficients = sh.coefficients;
142144

145+
const dataType = cubeRenderTarget.texture.type;
146+
143147
for ( let faceIndex = 0; faceIndex < 6; faceIndex ++ ) {
144148

145149
const imageWidth = cubeRenderTarget.width; // assumed to be square
146-
const data = new Uint8Array( imageWidth * imageWidth * 4 );
150+
151+
let data;
152+
153+
if ( dataType === HalfFloatType ) {
154+
155+
data = new Uint16Array( imageWidth * imageWidth * 4 );
156+
157+
} else {
158+
159+
// assuming UnsignedByteType
160+
161+
data = new Uint8Array( imageWidth * imageWidth * 4 );
162+
163+
}
164+
147165
renderer.readRenderTargetPixels( cubeRenderTarget, 0, 0, imageWidth, imageWidth, data, faceIndex );
148166

149167
const pixelSize = 2 / imageWidth;
150168

151169
for ( let i = 0, il = data.length; i < il; i += 4 ) { // RGBA assumed
152170

171+
let r, g, b;
172+
173+
if ( dataType === HalfFloatType ) {
174+
175+
r = DataUtils.fromHalfFloat( data[ i ] );
176+
g = DataUtils.fromHalfFloat( data[ i + 1 ] );
177+
b = DataUtils.fromHalfFloat( data[ i + 2 ] );
178+
179+
} else {
180+
181+
r = data[ i ] / 255;
182+
g = data[ i + 1 ] / 255;
183+
b = data[ i + 2 ] / 255;
184+
185+
}
186+
153187
// pixel color
154-
color.setRGB( data[ i ] / 255, data[ i + 1 ] / 255, data[ i + 2 ] / 255 );
188+
color.setRGB( r, g, b );
155189

156190
// convert to linear color space
157191
convertColorToLinear( color, cubeRenderTarget.texture.colorSpace );

0 commit comments

Comments
 (0)