-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (78 loc) · 2.57 KB
/
index.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
import * as THREE from 'three';
const OrbitControls = require('three-orbit-controls')(THREE);
import fragment from './shaders/fragment.glsl';
import vertex from './shaders/vertex.glsl';
class THREESceen {
constructor() {
this.container;
this.camera;
this.scene;
this.renderer;
this.controls;
this.material;
this.uniforms;
this.geometry;
this.mesh;
}
setup() {
this.scene = new THREE.Scene();
this.renderer = new THREE.WebGLRenderer({ antialias: true });
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.setClearColor(0xffffff);
this.container = document.getElementById('container');
this.container.appendChild(this.renderer.domElement);
this.camera = new THREE.PerspectiveCamera(
70,
window.innerWidth / window.innerHeight,
0.001,
100,
);
this.camera.position.set(0, 0, 5);
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
this.createPlane();
this.onWindowResize();
this.eventListeners();
}
createPlane() {
this.uniforms = {
u_time: { type: 'f', value: 0 },
u_mouse: { type: 'v2', value: new THREE.Vector2() },
u_resolution: { type: 'v2', value: new THREE.Vector2(window.innerWidth, window.innerHeight) },
};
this.material = new THREE.ShaderMaterial({
side: THREE.DoubleSide,
uniforms: this.uniforms,
// wireframe: true,
vertexShader: vertex,
fragmentShader: fragment,
});
this.geometry = new THREE.PlaneBufferGeometry(1, 1, 1, 1);
this.mesh = new THREE.Mesh(this.geometry, this.material);
this.scene.add(this.mesh);
}
updateMousePositions() {
this.uniforms.u_mouse.value.x = (event.clientX / window.innerWidth) * 2 - 1;
this.uniforms.u_mouse.value.y = -(event.clientY / window.innerHeight) * 2 + 1;
}
onWindowResize() {
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.uniforms.u_resolution.value.x = this.renderer.domElement.width;
this.uniforms.u_resolution.value.y = this.renderer.domElement.height;
}
animate() {
requestAnimationFrame(this.animate.bind(this));
this.render();
}
render() {
this.uniforms.u_time.value += 0.05;
this.renderer.render(this.scene, this.camera);
}
eventListeners() {
window.addEventListener('resize', this.onWindowResize, false);
document.addEventListener('mousemove', this.updateMousePositions, false);
}
}
const threeScene = new THREESceen();
threeScene.setup();
threeScene.animate();