-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.jsx
220 lines (205 loc) · 7.9 KB
/
animation.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { useEffect, useRef } from 'react';
import { Canvas, useLoader, useThree, useFrame } from '@react-three/fiber';
import { OrbitControls, PerspectiveCamera } from '@react-three/drei';
import * as THREE from 'three';
import { gsap } from 'gsap';
import { Simplex, Curl, patchShaders } from 'gl-noise'
import { createRoot } from 'react-dom/client';
import vertexShader from './shaders/vertex.glsl';
import fragmentShader from './shaders/fragment.glsl';
const chunks = [[ Simplex, Curl], null,]
let patchedVertexShader, patchedFragmentShader;
const loadShaders = async () => {
[patchedVertexShader, patchedFragmentShader] = await patchShaders([vertexShader, fragmentShader], chunks);
};
loadShaders();
// Functions...
const createCanvas = (texture) => {
let canvas = document.createElement('canvas');
let textureWidth = texture.image.width * 0.35;
let textureHeight = texture.image.height * 0.35;
let aspect = texture.image.width / texture.image.height;
canvas.height = window.innerHeight * 0.55;
canvas.width = canvas.height * aspect;
let context = canvas.getContext('2d');
context.drawImage(texture.image, 0, 0, canvas.width, canvas.height);
return { canvas, context };
};
const getImageData = (canvas, context) => {
return context.getImageData(0, 0, canvas.width, canvas.height);
};
const extractPositionsAndColors = (imageData) => {
let positions = [];
let colors = [];
for (let y = 0; y < imageData.height; y += 1) {
for (let x = 0; x < imageData.width; x += 1) {
let index = (x + y * imageData.width) * 4;
let r = imageData.data[index];
let g = imageData.data[index + 1];
let b = imageData.data[index + 2];
if (r > 0 || g > 0 || b > 0) {
let scale = 0.05;
let position = new THREE.Vector3((x - imageData.width / 2) * scale, (-y + imageData.height / 2) * scale, 0);
positions.push(position.x, position.y, position.z);
colors.push(r / 255, g / 255, b / 255);
}
}
}
return { positions, colors };
}
const createGeometry = (positions, colors) => {
let geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
return geometry;
};
const Animation = React.memo(() => {
const texture = useLoader(THREE.TextureLoader, 'src/assets/images/logo.png');
const { camera, gl, scene } = useThree();
const PointsRef = useRef();
const materialRef = useRef();
const geometryRef = useRef();
const positionRef = useRef([]);
const positionsRef = useRef([]);
const velocitiesRef = useRef([]);
const initialPositionsRef = useRef([]);
const currentPositionsRef = useRef([]);
// moved functions from here to top of file
useEffect(() => {
if (!texture.image || !PointsRef.current) {
return;
}
const { canvas, context } = createCanvas(texture);
const imageData = getImageData(canvas, context);
let position = [];
let velocities = [];
let initialPositions = [];
let currentPositions = [];
const { positions, colors } = extractPositionsAndColors(imageData);
geometryRef.current = createGeometry(positions, colors);
// positionsRef.current = positions; * Check if this is needed, if not remove it. -> significantly drops CPU load *
let pts = new Array(positions.length / 3).fill().map((p, i) => {
let position = new THREE.Vector3()
.randomDirection()
.multiplyScalar(Math.random() * 0.5 + 25);
return position;
});
position = pts;
positionRef.current = position;
initialPositions = [];
for (let i = 0; i < pts.length; i++) {
initialPositions.push(pts[i].x, pts[i].y, pts[i].z);
}
geometryRef.current.setAttribute('initialPosition', new THREE.Float32BufferAttribute(initialPositions, 3));
initialPositionsRef.current = initialPositions;
currentPositions = [...initialPositions];
geometryRef.current.setAttribute('currentPosition', new THREE.Float32BufferAttribute(currentPositions, 3));
currentPositionsRef.current = currentPositions;
velocities = [];
for (let i = 0; i < positions.length / 3; i++) {
let velocity = new THREE.Vector3()
.randomDirection()
.multiplyScalar(Math.random() * 0.5)
velocities.push(velocity.x, velocity.y, velocity.z);
}
geometryRef.current.setAttribute('velocity', new THREE.Float32BufferAttribute(velocities, 3));
velocitiesRef.current = velocities;
let clock = new THREE.Clock();
try {
let material = new THREE.ShaderMaterial({
uniforms: {
transition: { value: 0 },
morphTransition: { value: 0 },
opacity: { value: 1.0 },
time: { value: 0.0 },
velocity: { value: 0.0 },
uSeed: { value: Math.random() },
},
vertexShader: patchedVertexShader,
fragmentShader: patchedFragmentShader,
transparent: true,
depthTest: false,
blending: THREE.NormalBlending,
});
materialRef.current = material;
} catch (error) {
console.error("Error creating ShaderMaterial: ", error);
}
// gsap.to(materialRef.current.uniforms.opacity, {
// value: 0,
// duration: 0.5,
// delay: 5.9,
// });
let tl = gsap.timeline();
tl.to(materialRef.current.uniforms.transition, {
value: 1,
duration: 3,
onStart: function() {
geometryRef.current.attributes.initialPosition.copy(geometryRef.current.attributes.currentPosition);
}
});
tl.to(materialRef.current.uniforms.morphTransition, {
value: 1,
duration: 2.9,
ease: "power2.inOut",
});
tl.eventCallback("onComplete", function() {
}, [texture, PointsRef.current]);
return () => window.removeEventListener('resize', handleResize);
}, []);
useFrame(({ clock }) => {
if (materialRef.current && geometryRef.current) {
let material = materialRef.current;
let geometry = geometryRef.current;
let position = positionRef.current;
let positions = positionsRef.current;
let velocities = velocitiesRef.current;
let initialPositions = initialPositionsRef.current;
let t = clock.getElapsedTime();
let tClamped = Math.min(t, 2); // Clamp t to a maximum of 2
if (t <= 2) {
material.uniforms.transition.value = t / 2;
}
// Update the current positions
if (geometryRef.current && geometryRef.current.attributes.currentPosition) {
for (let i = 0; i < positions.length / 3; i++) {
let velocity = new THREE.Vector3(
velocities[i * 3],
velocities[i * 3 + 1],
velocities[i * 3 + 2]
);
let currentPosition = new THREE.Vector3(
initialPositions[i * 3] + velocity.x * tClamped,
initialPositions[i * 3 + 1] + velocity.y * tClamped,
initialPositions[i * 3 + 2] + velocity.z * tClamped
);
geometryRef.current.attributes.currentPosition.setXYZ(i, currentPosition.x, currentPosition.y, currentPosition.z);
}
geometryRef.current.attributes.currentPosition.needsUpdate = true;
material.uniforms.time.value = t;
}
}
});
return (
<>
<points ref={PointsRef} args={[geometryRef.current, materialRef.current]}>
<bufferGeometry attach="geometryRef.current"/>
<shaderMaterial attach="materialRef.current"/>
</points>
</>
);
})
export default function App() {
return (
<Canvas
camera={{ fov: 50, aspect: window.innerWidth / window.innerHeight, near: 1, far: 1000, position: [0, 0, 80] }}
style={{ width: '100vw', height: '100vh' }}
>
<PerspectiveCamera makeDefault position={[0, 0, 80]} />
<Animation />
<OrbitControls enableDamping={false} zoomToCursor={false} enableRotate={false} maxDistance={100} minDistance={100} />
</Canvas>
);
}
const root = createRoot(document.getElementById('root'));
root.render(<App />);