Skip to content

Commit 4c38ae3

Browse files
authored
feat: [#43] post processing layer
* feat: new post processing rendering workflow * feat: perfect post-process
1 parent 94aa8de commit 4c38ae3

File tree

9 files changed

+198
-101
lines changed

9 files changed

+198
-101
lines changed

src/components/particule-background/index.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/components/particule-background/particules.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {updateBgObjects} from "./particules.ts";
2+
import {renderWithPostProcess} from "./post-process.ts";
3+
import {camera, cameraTarget} from "./scene.ts";
4+
import * as THREE from "three";
5+
6+
let cameraLookAt = new THREE.Vector3(0, 0, 0);
7+
8+
export const animate = () => {
9+
requestAnimationFrame(animate);
10+
camera.position.lerp(cameraTarget, 0.2);
11+
camera.lookAt(cameraLookAt);
12+
13+
updateBgObjects();
14+
renderWithPostProcess();
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import "./styles.css";
2+
import {initCamera, initCanvas, initScene, initStage, renderer} from "./scene.ts";
3+
import {initBgMeshes} from "./particules.ts";
4+
import {initPostProcess} from "./post-process.ts";
5+
import {animate} from "./animation-loop.ts";
6+
7+
//-----------------------------------------------------------------------
8+
9+
export function animatedBackground() {
10+
initStage();
11+
initScene();
12+
initCanvas();
13+
initCamera();
14+
initBgMeshes();
15+
initPostProcess();
16+
animate();
17+
18+
return renderer.domElement;
19+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import * as THREE from "three";
2+
import {scene, windowHeight, windowWidth} from "./scene.ts";
3+
4+
5+
const meshWhiteMaterial = new THREE.MeshStandardMaterial( {
6+
color: 0xdddddd,
7+
wireframe: true,
8+
emissive: 0xdddddd,
9+
emissiveIntensity: 0.5,
10+
});
11+
12+
const meshBlueMaterial = new THREE.MeshStandardMaterial( {
13+
color: 0x3BFFC5,
14+
wireframe: false,
15+
emissive: 0x3BFFC5,
16+
emissiveIntensity: 0.5,
17+
});
18+
19+
20+
21+
//-----------------------------------------------------------------------
22+
23+
export const initBgMeshes = () => {
24+
for (let i = 0; i < 1000; i++) {
25+
const meshSize = Math.random() * 30 + 5;
26+
createBgMeshInMargins(
27+
new THREE.IcosahedronGeometry(meshSize, 0),
28+
meshWhiteMaterial
29+
);
30+
}
31+
32+
for (let i = 0; i < 100; i++) {
33+
const meshSize = Math.random() * 30 + 5;
34+
createBgMeshInMargins(
35+
new THREE.CylinderGeometry(meshSize, meshSize, 5, 32),
36+
meshBlueMaterial
37+
);
38+
}
39+
40+
createGiantWireSphere();
41+
createBackdropPlane();
42+
}
43+
44+
export const updateBgObjects = () => {
45+
scene.children.forEach((child) => {
46+
if (child instanceof THREE.Mesh && child.userData.rotationSpeed) {
47+
child.rotation.x += child.userData.rotationSpeed.x;
48+
child.rotation.y += child.userData.rotationSpeed.y;
49+
}
50+
});
51+
}
52+
53+
//-----------------------------------------------------------------------
54+
55+
const createBgMeshInMargins = (
56+
spawnMesh: THREE.CylinderGeometry | THREE.IcosahedronGeometry | THREE.TorusGeometry | THREE.BoxGeometry,
57+
meshMaterial: THREE.MeshStandardMaterial
58+
) => {
59+
const x = pointInMargins();
60+
const y = Math.random() * windowHeight * 15 - windowHeight * 7.5;
61+
const z = Math.random() * -2000 - 200;
62+
63+
const mesh = new THREE.Mesh( spawnMesh, meshMaterial );
64+
65+
mesh.userData.rotationSpeed = {
66+
x: Math.random() * 0.02 - 0.01,
67+
y: Math.random() * 0.02 - 0.01
68+
};
69+
70+
scene.add( mesh );
71+
mesh.position.set(x, y, z);
72+
}
73+
74+
75+
const pointInMargins = () => {
76+
const wrapper = 720;
77+
let point: number;
78+
79+
if (Math.random() < 0.5) {
80+
point = Math.random() * (windowWidth / 2 - wrapper * 2) - windowWidth / 2;
81+
} else {
82+
point = Math.random() * (windowWidth / 2 - wrapper * 2) + windowWidth / 2 + wrapper;
83+
}
84+
85+
return point;
86+
}
87+
88+
89+
const createGiantWireSphere = () => {
90+
const geometry = new THREE.IcosahedronGeometry(3250, 3);
91+
const material = new THREE.MeshBasicMaterial( {
92+
color: 0xdddddd,
93+
wireframe: true,
94+
} );
95+
96+
const sphere = new THREE.Mesh( geometry, material );
97+
scene.add(sphere);
98+
sphere.position.set(0, 0, 0);
99+
}
100+
101+
102+
const createBackdropPlane = () => {
103+
const geometry = new THREE.PlaneGeometry(windowWidth * 20, windowHeight * 20, 32, 32);
104+
const material = new THREE.MeshBasicMaterial({ color: 0x555555 });
105+
const plane = new THREE.Mesh(geometry, material);
106+
scene.add(plane);
107+
plane.position.set(windowWidth, 0, -2000);
108+
};
109+
110+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { camera, renderer, scene, windowHeight, windowWidth } from "./scene.ts";
2+
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
3+
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
4+
import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';
5+
import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
6+
import {Vector2} from "three";
7+
8+
let composer: EffectComposer;
9+
10+
const postProcessConfigs = {
11+
size: Vector2,
12+
threshold: 0.05,
13+
strength: 0.4,
14+
radius: 0.35,
15+
exposure: 0
16+
};
17+
18+
19+
export const initPostProcess = () => {
20+
const renderScene = new RenderPass( scene, camera );
21+
22+
const bloomPass = new UnrealBloomPass(
23+
new Vector2( windowWidth, windowHeight ), // size
24+
postProcessConfigs.strength, // strength
25+
postProcessConfigs.radius, // radius
26+
postProcessConfigs.threshold // threshold
27+
);
28+
29+
const outputPass = new OutputPass();
30+
31+
composer = new EffectComposer( renderer );
32+
composer.addPass( renderScene );
33+
composer.addPass( bloomPass );
34+
composer.addPass( outputPass );
35+
}
36+
37+
export const resizeComposer = () => {
38+
composer.setSize( windowWidth, windowHeight );
39+
}
40+
41+
export const renderWithPostProcess = () => {
42+
composer.render();
43+
}

src/components/particule-background/scene.ts renamed to src/components/three-background/scene.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import * as THREE from 'three';
2-
2+
import {resizeComposer} from "./post-process.ts";
33

44
export let renderer: THREE.WebGLRenderer,
55
scene: THREE.Scene,
66
camera: THREE.PerspectiveCamera,
77
cameraTarget = new THREE.Vector3(0, 0 ,800),
8-
windowWidth: number,
9-
windowHeight: number;
8+
windowWidth = window.innerWidth,
9+
windowHeight = window.innerHeight;
1010

1111
let graphicCanvas,
1212
canvasWidth = 240,
1313
canvasHeight = 240,
1414
mouseX = 0,
1515
mouseY = 0,
1616
windowHalfWidth: number,
17-
windowHalfHeight: number,
18-
cameraLookAt = new THREE.Vector3(0, 0, 0);
17+
windowHalfHeight: number;
1918

2019
const mouseSensitivity = 0.1;
2120
const cameraTilt = 35;
@@ -32,6 +31,7 @@ export const initStage = () => {
3231
export const initScene = () => {
3332
scene = new THREE.Scene();
3433
scene.fog = new THREE.Fog(0x010102, 1, 3000);
34+
scene.add( new THREE.AmbientLight( 0xcccccc ) );
3535

3636

3737
renderer = new THREE.WebGLRenderer({
@@ -62,23 +62,15 @@ export const initCanvas = () => {
6262
graphicCanvas.height = canvasHeight;
6363
}
6464

65-
export const setWindowSize = () => {
65+
//-----------------------------------------------------------------------
66+
67+
const setWindowSize = () => {
6668
windowWidth = window.innerWidth;
6769
windowHeight = window.innerHeight;
6870
windowHalfWidth = windowWidth / 2;
6971
windowHalfHeight = windowHeight / 2;
7072
}
7173

72-
export const animate = () => {
73-
requestAnimationFrame(animate);
74-
camera.position.lerp(cameraTarget, 0.2);
75-
camera.lookAt(cameraLookAt);
76-
77-
render();
78-
}
79-
80-
//-----------------------------------------------------------------------
81-
8274
const onMouseMove = (event: MouseEvent) => {
8375
mouseX = (event.clientX - windowHalfWidth);
8476
mouseY = (event.clientY - windowHalfHeight);
@@ -93,13 +85,11 @@ const onMouseMove = (event: MouseEvent) => {
9385

9486
const onWindowResize = () => {
9587
setWindowSize();
88+
resizeComposer();
9689

9790
camera.aspect = windowWidth / windowHeight;
9891
camera.updateProjectionMatrix();
9992
renderer.setSize(windowWidth, windowHeight);
93+
renderer.toneMapping = THREE.ReinhardToneMapping;
10094
}
10195

102-
103-
const render = () => {
104-
renderer.render(scene, camera);
105-
}
File renamed without changes.

src/views/utils/backgrounds-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {animatedBackground} from "../../components/particule-background";
1+
import {animatedBackground} from "../../components/three-background";
22

33

44
export function createBackground() {

0 commit comments

Comments
 (0)