-
Notifications
You must be signed in to change notification settings - Fork 1
/
feb20.js
179 lines (157 loc) · 4.62 KB
/
feb20.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
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
172
173
174
175
176
177
178
179
const canvasSketch = require("canvas-sketch");
const random = require("canvas-sketch-util").random;
const palettes = require("nice-color-palettes");
const THREE = require("three");
const settings = {
// Make the loop animated
animate: true,
// Get a WebGL canvas rather than 2D
context: "webgl",
// Turn on MSAA
attributes: { antialias: true }
};
const sketch = ({ context }) => {
// Create a renderer
const renderer = new THREE.WebGLRenderer({
context
});
const camera = new THREE.PerspectiveCamera(
50,
context.drawingBufferWidth / context.drawingBufferHeight,
0.01,
100
);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x8d0a55);
scene.fog = new THREE.FogExp2(0x8d0a55, 0.1);
const palette = random.pick(palettes);
const startDepth = 30;
const endDepth = -5;
const meshes = [];
const particles = [];
let lastTime;
scene.add(new THREE.AmbientLight("#cfcfcf"));
const light = new THREE.DirectionalLight("#fff", 1.6);
light.position.set(-1, 0, -4);
light.castShadow = true;
scene.add(light);
const cylinder = new THREE.CylinderBufferGeometry(1, 1, 0.3, 7, 1, true);
const material = new THREE.MeshLambertMaterial({
side: THREE.DoubleSide,
color: "#FBD600",
});
const makeRing = (offsetX, offsetY) => {
const mesh = new THREE.Mesh(cylinder, material);
mesh.rotation.set(90, 0, 0);
mesh.position.set(offsetX, offsetY, startDepth);
mesh.receiveShadow = true;
scene.add(mesh);
meshes.push(mesh);
};
const makeParticle = () => {
var particle = new THREE.Object3D();
var geometry = new THREE.TetrahedronGeometry(1, 0);
var material = new THREE.MeshPhongMaterial({
color: random.pick(palette),
flatShading:true
});
for (var i = 0; i < Math.random() * 20; i++) {
var mesh = new THREE.Mesh(geometry, material);
mesh.position
.set(Math.random() - 0.5, Math.random() - 0.5, Math.random() + 5)
.normalize();
let meshScale = Math.random() * 0.1;
mesh.scale.set(meshScale, meshScale, meshScale);
mesh.position.multiplyScalar(10 + Math.random() * 10);
mesh.rotation.set(
Math.random() * 2,
Math.random() * 2,
Math.random() * 2
);
mesh.castShadow = true;
particle.add(mesh);
particles.push(mesh);
}
scene.add(particle);
};
makeParticle();
const makeSky = () => {
var geometry = new THREE.CylinderBufferGeometry(30, 30, (context.drawingBufferWidth / context.drawingBufferHeight) * 70, 32, 1, true);
var material = new THREE.ShaderMaterial({
uniforms: {
color1: {
value: new THREE.Color("#003A76")
},
color2: {
value: new THREE.Color("#E60078")
}
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}
`,
fragmentShader: `
uniform vec3 color1;
uniform vec3 color2;
varying vec2 vUv;
void main() {
gl_FragColor = vec4(mix(color1, color2, vUv.y), 1.0);
}
`,
side: THREE.DoubleSide,
wireframe: false
});
var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
};
makeSky();
// draw each frame
return {
// Handle resize events here
resize({ pixelRatio, viewportWidth, viewportHeight }) {
renderer.setPixelRatio(pixelRatio);
renderer.setSize(viewportWidth, viewportHeight);
camera.position.set(0, 0, -5);
camera.lookAt(0, 0, -1);
},
// Update & render your scene here
render({ time }) {
for (let i = 0; i < meshes.length; i++) {
const mesh = meshes[i];
mesh.position.z += -0.05;
if (mesh.position.z <= endDepth) {
meshes.splice(i, 1);
scene.remove(mesh);
}
}
for (let i = 0; i < particles.length; i++) {
const particle = particles[i];
particle.position.z += -0.02;
particle.rotation.x += 0.01;
particle.rotation.y += 0.02;
if (particle.position.z < endDepth) {
particles.splice(i, 1);
scene.remove(particle);
}
}
if (!lastTime) {
lastTime = time;
}
if (time - lastTime > 0.8) {
makeRing(Math.sin(time) * 0.3, Math.sin(time) * 0.3);
makeParticle();
lastTime = time;
}
camera.rotation.z += 0.001;
renderer.render(scene, camera);
},
// Dispose of events & renderer for cleaner hot-reloading
unload() {
renderer.dispose();
}
};
};
canvasSketch(sketch, settings);