Cubemap faces given as URLs load as RGBP textures
A cubemap asset whose data.textures lists face URLs, rather than texture asset ids, loads each face as an RGBP-encoded, linear texture. The cubemap assembled from those faces inherits the RGBP type:
const cubemap = new pc.Asset('sky', 'cubemap', null, {
textures: ['posx.png', 'negx.png', 'posy.png', 'negy.png', 'posz.png', 'negz.png']
});
cubemap.ready(() => {
console.log(cubemap.resource.type, cubemap.resource.srgb); // 'rgbp' false
});
app.assets.add(cubemap);
app.assets.load(cubemap);The same six images passed as texture asset ids give a cubemap of type default.
Where
What's wrong
CubemapHandler#loadAssets creates the texture assets for URL entries itself. It gives every non-.dds URL the texture state meant for a prefiltered env atlas, whichever of the seven slots it is loading:
// if the referenced prefiltered texture is not a dds file, then we're loading an
// envAtlas. In this case we must specify the correct texture state.
const data = file.url.search('.dds') === -1 ? {
type: 'rgbp',
addressu: 'clamp',
addressv: 'clamp',
mipmaps: false
} : null;Slots 1-6 are the faces, though. The faces cubemap then takes getType() || faceTextures[0].type (https://github.com/playcanvas/engine/blob/61d79c09788da1300ee4cfad727743235717f7ef/src/framework/handlers/cubemap.js#L172), so it becomes rgbp as well, unless the cubemap's own data sets type or rgbm.
Impact
Reproduced on the null device with image loading stubbed. Each face texture asset gets {"type":"rgbp","addressu":"clamp","addressv":"clamp","mipmaps":false}, each face texture has type rgbp and is not sRGB, and cubemap.resource has type rgbp and is not sRGB.
When rendered, the faces go through RGBP decoding (rgb * (8 - 7a), then squared) instead of an sRGB read. For opaque 8-bit images this is close to an sRGB decode, because squaring approximates the curve, but it isn't exact. Any alpha in a face scales its color by up to 8x. And the cubemap reports type: 'rgbp' for data that isn't RGBP.
This matters for #317. The cubemap JSON the Editor exports lists its faces as relative URLs, so loading it would take this path. Until this is fixed, the workaround is to load the faces as texture assets and pass their ids, as suggested on #2423.
Fix
Apply the env atlas state only to slot 0, the prefiltered file. Face URLs should load as ordinary color textures, with srgb: true a sensible default for 8-bit face images.
Source: playcanvas/engine