Home > front end >  Javascript / Passing the right optional argument
Javascript / Passing the right optional argument

Time:12-10

If have this object : WebGLCubeRenderTarget

Its constructor is :

constructor(size?: number, options?: WebGLRenderTargetOptions);

Among options I need to specify the encoding :

export interface WebGLRenderTargetOptions {
    wrapS?: Wrapping | undefined;
    wrapT?: Wrapping | undefined;
    encoding?: TextureEncoding | undefined;

Encoding is enum :

export enum TextureEncoding {}
export const LinearEncoding: TextureEncoding;
export const sRGBEncoding: TextureEncoding;

what should be the right syntax to pass sRGBEncoding ?

This one doesn't work :

const formatted = new THREE.WebGLCubeRenderTarget(texture.image.height, "sRGBEncoding")

Edit : Complete Code

const Background = props => {
    const texture = useLoader(THREE.TextureLoader, "/03milkyway.jpg")
    const { gl } = useThree();
    const formatted = new THREE.WebGLCubeRenderTarget(texture.image.height, "sRGBEncoding").fromEquirectangularTexture(gl, texture)
    return (
        <primitive attach='background' object={formatted} />
    )
}

CodePudding user response:

you have to pass an object as a second parameter:

const formatted = new THREE.WebGLCubeRenderTarget(texture.image.height, {encoding: "sRGBEncoding"})

CodePudding user response:

WebGLCubeRenderTarget's second param is supposed to be an object, you're passing it a string.

I believe from the docs that the value for the "encoding" option should also not be a string; see the "Encoding" section here.

Therefore try this:

const formatted = new THREE.WebGLCubeRenderTarget(
    texture.image.height, 
    {encoding: THREE.sRGBEncoding}
)
  • Related