简体   繁体   中英

Javascript / Passing the right optional argument

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} />
    )
}

you have to pass an object as a second parameter:

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

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}
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM