generated from codrops/CodropsTemplate
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.js
103 lines (82 loc) · 2.79 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
import './css/base.css';
import * as THREE from 'three';
import fragmentShader from './shaders/fragment.glsl';
// extract "variation" parameter from the url
const urlParams = new URLSearchParams(window.location.search);
const variation = urlParams.get('var') || 0;
// add selected class to link based on variation parameter
document.querySelector(`[data-var="${variation}"]`).classList.add('selected');
// Scene setup
const scene = new THREE.Scene();
const vMouse = new THREE.Vector2();
const vMouseDamp = new THREE.Vector2();
const vResolution = new THREE.Vector2();
// Viewport setup (updated on resize)
let w, h = 1;
// Orthographic camera setup
const aspect = w / h;
const camera = new THREE.OrthographicCamera(-aspect, aspect, 1, -1, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
document.body.appendChild(renderer.domElement);
const onPointerMove = (e) => { vMouse.set(e.pageX, e.pageY) }
document.addEventListener('mousemove', onPointerMove);
document.addEventListener('pointermove', onPointerMove);
document.body.addEventListener('touchmove', function (e) { e.preventDefault(); }, { passive: false });
// Plane geometry covering the full viewport
const geo = new THREE.PlaneGeometry(1, 1); // Scaled to cover full viewport
// Shader material creation
const mat = new THREE.ShaderMaterial({
vertexShader: /* glsl */`
varying vec2 v_texcoord;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
v_texcoord = uv;
}`,
fragmentShader, // most of the action happening in the fragment
uniforms: {
u_mouse: { value: vMouseDamp },
u_resolution: { value: vResolution },
u_pixelRatio: { value: 2 }
},
defines: {
VAR: variation
}
});
// Mesh creation
const quad = new THREE.Mesh(geo, mat);
scene.add(quad);
// Camera position and orientation
camera.position.z = 1; // Set appropriately for orthographic
// Animation loop to render
let time, lastTime = 0;
const update = () => {
// calculate delta time
time = performance.now() * 0.001;
const dt = time - lastTime;
lastTime = time;
// ease mouse motion with damping
for (const k in vMouse) {
if (k == 'x' || k == 'y') vMouseDamp[k] = THREE.MathUtils.damp(vMouseDamp[k], vMouse[k], 8, dt);
}
// render scene
requestAnimationFrame(update);
renderer.render(scene, camera);
};
update();
const resize = () => {
w = window.innerWidth;
h = window.innerHeight;
const dpr = Math.min(window.devicePixelRatio, 2);
renderer.setSize(w, h);
renderer.setPixelRatio(dpr);
camera.left = -w / 2;
camera.right = w / 2;
camera.top = h / 2;
camera.bottom = -h / 2;
camera.updateProjectionMatrix();
quad.scale.set(w, h, 1);
vResolution.set(w, h).multiplyScalar(dpr);
mat.uniforms.u_pixelRatio.value = dpr;
};
resize();
window.addEventListener('resize', resize)