-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderScript.js
More file actions
114 lines (82 loc) · 2.3 KB
/
Copy pathrenderScript.js
File metadata and controls
114 lines (82 loc) · 2.3 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
function generate_cloth_mesh(x, y, w, h, nx = 10, ny = 10) {
const base = pointCount;
const ux = w / (nx - 1);
const uy = h / (ny - 1);
for (let r = 0; r < ny; r++) {
for (let c = 0; c < nx; c++) {
ADD_POINT(x + ux * c, y + uy * r, r == 0)
}
}
const index = (r, c) => base + r * nx + c;
for (let r = 0; r < ny; r++) {
for (let c = 0; c < nx; c++) {
if (r > 0) {
ADD_EDGE(index(r - 1, c), index(r, c))
}
if (c > 0) {
ADD_EDGE(index(r, c - 1), index(r, c))
}
/*
if (c > 0 && r > 0) {
ADD_EDGE(index(r - 1, c - 1), index(r, c))
}
*/
}
}
}
function DRAW_EDGE(i) {
const p1 = edgeA[i]
const p2 = edgeB[i]
const p1x = posX[p1]
const p1y = posY[p1]
const p2x = posX[p2]
const p2y = posY[p2]
drawLine(p1x, p1y, p2x, p2y)
}
function DRAW_ALL() {
for (let i = 0; i < edgeCount; i++) {
DRAW_EDGE(i);
}
}
function POINT_RECT(px, py, x1, y1, x2, y2, dist = 5) {
const pad = dist;
return (
px >= Math.min(x1, x2) - pad &&
px <= Math.max(x1, x2) + pad &&
py >= Math.min(y1, y2) - pad &&
py <= Math.max(y1, y2) + pad
);
}
function POINT_SEG_DIST(px, py, ax, ay, bx, by) {
const dx = bx - ax;
const dy = by - ay;
const lenSq = dx * dx + dy * dy;
if (lenSq === 0) return Infinity;
let t = ((px - ax) * dx + (py - ay) * dy) / lenSq;
t = Math.max(0, Math.min(1, t));
const cx = ax + t * dx;
const cy = ay + t * dy;
const ddx = px - cx;
const ddy = py - cy;
return ddx * ddx + ddy * ddy; // THIS IS SQUARED
}
function CAN_SLICE_EDGE(i, dist = 5) {
const p1 = edgeA[i]
const p2 = edgeB[i]
const p1x = posX[p1]
const p1y = posY[p1]
const p2x = posX[p2]
const p2y = posY[p2]
return POINT_RECT(mouseX, mouseY, p1x, p1y, p2x, p2y) && POINT_SEG_DIST(mouseX, mouseY, p1x, p1y, p2x, p2y) <= dist * dist
}
function HANDLE_SLICING_EDGES() {
const toSplit = [];
for (let i = 0; i < edgeCount; i++) {
if (CAN_SLICE_EDGE(i)) {
toSplit.push(i);
}
}
for (let i = toSplit.length - 1; i >= 0; i--) {
SPLIT_EDGE(toSplit[i]);
}
}