|
| 1 | +import * as React from 'react' |
| 2 | +import * as THREE from 'three' |
| 3 | +import { vi, describe, it, expect } from 'vitest' |
| 4 | +import { extend, createRoot, act } from '@react-three/fiber' |
| 5 | +import { EffectComposer } from './EffectComposer' |
| 6 | +import { EffectComposer as EffectComposerImpl, RenderPass, Pass, Effect, EffectPass } from 'postprocessing' |
| 7 | + |
| 8 | +// Let React know that we'll be testing effectful components |
| 9 | +declare global { |
| 10 | + var IS_REACT_ACT_ENVIRONMENT: boolean |
| 11 | +} |
| 12 | +global.IS_REACT_ACT_ENVIRONMENT = true |
| 13 | + |
| 14 | +// Mock scheduler to test React features |
| 15 | +vi.mock('scheduler', () => require('scheduler/unstable_mock')) |
| 16 | + |
| 17 | +// Create virtual R3F root for testing |
| 18 | +extend(THREE) |
| 19 | +const root = createRoot({ |
| 20 | + style: {} as CSSStyleDeclaration, |
| 21 | + addEventListener: (() => {}) as any, |
| 22 | + removeEventListener: (() => {}) as any, |
| 23 | + width: 1280, |
| 24 | + height: 800, |
| 25 | + clientWidth: 1280, |
| 26 | + clientHeight: 800, |
| 27 | + getContext: (() => |
| 28 | + new Proxy( |
| 29 | + {}, |
| 30 | + { |
| 31 | + get(_target, prop) { |
| 32 | + switch (prop) { |
| 33 | + case 'getParameter': |
| 34 | + return () => 'WebGL 2' // GL_VERSION |
| 35 | + case 'getExtension': |
| 36 | + return () => ({}) // EXT_blend_minmax |
| 37 | + case 'getContextAttributes': |
| 38 | + return () => ({ alpha: true }) |
| 39 | + case 'getShaderPrecisionFormat': |
| 40 | + return () => ({ rangeMin: 1, rangeMax: 1, precision: 1 }) |
| 41 | + default: |
| 42 | + return () => {} |
| 43 | + } |
| 44 | + }, |
| 45 | + } |
| 46 | + )) as any, |
| 47 | +} satisfies Partial<HTMLCanvasElement> as HTMLCanvasElement) |
| 48 | +root.configure({ frameloop: 'never' }) |
| 49 | + |
| 50 | +const EFFECT_SHADER = 'mainImage() {}' |
| 51 | + |
| 52 | +describe('EffectComposer', () => { |
| 53 | + it('should merge effects together', async () => { |
| 54 | + const composerRef = React.createRef<EffectComposerImpl>() |
| 55 | + |
| 56 | + const effectA = new Effect('A', EFFECT_SHADER) |
| 57 | + const effectB = new Effect('B', EFFECT_SHADER) |
| 58 | + const effectC = new Effect('C', EFFECT_SHADER) |
| 59 | + const passA = new Pass() |
| 60 | + const passB = new Pass() |
| 61 | + |
| 62 | + // Forward order |
| 63 | + await act(async () => |
| 64 | + root.render( |
| 65 | + <EffectComposer ref={composerRef}> |
| 66 | + {/* EffectPass(effectA, effectB) */} |
| 67 | + <primitive object={effectA} /> |
| 68 | + <primitive object={effectB} /> |
| 69 | + {/* PassA */} |
| 70 | + <primitive object={passA} /> |
| 71 | + {/* EffectPass(effectC) */} |
| 72 | + <primitive object={effectC} /> |
| 73 | + {/* PassB */} |
| 74 | + <primitive object={passB} /> |
| 75 | + </EffectComposer> |
| 76 | + ) |
| 77 | + ) |
| 78 | + expect(composerRef.current!.passes.map((p) => p.constructor)).toStrictEqual([ |
| 79 | + RenderPass, |
| 80 | + EffectPass, |
| 81 | + Pass, |
| 82 | + EffectPass, |
| 83 | + Pass, |
| 84 | + ]) |
| 85 | + // @ts-expect-error |
| 86 | + expect((composerRef.current!.passes[1] as EffectPass).effects).toStrictEqual([effectA, effectB]) |
| 87 | + expect(composerRef.current!.passes[2]).toBe(passA) |
| 88 | + // @ts-expect-error |
| 89 | + expect((composerRef.current!.passes[3] as EffectPass).effects).toStrictEqual([effectC]) |
| 90 | + expect(composerRef.current!.passes[4]).toBe(passB) |
| 91 | + |
| 92 | + // NOTE: instance children ordering is unstable until R3F v9, so we remount from scratch |
| 93 | + await act(async () => root.render(null)) |
| 94 | + |
| 95 | + // Reverse order |
| 96 | + await act(async () => |
| 97 | + root.render( |
| 98 | + <EffectComposer ref={composerRef}> |
| 99 | + {/* PassB */} |
| 100 | + <primitive object={passB} /> |
| 101 | + {/* EffectPass(effectC) */} |
| 102 | + <primitive object={effectC} /> |
| 103 | + {/* PassA */} |
| 104 | + <primitive object={passA} /> |
| 105 | + {/* EffectPass(effectB, effectA) */} |
| 106 | + <primitive object={effectB} /> |
| 107 | + <primitive object={effectA} /> |
| 108 | + </EffectComposer> |
| 109 | + ) |
| 110 | + ) |
| 111 | + expect(composerRef.current!.passes.map((p) => p.constructor)).toStrictEqual([ |
| 112 | + RenderPass, |
| 113 | + Pass, |
| 114 | + EffectPass, |
| 115 | + Pass, |
| 116 | + EffectPass, |
| 117 | + ]) |
| 118 | + expect(composerRef.current!.passes[1]).toBe(passB) |
| 119 | + // @ts-expect-error |
| 120 | + expect((composerRef.current!.passes[2] as EffectPass).effects).toStrictEqual([effectC]) |
| 121 | + expect(composerRef.current!.passes[3]).toBe(passA) |
| 122 | + // @ts-expect-error |
| 123 | + expect((composerRef.current!.passes[4] as EffectPass).effects).toStrictEqual([effectB, effectA]) |
| 124 | + }) |
| 125 | + |
| 126 | + it.skip('should split convolution effects', async () => { |
| 127 | + await act(async () => root.render(null)) |
| 128 | + }) |
| 129 | +}) |
0 commit comments