-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
171 lines (133 loc) · 4.23 KB
/
main.js
File metadata and controls
171 lines (133 loc) · 4.23 KB
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
import "./style.css";
import * as THREE from "three";
import {OrbitControls} from "three/examples/jsm/controls/OrbitControls.js";
import {Pane} from "tweakpane";
import gsap from "gsap";
import fragment from "./shaders/fragment.glsl";
import vertex from "./shaders/vertex.glsl";
import fox1 from "./img/fox0.png";
import fox2 from "./img/fox1.png";
export default class Sketch {
constructor(options) {
this.scene = new THREE.Scene();
this.container = options.dom;
this.width = this.container.offsetWidth;
this.height = this.container.offsetHeight;
this.renderer = new THREE.WebGLRenderer({antialias: true});
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
this.renderer.setSize(this.width, this.height);
this.renderer.setClearColor("#111111");
this.container.appendChild(this.renderer.domElement);
/*
CAMERAS
*/
// Perspective Camera
this.camera = new THREE.PerspectiveCamera(
70,
this.width / this.height,
0.01,
1000
);
this.camera.position.set(0, 0, 1.3);
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
this.controls.enableDamping = true;
this.controls.dampingFactor = 0.05;
/*
THINGS
*/
this.clock = new THREE.Clock();
this.uMouse = new THREE.Vector2(0, 0);
this.prevMouse = new THREE.Vector2(0, 0);
this.settings();
this.addObjects();
this.resize();
this.setupResize();
this.mouseMovement();
this.render();
}
settings() {
const pane = new Pane();
this.parameters = {
uProgress: 0,
};
const f1 = pane.addFolder({title: "Basic"});
f1.addInput(this.parameters, "uProgress", {min: 0, max: 1, step: 0.01});
}
setupResize() {
window.addEventListener("resize", this.resize.bind(this));
}
resize() {
this.width = this.container.offsetWidth;
this.height = this.container.offsetHeight;
this.renderer.setSize(this.width, this.height);
this.camera.aspect = this.width / this.height;
// ASPECTS
// Here we define the aspect
this.imageAspect = 853 / 1280;
let a1;
let a2;
if (this.height / this.width > this.imageAspect) {
a1 = (this.width / this.height) * this.imageAspect;
a2 = 1;
} else {
a1 = 1;
a2 = this.height / this.width / this.imageAspect;
}
this.material.uniforms.uResolution.value.x = this.width;
this.material.uniforms.uResolution.value.y = this.height;
this.material.uniforms.uResolution.value.z = a1;
this.material.uniforms.uResolution.value.w = a2;
this.camera.updateProjectionMatrix();
}
addObjects() {
this.geometry = new THREE.PlaneBufferGeometry(1, 1, 1, 1);
this.material = new THREE.ShaderMaterial({
uniforms: {
uTime: {value: 0},
uMouse: {value: new THREE.Vector2(0, 0)},
uProgress: {value: 0},
uTexture1: {value: new THREE.TextureLoader().load(fox1)},
uTexture2: {value: new THREE.TextureLoader().load(fox2)},
uResolution: {value: new THREE.Vector4()},
},
fragmentShader: fragment,
vertexShader: vertex,
side: THREE.DoubleSide,
wireframe: false,
});
this.plane = new THREE.Mesh(this.geometry, this.material);
this.scene.add(this.plane);
}
mouseMovement() {
window.addEventListener("mousemove", (event) => {
this.uMouse.x = event.clientX / this.width;
this.uMouse.y = 1 - event.clientY / this.height;
this.material.uniforms.uMouse.value = this.uMouse;
});
window.addEventListener("mousedown", (e) => {
gsap.to(this.parameters, {
duration: 1,
uProgress: 1,
ease: "power3.out",
});
if (this.parameters.uProgress === 1) {
gsap.to(this.parameters, {
duration: 1,
uProgress: 0,
ease: "power3.out",
});
}
});
}
render() {
this.elapsedTime = this.clock.getElapsedTime();
this.material.uniforms.uTime.value = this.elapsedTime;
this.material.uniforms.uProgress.value = this.parameters.uProgress;
this.controls.update();
this.renderer.render(this.scene, this.camera);
window.requestAnimationFrame(this.render.bind(this));
}
}
new Sketch({
dom: document.getElementById("container"),
});