Skip to content

Commit 93f70e9

Browse files
authored
[#77] feat: home page background
* wip: new branch test * feat: new home page three background
1 parent 867c980 commit 93f70e9

File tree

3 files changed

+102
-56
lines changed

3 files changed

+102
-56
lines changed

src/components/three-background/discs.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import * as THREE from "three";
2+
import {scene} from "./scene.ts";
3+
4+
let gridSpheres: { mesh: THREE.Mesh, dist: number }[] = [];
5+
let backgroundGridSpheres: { mesh: THREE.Mesh, baseY: number }[] = [];
6+
let backgroundGridSpheres2D: { mesh: THREE.Mesh, baseY: number }[][] = [];
7+
8+
export const initHomePage = () => {
9+
createWavyGridFloor();
10+
createBackgroundGrid();
11+
animateWaves();
12+
}
13+
14+
const createWavyGridFloor = () => {
15+
const size = 1000;
16+
const divisions = 40;
17+
const step = size / divisions;
18+
const halfSize = size / 2;
19+
20+
const sphereGeometry = new THREE.SphereGeometry(4, 8, 8);
21+
const sphereMaterial = new THREE.MeshBasicMaterial({ color: 0x3BFFC5, wireframe: true });
22+
23+
gridSpheres = [];
24+
25+
for (let i = 0; i <= divisions; i++) {
26+
for (let j = 0; j <= divisions; j++) {
27+
const x = -halfSize + i * step;
28+
const z = -halfSize + j * step;
29+
const dist = Math.sqrt(x * x + z * z) / halfSize; // normalized [0,1]
30+
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
31+
sphere.position.set(x, 0, z);
32+
scene.add(sphere);
33+
gridSpheres.push({ mesh: sphere, dist });
34+
}
35+
}
36+
}
37+
38+
const createBackgroundGrid = () => {
39+
const size = 10000;
40+
const divisions = 50;
41+
const step = size / divisions;
42+
const halfSize = size / 2;
43+
44+
const sphereGeometry = new THREE.SphereGeometry(3, 8, 8);
45+
const sphereMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true });
46+
47+
backgroundGridSpheres = [];
48+
backgroundGridSpheres2D = [];
49+
50+
// Create spheres and store in 2D array
51+
for (let i = 0; i <= divisions; i++) {
52+
backgroundGridSpheres2D[i] = [];
53+
for (let j = 0; j <= divisions; j++) {
54+
const x = -halfSize + i * step;
55+
const z = -halfSize + j * step - 500;
56+
const dist = Math.sqrt(x * x + z * z) / halfSize;
57+
const amplitude = -2500;
58+
const baseY = -2500 - dist * amplitude;
59+
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
60+
sphere.position.set(x, baseY, z);
61+
scene.add(sphere);
62+
const sphereObj = { mesh: sphere, baseY };
63+
backgroundGridSpheres.push(sphereObj);
64+
backgroundGridSpheres2D[i][j] = sphereObj;
65+
}
66+
}
67+
68+
// Link neighbors with lines
69+
const lineMaterial = new THREE.LineBasicMaterial({ color: 0xffffff, opacity: 0.3, transparent: true });
70+
for (let i = 0; i <= divisions; i++) {
71+
for (let j = 0; j <= divisions; j++) {
72+
const current = backgroundGridSpheres2D[i][j].mesh.position;
73+
// Link to right neighbor
74+
if (i < divisions) {
75+
const right = backgroundGridSpheres2D[i + 1][j].mesh.position;
76+
const geometry = new THREE.BufferGeometry().setFromPoints([current, right]);
77+
const line = new THREE.Line(geometry, lineMaterial);
78+
scene.add(line);
79+
}
80+
// Link to bottom neighbor
81+
if (j < divisions) {
82+
const bottom = backgroundGridSpheres2D[i][j + 1].mesh.position;
83+
const geometry = new THREE.BufferGeometry().setFromPoints([current, bottom]);
84+
const line = new THREE.Line(geometry, lineMaterial);
85+
scene.add(line);
86+
}
87+
}
88+
}
89+
}
90+
91+
const animateWaves = () => {
92+
const time = performance.now() * 0.001;
93+
for (const { mesh, dist } of gridSpheres) {
94+
const { x, z } = mesh.position;
95+
const amplitude = 20 + dist * 60;
96+
mesh.position.y = Math.sin(0.01 * (x + z) + time) * amplitude - 500;
97+
}
98+
99+
requestAnimationFrame(animateWaves);
100+
}

src/components/three-background/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {changeDepth, initCamera, initScene, initStage, renderer} from "./scene.t
33
import {initBgMeshes} from "./particules.ts";
44
import {initPostProcess} from "./post-process.ts";
55
import {animate} from "./animation-loop.ts";
6-
import {initDiscs} from "./discs.ts";
6+
import {initHomePage} from "./homeGrid.ts";
77

88
export enum BackgroundChoice {
99
Home,
@@ -18,7 +18,7 @@ export function threeBackground(choice: BackgroundChoice) {
1818
initCamera();
1919

2020
if (choice === BackgroundChoice.Home) {
21-
initDiscs();
21+
initHomePage();
2222
}
2323

2424
if (choice === BackgroundChoice.Project) {

0 commit comments

Comments
 (0)