-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (118 loc) · 3.8 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
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
// Ensure ThreeJS is in global scope for the 'examples/'
global.THREE = require("three");
const canvasSketch = require("canvas-sketch");
import frag from "./fragment.glsl";
import vert from "./vertex.glsl";
const matcap_shiny_red = "./resources/matcap_shiny_red.jpeg";
const matcap_pink = "./resources/matcap_pink.jpeg";
const matcap_green = "./resources/matcap_green.png";
const matcap_angel = "./resources/matcap_angel.png";
const matcap_solar = "./resources/matcap_solar.png";
const lerp = (a, b, t) => {
return a + (b - a) * t;
};
const settings = {
// Make the loop animated
animate: true,
// Get a WebGL canvas rather than 2D
context: "webgl",
};
const sketch = ({ context }) => {
// Create a renderer
const renderer = new THREE.WebGLRenderer({
canvas: context.canvas,
});
const textureLoader = new THREE.TextureLoader();
// WebGL background color
renderer.setClearColor("#000", 1);
const frustumSize = 10;
// Setup a camera
const camera = new THREE.OrthographicCamera(
frustumSize / -2,
frustumSize / 2,
frustumSize / 2,
frustumSize / -2,
-1000,
1000
);
camera.position.set(0, 0, -5);
camera.lookAt(new THREE.Vector3());
// Setup your scene
const scene = new THREE.Scene();
// Setup a geometry
const geometry = new THREE.PlaneGeometry(frustumSize, frustumSize, 1, 1);
const sphere1 = new THREE.Vector2();
const sphere2 = new THREE.Vector2();
const sphere3 = new THREE.Vector2();
const mouse = new THREE.Vector2();
// Setup a material
const material = new THREE.ShaderMaterial({
vertexShader: vert,
fragmentShader: frag,
uniforms: {
resolution: { value: new THREE.Vector4() },
sphere1: { value: sphere1 },
sphere2: { value: sphere2 },
sphere3: { value: sphere3 },
// mbIterations: 1,
mbBailout: 6.0,
mbPower: 6.0,
time: { value: 0.0 },
matcap: {
value: textureLoader.load(matcap_angel),
},
},
side: THREE.DoubleSide,
});
material.uniformsNeedUpdate = true;
// Setup a mesh with geometry + material
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
// draw each frame
return {
// Handle resize events here
resize({ pixelRatio, viewportWidth, viewportHeight }) {
renderer.setPixelRatio(pixelRatio);
renderer.setSize(viewportWidth, viewportHeight, false);
camera.aspect = viewportWidth / viewportHeight;
camera.updateProjectionMatrix();
const mouseListener = (e) => {
mouse.x = (e.pageX / viewportWidth) * 2 - 1;
mouse.y = (e.pageY / viewportHeight) * 2 - 1;
};
window.removeEventListener("mousemove", mouseListener);
window.addEventListener("mousemove", mouseListener);
if (material.uniforms.resolution) {
material.uniforms.resolution.value.x = viewportWidth;
material.uniforms.resolution.value.y = viewportHeight;
let a1;
let a2;
if (viewportHeight / viewportWidth > 1) {
a1 = viewportWidth / viewportHeight;
a2 = 1;
} else {
a1 = 1;
a2 = viewportHeight / viewportWidth;
}
material.uniforms.resolution.value.z = a1;
material.uniforms.resolution.value.w = a2;
}
},
// Update & render your scene here
render({ time }) {
material.uniforms.time.value = time;
sphere1.x = lerp(sphere1.x, mouse.x, 0.05);
sphere1.y = lerp(sphere1.y, mouse.y, 0.05);
sphere2.x = lerp(sphere2.x, sphere1.x, 0.1);
sphere2.y = lerp(sphere2.y, sphere1.y, 0.1);
sphere3.x = lerp(sphere3.x, sphere2.x, 0.1);
sphere3.y = lerp(sphere3.y, sphere2.y, 0.1);
renderer.render(scene, camera);
},
// Dispose of events & renderer for cleaner hot-reloading
unload() {
renderer.dispose();
},
};
};
canvasSketch(sketch, settings);