-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
218 lines (203 loc) · 6.82 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import * as THREE from "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js";
import createSpiro from "./createSpiro.js";
import patterns from "./patterns.js";
import { EffectComposer } from "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/RenderPass.js";
import { HalftonePass } from "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/HalftonePass.js";
import { OrbitControls } from "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/OrbitControls.js";
console.log(`THREE REVISION: %c${THREE.REVISION}`, "color: #FFFF00");
const w = window.innerWidth;
const h = window.innerHeight;
const globalPadding = 250;
const size = Math.min(w, h) - globalPadding;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, size / size, 0.1, 1000);
camera.position.set(0, 0, 4);
const renderer = new THREE.WebGLRenderer({
alpha: false,
preserveDrawingBuffer: true,
});
let bgColor = 0x101010;
let patternIndex = 1;
let masterHue = 0;
let canvasSizeMultiplier = { x: 1.0, y: 1.0 };
let enableRenderToFile = false;
renderer.setClearColor(bgColor);
renderer.setSize(size, size);
renderer.domElement.id = "three-canvas";
document.body.appendChild(renderer.domElement);
window.camera = camera;
// controls
const controls = new OrbitControls(camera, renderer.domElement);
controls.update();
// composer
const renderScene = new RenderPass(scene, camera);
const htParams = {
shape: 1,
radius: 3,
blending: 0.25,
scatter: 1.0,
blendingMode: 1, // Multiply = 2
};
const halftonePass = new HalftonePass(w, h, htParams);
const composer = new EffectComposer(renderer);
composer.addPass(renderScene);
// composer.addPass(halftonePass);
// canvas texture
function getTexture({ hue, path, saturation = 100, lightness = 50 }) {
const size = 1024;
const ctx = document.createElement("canvas").getContext("2d");
ctx.canvas.width = size;
ctx.canvas.height = size;
ctx.fillStyle = `hsl(${hue}, ${saturation}%, ${lightness}%, 1)`;
ctx.fill(path);
const texture = new THREE.CanvasTexture(ctx.canvas);
return texture;
}
function getPlane({ map, index, rotation, blending }) {
const size = 6.5;
const geometry = new THREE.PlaneGeometry(size, size, 1, 1);
const material = new THREE.MeshBasicMaterial({
map,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.2,
blending // Math.random() < 0.8 ? blending : THREE.AdditiveBlending,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.z = 0.01 * index;
mesh.rotation.z = (rotation * Math.PI) / 180;
const rate = 0.1 * index;
function update(t) {
mesh.rotation.z = Math.cos(t + rate) * 0.5;
}
return { mesh, update };
}
function getTexturedPlane(options) {
const { blending, hue, index, rotation, saturation, lightness } = options;
const spiro = createSpiro(options);
const tex = getTexture({ path: spiro, hue, saturation, lightness });
const plane = getPlane({ map: tex, index, rotation, blending });
return plane;
}
const sceneGroup = new THREE.Object3D();
const planes = [];
function setupSpiros({
index = patternIndex,
hue = masterHue,
blending = THREE.NormalBlending,
}) {
sceneGroup.remove.apply(sceneGroup, sceneGroup.children);
const recipes = patterns[index]();
window.currentRecipies = recipes;
let plane;
recipes.forEach((r) => {
r.hue += hue;
r.blending = blending;
plane = getTexturedPlane(r);
planes.push(plane);
sceneGroup.add(plane.mesh);
});
scene.add(sceneGroup);
}
const timeMult = 0.001;
let imgData;
function animate(t) {
requestAnimationFrame(animate);
// planes.forEach((p) => p.update(t * timeMult));
composer.render(scene, camera);
if (enableRenderToFile === true) {
// todo
// UP-RES to 4k
imgData = renderer.domElement.toDataURL("image/jpeg", 1.0);
enableRenderToFile = false;
const link = document.createElement("a");
link.setAttribute("href", imgData);
link.setAttribute("target", "_blank");
link.setAttribute("download", "test.jpg");
link.click();
}
}
setupSpiros({ index: patternIndex });
setupControls();
animate(0);
function resizeWindow() {
const newSize = Math.min(w, h) - globalPadding;
renderer.setSize(
newSize * canvasSizeMultiplier.x,
newSize * canvasSizeMultiplier.y
);
let size = renderer.getSize(new THREE.Vector2());
camera.aspect = size.x / size.y;
camera.updateProjectionMatrix();
}
function handleWindowResize() {
resizeWindow();
}
window.addEventListener("resize", handleWindowResize, false);
function setupControls() {
const cSlider = document.querySelector("#canvasSize");
const cOutput = document.querySelector("#canvasSize-output");
const canvasSizeRatios = [
{ label: "2:1", mult: { x: 1.0, y: 0.5 } },
{ label: "4:3", mult: { x: 1.0, y: 0.75 } },
{ label: "1:1", mult: { x: 1.0, y: 1.0 } },
{ label: "3:4", mult: { x: 0.75, y: 1.0 } },
{ label: "1:2", mult: { x: 0.5, y: 1.0 } },
];
cSlider.addEventListener("input", (evt) => {
const { target } = evt;
const { value } = target;
const ratio = canvasSizeRatios[value];
canvasSizeMultiplier = ratio.mult;
cOutput.textContent = ratio.label;
resizeWindow();
});
const pSlider = document.querySelector("#pattern");
const pOutput = document.querySelector("#pattern-output");
pSlider.addEventListener("input", (evt) => {
const { target } = evt;
const { value } = target;
pOutput.textContent = value;
patternIndex = +value;
setupSpiros({ index: patternIndex });
});
const hSlider = document.querySelector("#hue");
const hOutput = document.querySelector("#hue-output");
hSlider.addEventListener("input", (evt) => {
const { target } = evt;
const { value } = target;
hOutput.textContent = value;
masterHue = +value;
setupSpiros({ hue: masterHue });
});
const footer = document.querySelector("footer");
footer.addEventListener("change", (evt) => {
const { name, id } = evt.target;
if (name === "light-dark") {
if (id === "light") {
bgColor = "#F0F0F0";
}
if (id === "dark") {
bgColor = "#202020";
}
renderer.setClearColor(bgColor);
}
});
footer.addEventListener("click", (evt) => {
const { target } = evt;
const { id } = target;
if (id === "random") {
patternIndex = Math.floor(Math.random() * 12);
document.querySelector("#pattern").value = patternIndex;
document.querySelector("#pattern-output").textContent = patternIndex;
masterHue = Math.floor(Math.random() * 360);
document.querySelector("#hue").value = masterHue;
document.querySelector("#hue-output").textContent = masterHue;
setupSpiros({ index: patternIndex, hue: masterHue });
}
if (id === "print") {
enableRenderToFile = true;
}
});
}