-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.js
121 lines (102 loc) · 3.83 KB
/
Main.js
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
import './Style.css';
import * as THREE from 'three';
import { AsciiEffect } from 'three/examples/jsm/effects/AsciiEffect.js';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
const canvas = document.getElementById("display-canvas");
const renderer = new THREE.WebGLRenderer({ canvas });
const effect = new AsciiEffect(renderer, ' .:-+*=%@#', { invert: true });
function updateCanvasSize() {
const width = document.body.scrollWidth;
const height = document.body.scrollHeight;
renderer.setSize(width, height);
effect.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
updateCanvasSize();
effect.domElement.style.color = 'white';
effect.domElement.style.backgroundColor = 'black';
document.body.appendChild(effect.domElement);
document.body.removeChild(renderer.domElement);
const dirLight = new THREE.DirectionalLight(0xffffff, 1);
dirLight.position.set(5, 5, 5);
scene.add(dirLight);
const heartShape = new THREE.Shape()
.moveTo(5, 5)
.bezierCurveTo(5, 5, 4, 0, 0, 0)
.bezierCurveTo(-6, 0, -6, 7, -6, 7)
.bezierCurveTo(-6, 11, -3, 15.4, 5, 19)
.bezierCurveTo(12, 15.4, 16, 11, 16, 7)
.bezierCurveTo(16, 7, 16, 0, 10, 0)
.bezierCurveTo(7, 0, 5, 5, 5, 5);
const heartGeometry = new THREE.ShapeGeometry(heartShape);
const heartMaterial = new THREE.MeshPhysicalMaterial({ color: 0xec0927 });
const heartMesh = new THREE.Mesh(heartGeometry, heartMaterial);
heartMesh.scale.set(0.05, 0.05, 0.05);
heartMesh.position.set(3, 17, -20);
heartMesh.rotation.z = Math.PI / 4;
scene.add(heartMesh);
const crossMaterial = new THREE.MeshPhysicalMaterial({ color: 0xFFD700 });
const verticalBar = new THREE.BoxGeometry(0.2, 1, 0.2);
const horizontalBar = new THREE.BoxGeometry(0.6, 0.2, 0.2);
const verticalMesh = new THREE.Mesh(verticalBar, crossMaterial);
const horizontalMesh = new THREE.Mesh(horizontalBar, crossMaterial);
horizontalMesh.position.y = 0.3;
verticalMesh.add(horizontalMesh);
verticalMesh.position.set(1, 0, -5);
scene.add(verticalMesh);
const cubeGeometry = new THREE.BoxGeometry(0.4, 0.4, 0.4);
const cubeMaterial = new THREE.MeshPhysicalMaterial({ color: 0x00ff00 });
const cubeMesh = new THREE.Mesh(cubeGeometry, cubeMaterial);
cubeMesh.position.set(1, -10, -5);
scene.add(cubeMesh);
function createStar() {
const geometry = new THREE.SphereGeometry(0.1, 24, 24);
const material = new THREE.MeshBasicMaterial({ color: 0xffffff });
const star = new THREE.Mesh(geometry, material);
const x = Math.random() * 200 - 100;
const y = Math.random() * 200 - 100;
const z = -Math.random() * 500 - 50;
star.position.set(x, y, z);
scene.add(star);
}
for (let i = 0; i < 500; i++) {
createStar();
}
const gltfLoader = new GLTFLoader();
async function loadModels() {
const teapot = await gltfLoader.loadAsync('Teapot.gltf');
teapot.scene.position.set(10, -15, -5);
teapot.scene.scale.set(1, 1, 1);
teapot.scene.rotation.set(35, 0, Math.PI / 4);
scene.add(teapot.scene);
const suzanne = await gltfLoader.loadAsync('Suzanne.glb');
suzanne.scene.position.set(-11, -5, -5);
suzanne.scene.scale.set(2, 2, 2);
scene.add(suzanne.scene);
}
loadModels();
function moveCamera() {
const t = document.body.getBoundingClientRect().top;
camera.position.y = t * 0.003;
dirLight.position.copy(camera.position);
}
document.body.onscroll = moveCamera;
function rotateObjects() {
heartMesh.rotation.x += 0.01;
heartMesh.rotation.y += 0.01;
verticalMesh.rotation.x += 0.01;
verticalMesh.rotation.y += 0.01;
cubeMesh.rotation.x += 0.01;
cubeMesh.rotation.y += 0.01;
}
function animate() {
requestAnimationFrame(animate);
rotateObjects();
effect.render(scene, camera);
}
window.addEventListener('resize', updateCanvasSize);
animate();