Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type error with ChromaticAberration #298

Open
Johnjackbogart opened this issue Oct 4, 2024 · 3 comments
Open

Type error with ChromaticAberration #298

Johnjackbogart opened this issue Oct 4, 2024 · 3 comments

Comments

@Johnjackbogart
Copy link

Johnjackbogart commented Oct 4, 2024

Hi,

I have the below code:

import { useRef } from "react";
import * as THREE from "three";
import { useFrame } from "@react-three/fiber";
import {
  Bloom,
  EffectComposer,
  N8AO,
  TiltShift2,
  ChromaticAberration,
} from "@react-three/postprocessing";
import { ChromaticAberrationEffect, BlendFunction } from "postprocessing";
import { easing } from "maath";

export default function Effects() {
  const chromaRef = useRef<ChromaticAberrationEffect>(null);

  useFrame((state, delta) => {
    easing.damp3(
      state.camera.position,
      [
        Math.sin(-state.pointer.x) * 5,
        state.pointer.y * 2,
        0.5 + Math.cos(state.pointer.x) * 5,
      ],
      0.1,
      delta,
    );
    state.camera.lookAt(0, 0, 0);

    if (!chromaRef.current) return;
    const x = Math.sin(-state.pointer.x) / 100;
    const y = state.pointer.y / 100;
    const newOffset = new THREE.Vector2(x, y);
    chromaRef.current.offset = newOffset;
  });
  return (
    <EffectComposer enableNormalPass={true}>
      <ChromaticAberration
        ref={chromaRef}
        blendFunction={BlendFunction.NORMAL}
        offset={new THREE.Vector2(0.01, 0.01)}
        radialModulation={false}
        modulationOffset={1.0}
      />
      <Bloom mipmapBlur luminanceThreshold={0.8} intensity={2} levels={8} />
      <TiltShift2 blur={0.2} />
    </EffectComposer>
  );
}

I'm trying to animate the chromatic aberration offset.

However, I get the following error:

Type 'RefObject<ChromaticAberrationEffect>' is not assignable to type 'RefObject<typeof ChromaticAberrationEffect>'.
     Property 'prototype' is missing in type 'ChromaticAberrationEffect' but required in type 'typeof ChromaticAberrationEffect'.

I believe this is because the type is exported as typeof ChromaticAberrationEffect in react-three/postprocessing

instead of just ChromaticAberrationEffect

When I changed this in my node modules folder the issue is resolved

@Johnjackbogart
Copy link
Author

I opened up a PR here:

#299

Not sure if this is the right way to do it tbh, curious if this solution makes sense.

To me, it doesn't make sense to export typeof ChromaticAberrationEffect, that just returns function

I'd expect you to want to export the full type, so this makes sense to me

@Johnjackbogart
Copy link
Author

The PR doesn't work. My goal is basically to animate the offset of chromatic aberration based on mouse position. But with strict type checking enabled, I can't

@Johnjackbogart
Copy link
Author

the solution I ended up going with was casting the type to unknown then to its expected type.

If I typed the ref I was using the same way <ChromaticAberrationEffect> expects then I can't access the offset in useFrame,

So it still seems like there is an issue here.

My PR doesn't build because of other TS issues, so working on that! Let me know if I'm on the right track, thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant