Skip to content

Commit

Permalink
fix: treeshake THREE.ColorManagement (#2798)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett authored Mar 6, 2023
1 parent e154260 commit 63e9da5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/fiber/src/core/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
useIsomorphicLayoutEffect,
Camera,
updateCamera,
ColorManagement,
getColorManagement,
} from './utils'
import { useStore } from './hooks'
import type { Properties } from '../three-types'
Expand Down Expand Up @@ -286,6 +286,7 @@ function createRoot<TCanvas extends Canvas>(canvas: TCanvas): ReconcilerRoot<TCa

// Safely set color management if available.
// Avoid accessing THREE.ColorManagement to play nice with older versions
const ColorManagement = getColorManagement()
if (ColorManagement) {
if ('enabled' in ColorManagement) ColorManagement.enabled = !legacy
else if ('legacyMode' in ColorManagement) ColorManagement.legacyMode = legacy
Expand Down
4 changes: 2 additions & 2 deletions packages/fiber/src/core/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ interface Catalogue {
}
}

let catalogue: Catalogue = {}
let extend = (objects: object): void => void (catalogue = { ...catalogue, ...objects })
export const catalogue: Catalogue = {}
const extend = (objects: object): void => void Object.assign(catalogue, objects)

function createRenderer<TCanvas>(_roots: Map<TCanvas, Root>, _getEventPriority?: () => any) {
function createInstance(
Expand Down
12 changes: 3 additions & 9 deletions packages/fiber/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ import * as THREE from 'three'
import * as React from 'react'
import { UseBoundStore } from 'zustand'
import { EventHandlers } from './events'
import { AttachType, Instance, InstanceProps, LocalState } from './renderer'
import { AttachType, catalogue, Instance, InstanceProps, LocalState } from './renderer'
import { Dpr, RootState, Size } from './store'

/**
* Safely accesses a deeply-nested value on an object to get around static bundler analysis.
*/
const getDeep = (obj: any, ...keys: string[]): any => keys.reduce((acc, key) => acc?.[key], obj)

export type ColorManagementRepresentation = { enabled: boolean | never } | { legacyMode: boolean | never }

/**
* The current THREE.ColorManagement instance, if present.
*/
export const ColorManagement: ColorManagementRepresentation | null =
('ColorManagement' in THREE && getDeep(THREE, 'ColorManagement')) || null
export const getColorManagement = (): ColorManagementRepresentation | null => (catalogue as any).ColorManagement ?? null

export type Camera = THREE.OrthographicCamera | THREE.PerspectiveCamera
export const isOrthographicCamera = (def: Camera): def is THREE.OrthographicCamera =>
Expand Down Expand Up @@ -356,7 +350,7 @@ export function applyProps(instance: Instance, data: InstanceProps | DiffSet) {
// For versions of three which don't support THREE.ColorManagement,
// Auto-convert sRGB colors
// https://github.com/pmndrs/react-three-fiber/issues/344
if (!ColorManagement && !rootState.linear && isColor) targetProp.convertSRGBToLinear()
if (!getColorManagement() && !rootState.linear && isColor) targetProp.convertSRGBToLinear()
}
// Else, just overwrite the value
} else {
Expand Down
8 changes: 8 additions & 0 deletions packages/fiber/tests/core/renderer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,14 @@ describe('renderer', () => {
})

it('should respect legacy prop', async () => {
// <= r138 internal fallback
const material = React.createRef<THREE.MeshBasicMaterial>()
extend({ ColorManagement: null })
await act(async () => root.render(<meshBasicMaterial ref={material} color="#111111" />))
expect((THREE as any).ColorManagement.legacyMode).toBe(false)
expect(material.current!.color.toArray()).toStrictEqual(new THREE.Color('#111111').convertSRGBToLinear().toArray())
extend({ ColorManagement: (THREE as any).ColorManagement })

// r139 legacyMode
await act(async () => {
root.configure({ legacy: true }).render(<group />)
Expand Down

0 comments on commit 63e9da5

Please sign in to comment.