Skip to content

Commit

Permalink
chore: harden color management tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett committed Apr 18, 2023
1 parent adbea92 commit ccc52af
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
26 changes: 13 additions & 13 deletions packages/fiber/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,19 +339,6 @@ export function applyProps(instance: Instance, data: InstanceProps | DiffSet) {
key = 'outputColorSpace'
value = value === sRGBEncoding ? SRGBColorSpace : LinearSRGBColorSpace
}

// Auto-convert sRGB textures, for now ...
// https://github.com/pmndrs/react-three-fiber/issues/344
} else if (
!rootState.linear &&
currentInstance[key] instanceof THREE.Texture &&
// sRGB textures must be RGBA8 since r137 https://github.com/mrdoob/three.js/pull/23129
currentInstance[key].format === THREE.RGBAFormat &&
currentInstance[key].type === THREE.UnsignedByteType
) {
const texture = currentInstance[key] as THREE.Texture
if (hasColorSpace(texture) && hasColorSpace(rootState.gl)) texture.colorSpace = rootState.gl.outputColorSpace
else texture.encoding = rootState.gl.outputEncoding
}

// Deal with pointer events ...
Expand Down Expand Up @@ -394,6 +381,19 @@ export function applyProps(instance: Instance, data: InstanceProps | DiffSet) {
// Else, just overwrite the value
} else {
currentInstance[key] = value

// Auto-convert sRGB textures, for now ...
// https://github.com/pmndrs/react-three-fiber/issues/344
if (
currentInstance[key] instanceof THREE.Texture &&
// sRGB textures must be RGBA8 since r137 https://github.com/mrdoob/three.js/pull/23129
currentInstance[key].format === THREE.RGBAFormat &&
currentInstance[key].type === THREE.UnsignedByteType
) {
const texture = currentInstance[key] as THREE.Texture
if (hasColorSpace(texture) && hasColorSpace(rootState.gl)) texture.colorSpace = rootState.gl.outputColorSpace
else texture.encoding = rootState.gl.outputEncoding
}
}

invalidateInstance(instance)
Expand Down
21 changes: 15 additions & 6 deletions packages/fiber/tests/core/renderer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -740,31 +740,40 @@ describe('renderer', () => {
})

it('should respect color management preferences via gl', async () => {
const texture = new THREE.Texture() as THREE.Texture & { colorSpace?: string }
let key = 0
function Test() {
return <meshBasicMaterial key={key++} map={texture} />
}

const LinearEncoding = 3000
const sRGBEncoding = 3001

let gl: THREE.WebGLRenderer & { outputColorSpace?: string } = null!
await act(async () => (gl = root.render(<group />).getState().gl))
await act(async () => (gl = root.render(<Test />).getState().gl))
expect(gl.outputEncoding).toBe(sRGBEncoding)
expect(gl.toneMapping).toBe(THREE.ACESFilmicToneMapping)
expect(texture.encoding).toBe(sRGBEncoding)

await act(async () =>
root.configure({ gl: { outputEncoding: LinearEncoding, toneMapping: THREE.NoToneMapping } }).render(<group />),
)
await act(async () => root.configure({ linear: true, flat: true }).render(<Test />))
expect(gl.outputEncoding).toBe(LinearEncoding)
expect(gl.toneMapping).toBe(THREE.NoToneMapping)
expect(texture.encoding).toBe(LinearEncoding)

// Sets outputColorSpace since r152
const SRGBColorSpace = 'srgb'
const LinearSRGBColorSpace = 'srgb-linear'

gl.outputColorSpace ??= ''
texture.colorSpace ??= ''

await act(async () => root.configure({ linear: true }).render(<group />))
await act(async () => root.configure({ linear: true }).render(<Test />))
expect(gl.outputColorSpace).toBe(LinearSRGBColorSpace)
expect(texture.colorSpace).toBe(LinearSRGBColorSpace)

await act(async () => root.configure({ linear: false }).render(<group />))
await act(async () => root.configure({ linear: false }).render(<Test />))
expect(gl.outputColorSpace).toBe(SRGBColorSpace)
expect(texture.colorSpace).toBe(SRGBColorSpace)
})

it('should respect legacy prop', async () => {
Expand Down

0 comments on commit ccc52af

Please sign in to comment.