-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
86 lines (70 loc) · 2.44 KB
/
app.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
import * as THREE from "https://cdn.skypack.dev/[email protected]";
import { OrbitControls } from "https://cdn.skypack.dev/[email protected]/examples/jsm/controls/OrbitControls.js";
import fragment from "./shaders/fragment.glsl.js";
import vertex from "./shaders/vertex.glsl.js";
// import girl from "./img/girl.jpeg";
class Sketch {
constructor(options) {
this.time = 0;
this.container = options.dom;
//create scene
this.scene = new THREE.Scene();
this.width = this.container.offsetWidth;
this.height = this.container.offsetHeight;
//create camera
this.camera = new THREE.PerspectiveCamera(
70,
this.width / this.height,
0.01,
10
);
this.camera.position.z = 1;
this.renderer = new THREE.WebGLRenderer({ antialias: true });
this.container.appendChild(this.renderer.domElement);
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
this.resize();
this.setupResize();
this.addObjects();
this.render();
}
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;
//we are saying the viewport has changed and resize based on vieport
this.camera.updateProjectionMatrix();
}
//creating shaders
addObjects() {
this.geometry = new THREE.PlaneBufferGeometry(.7, .7, 150, 150);
this.material = new THREE.MeshNormalMaterial();
//this shader material has couple of options
this.material = new THREE.ShaderMaterial({
uniforms: {
time: {value: 0},
picTexture: {value: new THREE.TextureLoader().load("https://images.pexels.com/photos/2690323/pexels-photo-2690323.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260")},
},
side: THREE.DoubleSide,
fragmentShader: fragment,
vertexShader: vertex,
// wireframe: true,
});
this.mesh = new THREE.Mesh(this.geometry, this.material);
this.scene.add(this.mesh);
}
render() {
this.time += 0.05;
this.mesh.rotation.x = this.time / 2000;
this.mesh.rotation.y = this.time / 1000;
this.material.uniforms.time.value = this.time;
this.renderer.render(this.scene, this.camera);
window.requestAnimationFrame(this.render.bind(this));
}
}
new Sketch({
dom: document.getElementById("container"),
});