-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
173 lines (145 loc) · 4.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
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
import "./style.css";
import * as THREE from "three";
import backgroundFile from "./assets/background.jpg";
import pictureFile from "./assets/nico.jpg";
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
const renderer = new THREE.WebGLRenderer({
canvas: document.querySelector("#bg"),
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
camera.position.setX(-3);
camera.position.setZ(30);
//eje x: mov horizontal
//eje y: mov vertical
//eje z: cercania con camara
const geometry1 = new THREE.IcosahedronGeometry(1);
const material1 = new THREE.MeshStandardMaterial({
color: 0x165654,
// wireframe: true,
});
const figure1 = new THREE.Mesh(geometry1, material1);
figure1.position.set(3, 0.5, 1);
scene.add(figure1);
const geometry2 = new THREE.OctahedronGeometry(1);
const material2 = new THREE.MeshStandardMaterial({
color: 0x8fc992,
});
const figure2 = new THREE.Mesh(geometry2, material2);
figure2.position.set(-3, -1, -4);
scene.add(figure2);
const geometry3 = new THREE.TetrahedronGeometry(1);
const material3 = new THREE.MeshStandardMaterial({
color: 0x03428c,
});
const figure3 = new THREE.Mesh(geometry3, material3);
figure3.position.set(-2, -1, 2);
scene.add(figure3);
const geometry4 = new THREE.ConeGeometry(2, 2, 4);
const material4 = new THREE.MeshStandardMaterial({
color: 0xc42744,
});
const figure4 = new THREE.Mesh(geometry4, material4);
figure4.position.set(1, 1, -10);
scene.add(figure4);
const pointLight = new THREE.PointLight(0xffffff);
pointLight.position.set(0.5, 0, 3);
scene.add(pointLight);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
// const lightHelper = new THREE.PointLightHelper(pointLight);
// const gridHelper = new THREE.GridHelper(200, 200);
// const cameraHelper = new THREE.CameraHelper(camera);
// scene.add(lightHelper, gridHelper, cameraHelper);
function addStar() {
const geometry = new THREE.SphereGeometry(0.25, 24, 24);
const material = new THREE.MeshStandardMaterial({ color: 0xffffff });
const star = new THREE.Mesh(geometry, material);
const [x, y, z] = Array(3)
.fill()
.map(() => THREE.MathUtils.randFloatSpread(100));
star.position.set(x, y, z);
scene.add(star);
}
Array(500).fill().forEach(addStar);
function reloadBackground() {
const background = new THREE.TextureLoader().load(
backgroundFile,
function (tex) {
// tex and texture are the same in this example, but that might not always be the case
const targetAspect = window.innerWidth / window.innerHeight;
const imageAspect = tex.image.naturalWidth / tex.image.naturalHeight;
const factor = imageAspect / targetAspect;
// When factor larger than 1, that means texture 'wilder' than target。
// we should scale texture height to target height and then 'map' the center of texture to target, and vice versa.
scene.background.offset.x = factor > 1 ? (1 - 1 / factor) / 2 : 0;
scene.background.repeat.x = factor > 1 ? 1 / factor : 1;
scene.background.offset.y = factor > 1 ? 0 : (1 - factor) / 2;
scene.background.repeat.y = factor > 1 ? 1 : factor;
}
);
scene.background = background;
}
reloadBackground();
const foto = new THREE.TextureLoader().load(pictureFile);
const nico = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 0.07),
new THREE.MeshBasicMaterial({ map: foto })
);
nico.position.set(1, 1, -2.5);
nico.rotation.set(0, -0.4, 0);
repositionPhoto();
scene.add(nico);
function moveCamera() {
const t = document.body.getBoundingClientRect().top;
camera.position.z = t * -0.002;
camera.position.x = t * -0.001;
camera.rotation.y = t * -0.0002;
}
document.body.onscroll = moveCamera;
moveCamera();
function animate() {
requestAnimationFrame(animate);
figure1.rotation.x += 0.001;
figure1.rotation.y += 0.001;
figure1.rotation.z += 0.001;
figure2.rotation.y += 0.005;
figure3.rotation.x -= 0.001;
figure3.rotation.y -= 0.001;
figure3.rotation.z -= 0.001;
figure4.rotation.y += 0.005;
renderer.render(scene, camera);
}
window.addEventListener("resize", onWindowResize, false);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
repositionPhoto();
reloadBackground();
}
function repositionPhoto() {
if (window.innerWidth <= 720) {
nico.position.x = 0;
} else {
nico.position.x = 1;
}
}
animate();
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener("click", function (e) {
e.preventDefault();
const targetElement = document.querySelector(this.getAttribute("href"));
if (targetElement) {
targetElement.scrollIntoView({
behavior: "smooth",
});
}
});
});