Skip to content

Commit adc4085

Browse files
authored
Merge pull request #22 from WooWan/perf/stage-performance-improve
Perf/stage performance improve
2 parents 8dc8dd0 + 3afdf2b commit adc4085

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

src/components/Bar.tsx

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
1-
import React, { useRef, useState } from 'react';
1+
import React, { useRef } from 'react';
22
import * as THREE from 'three';
33
import { useFrame } from '@react-three/fiber';
44
import { lerp } from 'three/src/math/MathUtils';
55

66
type Props = {
77
radius: number;
8+
index: number;
89
centerPos: number[];
9-
mean: number;
10-
musicInput: number;
1110
position: THREE.Vector3;
1211
theta: number;
1312
color: string;
13+
meanRef: React.MutableRefObject<number>;
14+
dataArrayRef: React.MutableRefObject<Uint8Array | null>;
1415
};
1516

16-
export default function Bar({ radius, centerPos, mean, musicInput, position, theta, color }: Props) {
17+
export default function Bar({ radius, index, centerPos, position, theta, color, meanRef, dataArrayRef }: Props) {
1718
const barRef = useRef<THREE.Mesh>(null!);
18-
const [height, setHeight] = useState(0);
19-
const [angle, setAngle] = useState<any>(theta);
20-
19+
const heightRef = useRef(0);
20+
const angleRef = useRef(theta);
2121
const radian = Math.PI / 180;
2222

23-
useFrame((_, delta) => {
24-
if (musicInput > height) {
25-
setHeight(musicInput);
23+
useFrame((_) => {
24+
if (!dataArrayRef.current) return;
25+
const mean = meanRef.current - 1;
26+
const frequency = dataArrayRef.current?.[index] / 128 - 1;
27+
const height = heightRef.current;
28+
29+
if (frequency > height) {
30+
heightRef.current = frequency;
2631
} else {
27-
setHeight(height - height * 0.2);
32+
heightRef.current = height - height * 0.2;
2833
}
29-
30-
const na = (angle + radian) % 360;
31-
setAngle(na);
34+
angleRef.current = (angleRef.current + radian) % 360;
3235
const power = mean < 0 ? 0 : Math.round(mean * 10000) / 10000;
33-
const nx = centerPos[0] + (radius + power * 500) * Math.cos(angle);
34-
const ny = centerPos[1] + height * height * 1200;
35-
const nz = centerPos[2] + (radius + power * 500) * Math.sin(angle);
36+
const nx = centerPos[0] + (radius + power * 500) * Math.cos(angleRef.current);
37+
const ny = centerPos[1] + heightRef.current * heightRef.current * 1200;
38+
const nz = centerPos[2] + (radius + power * 500) * Math.sin(angleRef.current);
3639

3740
barRef.current.position.x = lerp(barRef.current.position.x, nx, 0.02);
3841
barRef.current.position.y = lerp(barRef.current.position.y, ny, 0.03);

src/components/MusicAnalyzer.tsx

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useMemo, useState } from 'react';
1+
import React, { useEffect, useMemo, useRef, useState } from 'react';
22
import { useFrame } from '@react-three/fiber';
33
import * as THREE from 'three';
44
import Bar from './Bar';
@@ -13,8 +13,10 @@ type Props = {
1313

1414
export default function MusicAnalyzer({ music, fftSize, centerPos, radius }: Props) {
1515
const [analyser, setAnalyser] = useState<any>(null);
16-
const [dataArray, setDataArray] = useState<any>(null);
17-
const [mean, setMean] = useState(0);
16+
const dataArrayRef = useRef<Uint8Array | null>(null);
17+
const meanRef = useRef(0);
18+
// const [dataArray, setDataArray] = useState<any>(null);
19+
// const [mean, setMean] = useState(0);
1820
const [sourceNode, setSourceNode] = useState<MediaElementAudioSourceNode | null>(null);
1921
const isMusicPlay = useMusicPlayStore((state) => state.isMusicPlay);
2022

@@ -46,26 +48,31 @@ export default function MusicAnalyzer({ music, fftSize, centerPos, radius }: Pro
4648
setAnalyser(analyser);
4749
}, [isMusicPlay]);
4850

51+
const newData = new Uint8Array(fftSize);
4952
useFrame(() => {
5053
if (analyser) {
51-
const newData = new Uint8Array(fftSize);
52-
5354
analyser.getByteTimeDomainData(newData);
5455

55-
setMean(newData.reduce((a, b) => a + b) / (128 * newData.length));
56-
setDataArray(newData);
56+
// setMean(newData.reduce((a, b) => a + b) / (128 * newData.length));
57+
// setDataArray(newData);
58+
59+
meanRef.current = newData.reduce((a, b) => a + b) / (128 * newData.length);
60+
dataArrayRef.current = newData;
5761
}
5862
});
59-
if (isMusicPlay && dataArray) {
63+
if (isMusicPlay) {
6064
return (
6165
<group>
6266
{bars.map((item, index) => (
6367
<Bar
6468
key={index}
69+
index={index}
6570
radius={radius}
6671
centerPos={centerPos}
67-
mean={mean - 1}
68-
musicInput={dataArray[index] / 128 - 1}
72+
// mean={mean - 1}
73+
// musicInput={dataArray[index] / 128 - 1}
74+
dataArrayRef={dataArrayRef}
75+
meanRef={meanRef}
6976
position={item.position}
7077
theta={item.theta}
7178
color={item.color}

0 commit comments

Comments
 (0)