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

Extend Texture Effect to support opacity #271

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
11 changes: 10 additions & 1 deletion src/effects/Texture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { TextureLoader, RepeatWrapping } from 'three'

type TextureProps = ConstructorParameters<typeof TextureEffect>[0] & {
textureSrc: string
/** opacity of provided texture */
opacity?: number
}

export const Texture = forwardRef<TextureEffect, TextureProps>(function Texture(
{ textureSrc, texture, ...props }: TextureProps,
{ textureSrc, texture, opacity, ...props }: TextureProps,
ref: Ref<TextureEffect>
) {
const t = useLoader(TextureLoader, textureSrc)
Expand All @@ -20,5 +22,12 @@ export const Texture = forwardRef<TextureEffect, TextureProps>(function Texture(
t.wrapS = t.wrapT = RepeatWrapping
}, [t])
const effect = useMemo(() => new TextureEffect({ ...props, texture: t || texture }), [props, t, texture])
const effect = useMemo(() => {
let tEffect = new TextureEffect({ ...props, texture: t || texture });
if (typeof opacity === "number") {
tEffect.blendMode.opacity.value = opacity;
}
return tEffect;
}, [props, t, texture, opacity])
Copy link
Member

@CodyJasonBennett CodyJasonBennett Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer setting this as a prop instead so we don't recreate the effect and its resources when opacity changes (e.g. <primitive object={effect} blendMode-opacity-value={opacity ?? 1} />).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, I didn't know about such possibility. I'd implement that way and commit it then

return <primitive ref={ref} object={effect} dispose={null} />
})
Loading