Skip to content

Commit b56f256

Browse files
committed
Refactor wireframe object creation and animation logic in SynthwaveBackground component; implement staggered loading of objects and optimize color update mechanism for improved performance. Adjust WebGLRenderer settings for better rendering quality.
1 parent a68e7eb commit b56f256

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

app/components/SynthwaveBackground/objects/WireframeObjects.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
LineBasicMaterial,
1010
Line,
1111
LineLoop,
12-
Scene
12+
Scene,
1313
} from '~/utils/three-lite'
1414
import { COLORS } from '../config'
1515

@@ -182,11 +182,19 @@ export function createWireframeObjects(scene: Scene) {
182182

183183
const objectCount = 15
184184

185-
for (let i = 0; i < objectCount; i++) {
185+
for (let i = 0; i < 5; i++) {
186186
const lineObject = createLineObject(i)
187187
objectGroup.add(lineObject)
188188
}
189189

190190
scene.add(objectGroup)
191+
192+
setTimeout(() => {
193+
for (let i = 5; i < objectCount; i++) {
194+
const lineObject = createLineObject(i)
195+
objectGroup.add(lineObject)
196+
}
197+
}, 0)
198+
191199
return objectGroup
192200
}

app/components/SynthwaveBackground/utils/animate.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,43 @@ import {
77
Line,
88
LineLoop,
99
LineBasicMaterial,
10-
Color
10+
Color,
1111
} from '~/utils/three-lite'
12-
import type { MutableRefObject } from 'react'
1312
import { COLORS } from '../config'
1413

14+
const PI_2 = Math.PI * 2
15+
const DELTA_TIME = 0.016
16+
1517
export function animateScene(
1618
renderer: WebGLRenderer,
1719
scene: Scene,
1820
camera: PerspectiveCamera,
1921
objects: {
2022
wireframeObjects?: Group
2123
},
22-
animationFrameRef: MutableRefObject<number | null>,
24+
animationFrameRef: { current: number | null },
2325
reducedMotion = false
2426
) {
27+
// Cache frequently accessed values
28+
let frameCount = 0
29+
2530
const animate = () => {
2631
animationFrameRef.current = requestAnimationFrame(animate)
32+
frameCount++
33+
34+
if (objects.wireframeObjects && objects.wireframeObjects.children.length > 0) {
35+
const shouldUpdateColors = frameCount % 2 === 0
36+
37+
const maxDistance = 200
2738

28-
if (objects.wireframeObjects) {
2939
objects.wireframeObjects.children.forEach((object: Object3D) => {
40+
const distance = Math.sqrt(
41+
object.position.x ** 2 + object.position.y ** 2 + object.position.z ** 2
42+
)
43+
44+
if (distance > maxDistance) {
45+
return
46+
}
3047
const { rotationSpeed, scale, movement, color } = object.userData
3148

3249
if (!reducedMotion) {
@@ -43,7 +60,7 @@ export function animateScene(
4360
Math.abs(object.position.y) > movement.bounds.y ||
4461
Math.abs(object.position.z) > movement.bounds.z
4562
) {
46-
const visibleAngle = Math.random() * Math.PI * 2
63+
const visibleAngle = Math.random() * PI_2
4764
const distanceFromCenter = 90 + Math.random() * 40
4865

4966
object.position.x =
@@ -82,11 +99,9 @@ export function animateScene(
8299
object.scale.set(scale.current, scale.current, scale.current)
83100
}
84101

85-
const deltaTime = 0.016
86-
const lineObject = object as Line | LineLoop
87-
88-
if (!reducedMotion) {
89-
color.timeElapsed += deltaTime
102+
if (!reducedMotion && shouldUpdateColors) {
103+
const lineObject = object as Line | LineLoop
104+
color.timeElapsed += DELTA_TIME
90105

91106
if (!color.inTransition) {
92107
if (color.timeElapsed >= color.nextChangeDelay) {

app/components/SynthwaveBackground/utils/useThreeScene.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { useEffect, useRef, useState } from 'react'
2-
import {
3-
Scene,
4-
Color,
5-
Fog,
6-
PerspectiveCamera,
7-
WebGLRenderer
8-
} from '~/utils/three-lite'
2+
import { Scene, Color, Fog, PerspectiveCamera, WebGLRenderer } from '~/utils/three-lite'
93
import { COLORS, SCENE_CONFIG } from '../config'
104
import { useResize } from './useResize'
115

@@ -28,12 +22,7 @@ export function useThreeScene() {
2822

2923
scene.fog = new Fog(COLORS.DARK_NAVY, SCENE_CONFIG.FOG_NEAR, SCENE_CONFIG.FOG_FAR)
3024

31-
const camera = new PerspectiveCamera(
32-
75,
33-
window.innerWidth / window.innerHeight,
34-
0.1,
35-
1000
36-
)
25+
const camera = new PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
3726
cameraRef.current = camera
3827
camera.position.set(
3928
SCENE_CONFIG.CAMERA_POSITION.x,
@@ -42,9 +31,10 @@ export function useThreeScene() {
4231
)
4332

4433
const renderer = new WebGLRenderer({
45-
antialias: true,
34+
antialias: false,
4635
alpha: true,
4736
powerPreference: 'high-performance',
37+
stencil: false,
4838
})
4939
rendererRef.current = renderer
5040
renderer.setSize(window.innerWidth, window.innerHeight)

app/utils/three-lite.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ export {
1414
Fog,
1515
PerspectiveCamera,
1616
WebGLRenderer,
17-
Object3D
18-
} from 'three'
17+
Object3D,
18+
} from 'three'

0 commit comments

Comments
 (0)