Skip to content

Commit 94aa8de

Browse files
authored
feat: [#40] three backgrounds
* wip: corridor bacground * feat: corridor background
1 parent ee3443a commit 94aa8de

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed
Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,64 @@
11
import * as THREE from "three";
22
import {scene, windowHeight, windowWidth} from "./scene.ts";
33

4+
45
//-----------------------------------------------------------------------
56

67
export const initBgObjects = () => {
7-
for (let i = 0; i < 40; i++) {
8+
for (let i = 0; i < 1000; i++) {
89
createBgObject();
910
}
11+
12+
createCurvedPlane();
1013
}
1114

15+
1216
//-----------------------------------------------------------------------
1317

1418
const createBgObject = () => {
15-
const geometry = new THREE.SphereGeometry( 10, 6, 6 );
16-
const material = new THREE.MeshBasicMaterial( {color: 0xdddddd} );
19+
const size = Math.random() * 30 + 5;
20+
const geometry = new THREE.IcosahedronGeometry(size, 1);
21+
const material = new THREE.MeshBasicMaterial( {
22+
color: 0xdddddd,
23+
wireframe: true
24+
} );
1725
const sphere = new THREE.Mesh( geometry, material );
1826
scene.add( sphere );
19-
const x = Math.random() * windowWidth * 2 - windowWidth;
20-
const y = Math.random() * windowHeight * 2 - windowHeight;
27+
const x = pointInMargins();
28+
const y = Math.random() * windowHeight * 15 - windowHeight * 7.5;
2129
const z = Math.random() * -2000 - 200;
30+
31+
sphere.userData.rotationSpeed = {
32+
x: Math.random() * 0.02 - 0.01,
33+
y: Math.random() * 0.02 - 0.01
34+
};
35+
2236
sphere.position.set(x, y, z);
23-
}
37+
}
38+
39+
40+
const pointInMargins = () => {
41+
const wrapper = 720;
42+
let point: number;
43+
44+
if (Math.random() < 0.5) {
45+
point = Math.random() * (windowWidth / 2 - wrapper * 2) - windowWidth / 2;
46+
} else {
47+
point = Math.random() * (windowWidth / 2 - wrapper * 2) + windowWidth / 2 + wrapper;
48+
}
49+
50+
return point;
51+
}
52+
53+
54+
const createCurvedPlane = () => {
55+
const geometry = new THREE.PlaneGeometry(windowWidth * 20, windowHeight * 20, 32, 32);
56+
57+
const material = new THREE.MeshBasicMaterial({ color: 0x000000 });
58+
59+
const plane = new THREE.Mesh(geometry, material);
60+
scene.add(plane);
61+
plane.position.set(windowWidth, 0, -2000); // Position the plane in the far distance
62+
};
63+
64+

src/components/particule-background/scene.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as THREE from 'three';
22

3+
34
export let renderer: THREE.WebGLRenderer,
45
scene: THREE.Scene,
56
camera: THREE.PerspectiveCamera,
@@ -16,6 +17,9 @@ let graphicCanvas,
1617
windowHalfHeight: number,
1718
cameraLookAt = new THREE.Vector3(0, 0, 0);
1819

20+
const mouseSensitivity = 0.1;
21+
const cameraTilt = 35;
22+
1923
//-----------------------------------------------------------------------
2024

2125
export const initStage = () => {
@@ -27,6 +31,8 @@ export const initStage = () => {
2731

2832
export const initScene = () => {
2933
scene = new THREE.Scene();
34+
scene.fog = new THREE.Fog(0x010102, 1, 3000);
35+
3036

3137
renderer = new THREE.WebGLRenderer({
3238
alpha: true,
@@ -41,7 +47,7 @@ export const initCamera = () => {
4147
const fieldOfView = 75;
4248
const aspectRatio = windowWidth / windowHeight;
4349
const nearPlane = 1;
44-
const farPlane = 3000;
50+
const farPlane = 30000;
4551
camera = new THREE.PerspectiveCamera(
4652
fieldOfView,
4753
aspectRatio,
@@ -67,6 +73,7 @@ export const animate = () => {
6773
requestAnimationFrame(animate);
6874
camera.position.lerp(cameraTarget, 0.2);
6975
camera.lookAt(cameraLookAt);
76+
7077
render();
7178
}
7279

@@ -75,8 +82,13 @@ export const animate = () => {
7582
const onMouseMove = (event: MouseEvent) => {
7683
mouseX = (event.clientX - windowHalfWidth);
7784
mouseY = (event.clientY - windowHalfHeight);
78-
cameraTarget.x = (mouseX * -1) / 2;
79-
cameraTarget.y = mouseY / 2;
85+
cameraTarget.x = (mouseX * -1) * mouseSensitivity;
86+
cameraTarget.y = mouseY * mouseSensitivity;
87+
88+
cameraTarget.clamp(
89+
new THREE.Vector3(-cameraTilt, -cameraTilt, 800),
90+
new THREE.Vector3(cameraTilt, cameraTilt, 800)
91+
);
8092
}
8193

8294
const onWindowResize = () => {
@@ -87,6 +99,7 @@ const onWindowResize = () => {
8799
renderer.setSize(windowWidth, windowHeight);
88100
}
89101

102+
90103
const render = () => {
91104
renderer.render(scene, camera);
92105
}

0 commit comments

Comments
 (0)