Skip to content

feat: optimize dither backgrounds #332

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

Merged
merged 1 commit into from
Jul 23, 2025
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
61 changes: 41 additions & 20 deletions src/content/Backgrounds/Dither/Dither.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable react/no-unknown-property */
import { useRef, useState, useEffect, forwardRef } from "react";
import { useRef, useEffect, forwardRef } from "react";
import { Canvas, useFrame, useThree } from "@react-three/fiber";
import { EffectComposer, wrapEffect } from "@react-three/postprocessing";
import { Effect } from "postprocessing";
import * as THREE from "three";

import './Dither.css';
import "./Dither.css";

const waveVertexShader = `
precision highp float;
Expand Down Expand Up @@ -63,7 +63,7 @@ float cnoise(vec2 P) {
return 2.3 * mix(n_x.x, n_x.y, fade_xy.y);
}

const int OCTAVES = 8;
const int OCTAVES = 4;
float fbm(vec2 p) {
float value = 0.0;
float amp = 1.0;
Expand All @@ -78,7 +78,7 @@ float fbm(vec2 p) {

float pattern(vec2 p) {
vec2 p2 = p - time * waveSpeed;
return fbm(p - fbm(p + fbm(p2)));
return fbm(p + fbm(p2));
}

void main() {
Expand Down Expand Up @@ -143,10 +143,18 @@ class RetroEffectImpl extends Effect {
super("RetroEffect", ditherFragmentShader, { uniforms });
this.uniforms = uniforms;
}
set colorNum(v) { this.uniforms.get("colorNum").value = v; }
get colorNum() { return this.uniforms.get("colorNum").value; }
set pixelSize(v) { this.uniforms.get("pixelSize").value = v; }
get pixelSize() { return this.uniforms.get("pixelSize").value; }
set colorNum(v) {
this.uniforms.get("colorNum").value = v;
}
get colorNum() {
return this.uniforms.get("colorNum").value;
}
set pixelSize(v) {
this.uniforms.get("pixelSize").value = v;
}
get pixelSize() {
return this.uniforms.get("pixelSize").value;
}
}

const WrappedRetro = wrapEffect(RetroEffectImpl);
Expand All @@ -169,7 +177,7 @@ function DitheredWaves({
mouseRadius,
}) {
const mesh = useRef(null);
const [mousePos, setMousePos] = useState({ x: 0, y: 0 });
const mouseRef = useRef(new THREE.Vector2());
const { viewport, size, gl } = useThree();

const waveUniformsRef = useRef({
Expand All @@ -194,28 +202,41 @@ function DitheredWaves({
}
}, [size, gl]);

const prevColor = useRef([...waveColor]);
useFrame(({ clock }) => {
const u = waveUniformsRef.current;
if (!disableAnimation) u.time.value = clock.getElapsedTime();
u.waveSpeed.value = waveSpeed;
u.waveFrequency.value = waveFrequency;
u.waveAmplitude.value = waveAmplitude;
u.waveColor.value.set(...waveColor);

if (!disableAnimation) {
u.time.value = clock.getElapsedTime();
}

if (u.waveSpeed.value !== waveSpeed) u.waveSpeed.value = waveSpeed;
if (u.waveFrequency.value !== waveFrequency)
u.waveFrequency.value = waveFrequency;
if (u.waveAmplitude.value !== waveAmplitude)
u.waveAmplitude.value = waveAmplitude;

if (!prevColor.current.every((v, i) => v === waveColor[i])) {
u.waveColor.value.set(...waveColor);
prevColor.current = [...waveColor];
}

u.enableMouseInteraction.value = enableMouseInteraction ? 1 : 0;
u.mouseRadius.value = mouseRadius;

if (enableMouseInteraction) {
u.mousePos.value.set(mousePos.x, mousePos.y);
u.mousePos.value.copy(mouseRef.current);
}
});

const handlePointerMove = (e) => {
if (!enableMouseInteraction) return;
const rect = gl.domElement.getBoundingClientRect();
const dpr = gl.getPixelRatio();
setMousePos({
x: (e.clientX - rect.left) * dpr,
y: (e.clientY - rect.top) * dpr,
});
mouseRef.current.set(
(e.clientX - rect.left) * dpr,
(e.clientY - rect.top) * dpr
);
};

return (
Expand Down Expand Up @@ -277,4 +298,4 @@ export default function Dither({
/>
</Canvas>
);
}
}
59 changes: 40 additions & 19 deletions src/tailwind/Backgrounds/Dither/Dither.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable react/no-unknown-property */
import { useRef, useState, useEffect, forwardRef } from "react";
import { useRef, useEffect, forwardRef } from "react";
import { Canvas, useFrame, useThree } from "@react-three/fiber";
import { EffectComposer, wrapEffect } from "@react-three/postprocessing";
import { Effect } from "postprocessing";
Expand Down Expand Up @@ -61,7 +61,7 @@ float cnoise(vec2 P) {
return 2.3 * mix(n_x.x, n_x.y, fade_xy.y);
}

const int OCTAVES = 8;
const int OCTAVES = 4;
float fbm(vec2 p) {
float value = 0.0;
float amp = 1.0;
Expand All @@ -76,7 +76,7 @@ float fbm(vec2 p) {

float pattern(vec2 p) {
vec2 p2 = p - time * waveSpeed;
return fbm(p - fbm(p + fbm(p2)));
return fbm(p + fbm(p2));
}

void main() {
Expand Down Expand Up @@ -141,10 +141,18 @@ class RetroEffectImpl extends Effect {
super("RetroEffect", ditherFragmentShader, { uniforms });
this.uniforms = uniforms;
}
set colorNum(v) { this.uniforms.get("colorNum").value = v; }
get colorNum() { return this.uniforms.get("colorNum").value; }
set pixelSize(v) { this.uniforms.get("pixelSize").value = v; }
get pixelSize() { return this.uniforms.get("pixelSize").value; }
set colorNum(v) {
this.uniforms.get("colorNum").value = v;
}
get colorNum() {
return this.uniforms.get("colorNum").value;
}
set pixelSize(v) {
this.uniforms.get("pixelSize").value = v;
}
get pixelSize() {
return this.uniforms.get("pixelSize").value;
}
}

const WrappedRetro = wrapEffect(RetroEffectImpl);
Expand All @@ -167,7 +175,7 @@ function DitheredWaves({
mouseRadius,
}) {
const mesh = useRef(null);
const [mousePos, setMousePos] = useState({ x: 0, y: 0 });
const mouseRef = useRef(new THREE.Vector2());
const { viewport, size, gl } = useThree();

const waveUniformsRef = useRef({
Expand All @@ -192,28 +200,41 @@ function DitheredWaves({
}
}, [size, gl]);

const prevColor = useRef([...waveColor]);
useFrame(({ clock }) => {
const u = waveUniformsRef.current;
if (!disableAnimation) u.time.value = clock.getElapsedTime();
u.waveSpeed.value = waveSpeed;
u.waveFrequency.value = waveFrequency;
u.waveAmplitude.value = waveAmplitude;
u.waveColor.value.set(...waveColor);

if (!disableAnimation) {
u.time.value = clock.getElapsedTime();
}

if (u.waveSpeed.value !== waveSpeed) u.waveSpeed.value = waveSpeed;
if (u.waveFrequency.value !== waveFrequency)
u.waveFrequency.value = waveFrequency;
if (u.waveAmplitude.value !== waveAmplitude)
u.waveAmplitude.value = waveAmplitude;

if (!prevColor.current.every((v, i) => v === waveColor[i])) {
u.waveColor.value.set(...waveColor);
prevColor.current = [...waveColor];
}

u.enableMouseInteraction.value = enableMouseInteraction ? 1 : 0;
u.mouseRadius.value = mouseRadius;

if (enableMouseInteraction) {
u.mousePos.value.set(mousePos.x, mousePos.y);
u.mousePos.value.copy(mouseRef.current);
}
});

const handlePointerMove = (e) => {
if (!enableMouseInteraction) return;
const rect = gl.domElement.getBoundingClientRect();
const dpr = gl.getPixelRatio();
setMousePos({
x: (e.clientX - rect.left) * dpr,
y: (e.clientY - rect.top) * dpr,
});
mouseRef.current.set(
(e.clientX - rect.left) * dpr,
(e.clientY - rect.top) * dpr
);
};

return (
Expand Down Expand Up @@ -275,4 +296,4 @@ export default function Dither({
/>
</Canvas>
);
}
}
68 changes: 39 additions & 29 deletions src/ts-default/Backgrounds/Dither/Dither.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable react/no-unknown-property */
import { useRef, useState, useEffect } from "react";
import { useRef, useEffect, forwardRef } from "react";
import { Canvas, useFrame, useThree, ThreeEvent } from "@react-three/fiber";
import { EffectComposer, wrapEffect } from "@react-three/postprocessing";
import { Effect } from "postprocessing";
Expand Down Expand Up @@ -63,7 +63,7 @@ float cnoise(vec2 P) {
return 2.3 * mix(n_x.x, n_x.y, fade_xy.y);
}

const int OCTAVES = 8;
const int OCTAVES = 4;
float fbm(vec2 p) {
float value = 0.0;
float amp = 1.0;
Expand All @@ -78,7 +78,7 @@ float fbm(vec2 p) {

float pattern(vec2 p) {
vec2 p2 = p - time * waveSpeed;
return fbm(p - fbm(p + fbm(p2)));
return fbm(p + fbm(p2));
}

void main() {
Expand Down Expand Up @@ -158,17 +158,18 @@ class RetroEffectImpl extends Effect {
}
}

import { forwardRef } from 'react';

const RetroEffect = forwardRef<RetroEffectImpl, { colorNum: number; pixelSize: number }>(
(props, ref) => {
const { colorNum, pixelSize } = props;
const WrappedRetroEffect = wrapEffect(RetroEffectImpl);
return <WrappedRetroEffect ref={ref} colorNum={colorNum} pixelSize={pixelSize} />;
}
);
const RetroEffect = forwardRef<
RetroEffectImpl,
{ colorNum: number; pixelSize: number }
>((props, ref) => {
const { colorNum, pixelSize } = props;
const WrappedRetroEffect = wrapEffect(RetroEffectImpl);
return (
<WrappedRetroEffect ref={ref} colorNum={colorNum} pixelSize={pixelSize} />
);
});

RetroEffect.displayName = 'RetroEffect';
RetroEffect.displayName = "RetroEffect";

interface WaveUniforms {
[key: string]: THREE.Uniform<any>;
Expand Down Expand Up @@ -207,10 +208,7 @@ function DitheredWaves({
mouseRadius,
}: DitheredWavesProps) {
const mesh = useRef<THREE.Mesh>(null);
const [mousePos, setMousePos] = useState<{ x: number; y: number }>({
x: 0,
y: 0,
});
const mouseRef = useRef(new THREE.Vector2());
const { viewport, size, gl } = useThree();

const waveUniformsRef = useRef<WaveUniforms>({
Expand All @@ -235,29 +233,41 @@ function DitheredWaves({
}
}, [size, gl]);

const prevColor = useRef([...waveColor]);
useFrame(({ clock }) => {
const u = waveUniformsRef.current;

if (!disableAnimation) {
waveUniformsRef.current.time.value = clock.getElapsedTime();
u.time.value = clock.getElapsedTime();
}

if (u.waveSpeed.value !== waveSpeed) u.waveSpeed.value = waveSpeed;
if (u.waveFrequency.value !== waveFrequency)
u.waveFrequency.value = waveFrequency;
if (u.waveAmplitude.value !== waveAmplitude)
u.waveAmplitude.value = waveAmplitude;

if (!prevColor.current.every((v, i) => v === waveColor[i])) {
u.waveColor.value.set(...waveColor);
prevColor.current = [...waveColor];
}
waveUniformsRef.current.waveSpeed.value = waveSpeed;
waveUniformsRef.current.waveFrequency.value = waveFrequency;
waveUniformsRef.current.waveAmplitude.value = waveAmplitude;
waveUniformsRef.current.waveColor.value.set(...waveColor);
waveUniformsRef.current.enableMouseInteraction.value =
enableMouseInteraction ? 1 : 0;
waveUniformsRef.current.mouseRadius.value = mouseRadius;

u.enableMouseInteraction.value = enableMouseInteraction ? 1 : 0;
u.mouseRadius.value = mouseRadius;

if (enableMouseInteraction) {
waveUniformsRef.current.mousePos.value.set(mousePos.x, mousePos.y);
u.mousePos.value.copy(mouseRef.current);
}
});

const handlePointerMove = (e: ThreeEvent<PointerEvent>) => {
if (!enableMouseInteraction) return;
const rect = gl.domElement.getBoundingClientRect();
const dpr = gl.getPixelRatio();
const x = (e.clientX - rect.left) * dpr;
const y = (e.clientY - rect.top) * dpr;
setMousePos({ x, y });
mouseRef.current.set(
(e.clientX - rect.left) * dpr,
(e.clientY - rect.top) * dpr
);
};

return (
Expand Down
Loading