-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
201 lines (162 loc) · 4.78 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import './style.css';
import * as THREE from 'three';
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader';
// import { } from 'three/examples/jsm/loaders/BasisTextureLoader'
var loadProgress = 0;
import oceanURI from './src/ocean2.jpg';
import scallop_texture_uri from './src/scallop_texture.png';
import seastar_texture_uri from './src/seastar_texture.jpeg';
const loader = new DRACOLoader();
loader.setDecoderPath('./src/draco/');
// Setup
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.setZ(30);
camera.position.setX(-3);
renderer.render(scene, camera);
// Torus
const geometry = new THREE.SphereGeometry(10);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
wireframe: true,
});
const torus = new THREE.Mesh(geometry, material);
scene.add(torus);
// Lights
const pointLight = new THREE.PointLight(0xffffff);
pointLight.position.set(5, 5, 5);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.75);
scene.add(pointLight, ambientLight);
// Helpers
// const lightHelper = new THREE.PointLightHelper(pointLight)
// const gridHelper = new THREE.GridHelper(200, 50);
// scene.add(lightHelper, gridHelper)
// const controls = new OrbitControls(camera, renderer.domElement);
function addStar() {
const geometry = new THREE.SphereGeometry(0.25, 24, 24);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
opacity: 0.7,
transparent: true,
});
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(200).fill().forEach(addStar);
// Background
const oceanTexture = new THREE.TextureLoader().load(oceanURI);
scene.background = oceanTexture;
//Load seastar
const seastarTexture = new THREE.TextureLoader().load(seastar_texture_uri);
// Sea Star
loader.load(
'./src/seastar.drc',
function (geometry) {
const material = new THREE.MeshStandardMaterial({ map: seastarTexture });
const mesh = new THREE.Mesh(geometry, material);
mesh.scale.multiplyScalar(0.03);
mesh.name = 'seastar';
mesh.position.z = -5;
mesh.position.y = -1;
mesh.position.x = 3;
scene.add(mesh);
loadProgress++;
if (loadProgress === 2) {
document.querySelector('body').classList.toggle('loaded');
}
},
// called as loading progresses
function (xhr) {
console.log((xhr.loaded / xhr.total) * 100 + '% loaded');
},
function (error) {
console.error(error);
}
);
//Load scallop
const scallopTexture = new THREE.TextureLoader().load(scallop_texture_uri);
loader.load(
'./src/scallop.drc',
function (geometry) {
const material = new THREE.MeshStandardMaterial({ map: scallopTexture });
const mesh = new THREE.Mesh(geometry, material);
mesh.scale.multiplyScalar(0.08);
mesh.name = 'scallop';
mesh.position.z = 17;
mesh.position.y = 0;
mesh.position.x = -10;
scene.add(mesh);
loadProgress++;
if (loadProgress === 2) {
document.querySelector('body').classList.toggle('loaded');
}
},
// called as loading progresses
function (xhr) {
console.log((xhr.loaded / xhr.total) * 100 + '% loaded');
},
function (error) {
console.error(error);
}
);
// Scroll Animation
function moveCamera() {
const fish = scene.getObjectByName('seastar');
const scallop = scene.getObjectByName('scallop');
const t = document.body.getBoundingClientRect().top;
if (fish) {
fish.position.z = t * -0.01 - 5;
fish.position.x = t * -0.0002 + 3;
fish.position.y = -1;
// fish.rotation.x += 0.05;
}
if (scallop) {
scallop.rotation.x += 0.015;
scallop.rotation.y += 0.03;
scallop.rotation.z += 0.015;
if (t < -3000) {
scallop.position.z = (t + 3000) * -0.005 + 17;
scallop.position.x = (t + 3000) * -0.0002 - 10;
}
}
// jeff.rotation.y += 0.01;
// jeff.rotation.z += 0.01;
camera.position.z = t * -0.01;
camera.position.x = t * -0.0002;
// camera.rotation.y = t * -0.0002;
}
document.body.onscroll = moveCamera;
moveCamera();
// Animation Loop
function animate() {
requestAnimationFrame(animate);
torus.rotation.x += 0.001;
torus.rotation.y += 0.001;
torus.rotation.z += 0.001;
const fish = scene.getObjectByName('seastar');
if (fish) {
fish.rotation.x -= 0.0017;
fish.rotation.z += 0.009;
}
const scallop = scene.getObjectByName('scallop');
if (scallop) {
scallop.rotation.y += 0.01;
}
// controls.update();
renderer.render(scene, camera);
}
animate();