Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/effects/Texture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ import { useLoader } from '@react-three/fiber'
import { TextureEffect } from 'postprocessing'
import type { Ref } from 'react'
import { useLayoutEffect } from 'react'
import { RepeatWrapping, SRGBColorSpace, TextureLoader } from 'three'
import { RepeatWrapping, SRGBColorSpace, TextureLoader, type Texture as ThreeTexture } from 'three'
import { createEffectComponent, type EffectOptions } from '../createEffectComponent'

const TextureImpl = /* @__PURE__ */ createEffectComponent<typeof TextureEffect, EffectOptions<typeof TextureEffect>>(
TextureEffect
)

export type TextureProps = EffectOptions<typeof TextureEffect> & {
textureSrc: string
export type TextureProps = Omit<EffectOptions<typeof TextureEffect>, 'texture'> & {
/** opacity of provided texture */
opacity?: number
ref?: Ref<TextureEffect>
}
} & ({ textureSrc: string; texture?: never } | { texture: ThreeTexture; textureSrc?: never })

export function Texture({ textureSrc, texture, opacity = 1, ...props }: TextureProps) {
if (texture) return <TextureImpl {...props} texture={texture} opacity={opacity} />
return <LoadedTexture {...props} textureSrc={textureSrc!} opacity={opacity} />
}

function LoadedTexture({ textureSrc, ...props }: TextureProps & { textureSrc: string }) {
const t = useLoader(TextureLoader, textureSrc)

useLayoutEffect(() => {
t.colorSpace = SRGBColorSpace
t.wrapS = t.wrapT = RepeatWrapping
}, [t])

return <TextureImpl {...props} texture={texture ?? t} opacity={opacity} />
return <TextureImpl {...props} texture={t} />
}
57 changes: 57 additions & 0 deletions src/tests/Texture.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { TextureEffect } from 'postprocessing'
import * as React from 'react'
import * as THREE from 'three'
import { describe, expect, it, vi } from 'vitest'
import { EffectComposer } from '../EffectComposer'
import { Texture } from '../effects/Texture'
import { flush, root } from './test-utils'

describe('Texture', () => {
it('uses a passed texture without loading anything', async () => {
const loadSpy = vi.spyOn(THREE.TextureLoader.prototype, 'load')
const ref = React.createRef<TextureEffect>()
const texture = new THREE.Texture()

await React.act(async () =>
root.render(
<EffectComposer>
<Texture ref={ref} texture={texture} />
</EffectComposer>
)
)
await flush()

expect(loadSpy).not.toHaveBeenCalled()
expect(ref.current!.texture).toBe(texture)
// A user-provided texture is not mutated
expect(texture.colorSpace).toBe(THREE.NoColorSpace)

await React.act(async () => root.render(null))
loadSpy.mockRestore()
})

it('swaps a passed texture live, without reconstructing the effect', async () => {
const ref = React.createRef<TextureEffect>()
const a = new THREE.Texture()
const b = new THREE.Texture()

const render = (texture: THREE.Texture) =>
root.render(
<EffectComposer>
<Texture ref={ref} texture={texture} />
</EffectComposer>
)

await React.act(async () => render(a))
await flush()
const first = ref.current

await React.act(async () => render(b))
await flush()

expect(ref.current).toBe(first)
expect(ref.current!.texture).toBe(b)

await React.act(async () => root.render(null))
})
})
8 changes: 6 additions & 2 deletions src/tests/effects.smoke.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { Sepia } from '../effects/Sepia'
import { ShockWave } from '../effects/ShockWave'
import { SMAA } from '../effects/SMAA'
import { SSAO } from '../effects/SSAO'
import { Texture } from '../effects/Texture'
import { TiltShift } from '../effects/TiltShift'
import { TiltShift2 } from '../effects/TiltShift2'
import { ToneMapping } from '../effects/ToneMapping'
Expand Down Expand Up @@ -125,6 +126,11 @@ const SMOKE_CASES: SmokeCase[] = [
composerProps: { enableNormalPass: true },
effect: (ref) => <SSAO ref={ref} />,
},
{
file: 'Texture.tsx',
label: 'Texture (with a passed texture)',
effect: (ref) => <Texture ref={ref} texture={new THREE.Texture()} />,
},
{ file: 'TiltShift.tsx', label: 'TiltShift', effect: (ref) => <TiltShift ref={ref} /> },
{ file: 'TiltShift2.tsx', label: 'TiltShift2', effect: (ref) => <TiltShift2 ref={ref} /> },
{ file: 'ToneMapping.tsx', label: 'ToneMapping', effect: (ref) => <ToneMapping ref={ref} /> },
Expand All @@ -133,8 +139,6 @@ const SMOKE_CASES: SmokeCase[] = [
]

const EXCLUDED: Record<string, string> = {
'Texture.tsx':
'loads via useLoader(TextureLoader, textureSrc) — needs a real image decode pipeline this test environment cannot provide. Verify manually.',
'ASCII.tsx':
"constructs its character atlas via document.createElement('canvas') — this project's vitest config runs the node test environment (no jsdom/document). Verify manually.",
}
Expand Down
Loading