threepipe
Version:
A 3D viewer framework built on top of three.js in TypeScript with a focus on quality rendering, modularity and extensibility.
62 lines • 2.91 kB
JavaScript
import { LinearSRGBColorSpace, NoColorSpace, RGBAFormat, RGBM16ColorSpace, SRGBColorSpace, UnsignedByteType, } from 'three';
export function getEncodingComponents(colorSpace) {
switch (colorSpace) {
case NoColorSpace:
case LinearSRGBColorSpace:
return ['Linear', '( value )'];
case SRGBColorSpace:
return ['sRGB', '( value )'];
// case RGBEEncoding:
// return ['RGBE', '( value )']
// case RGBM7Encoding:
// return ['RGBM', '( value, 7.0 )']
case RGBM16ColorSpace:
return ['RGBM', '( value, 16.0 )'];
// case RGBDEncoding:
// return ['RGBD', '( value, 256.0 )']
// case GammaEncoding:
// return ['Gamma', '( value, float( GAMMA_FACTOR ) )']
// case LogLuvEncoding:
// return ['LogLuv', '( value )']
default:
console.warn('utils: Unsupported colorspace:', colorSpace);
return ['Linear', '( value )'];
}
}
export function getTextureColorSpaceFromMap(map, isWebGL2) {
let colorSpace;
if (map && map.colorSpace !== undefined) {
colorSpace = map.colorSpace || NoColorSpace;
}
else if (map && map.isWebGLRenderTarget) {
console.warn('THREE.WebGLPrograms.getTextureColorSpaceFromMap: don\'t use render targets as textures. Use their .texture property instead.');
colorSpace = map.texture.colorSpace;
}
else {
colorSpace = LinearSRGBColorSpace;
}
// See https://github.com/mrdoob/three.js/pull/22952
// todo: just check if srgb8 is enabled, instead of relying on threejs.
if (isWebGL2 && map && map.isTexture && map.format === RGBAFormat && map.type === UnsignedByteType && map.colorSpace === SRGBColorSpace) {
colorSpace = LinearSRGBColorSpace; // disable inline decode for sRGB textures in WebGL 2
}
return colorSpace;
}
export function getTexelDecodingFunction(functionName, colorSpace) {
const components = getEncodingComponents(colorSpace);
return 'vec4 ' + functionName + '( vec4 value ) { return ' + components[0] + 'ToLinear' + components[1] + '; }';
}
export function getTexelDecoding(mapName, map, isWebGL2) {
return getTexelDecodingFunction(mapName + 'TexelToLinear', getTextureColorSpaceFromMap(map, isWebGL2)) + '\n';
}
export function getTexelDecoding2(mapName, colorSpace) {
return getTexelDecodingFunction(mapName + 'TexelToLinear', colorSpace) + '\n';
}
export function getTexelEncodingFunction(functionName, colorSpace) {
const components = getEncodingComponents(colorSpace);
return 'vec4 ' + functionName + '( vec4 value ) { return LinearTo' + components[0] + components[1] + '; }';
}
export function getTexelEncoding(functionName, map, isWebGL2) {
return getTexelEncodingFunction(functionName, getTextureColorSpaceFromMap(map, isWebGL2));
}
//# sourceMappingURL=encoding.js.map