-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgptscript.js
123 lines (102 loc) · 3.65 KB
/
gptscript.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
const exec = op.inTrigger("Exec"),
inArray1 = op.inArray("Letter Pos", null, 3),
outArray = op.outArray("Array out", null, 3),
inMaxLifetime = op.inValue("Lifetime", 10.0),
inRandomize = op.inBool("Randomize", false),
inMaxDistance = op.inValue("Max Distance", 1.0),
inSquareSize = op.inValue("Square Size", 200.0);
distanceArr = [];
var particles = [];
var speeds = [];
outArray.set(particles);
let showingError = false;
// Define centerX, centerY, and scale
const centerX = 0.5;
const centerY = 0.5;
const scale = 0.5;
exec.onTriggered = update;
function update() {
let squareSize = inSquareSize.get();
let letterPos = inArray1.get();
let numParticles = Math.floor(letterPos.length / 3);
numParticles -= numParticles % 3; // Round down to nearest multiple of 3
distanceArr.length = numParticles / 3;
distanceArr.length = 0;
if (!letterPos) {
outArray.set(null);
return;
}
if (distanceArr.length != letterPos.length) {
distanceArr.length = letterPos.length;
}
let divisibleBy3 = distanceArr.length % 3 === 0;
distanceArr.length = Math.floor(letterPos.length / 3);
if (divisibleBy3 === false) {
op.setUiError("arraytriple", "Arrays length not divisible by 3 !");
} else {
op.setUiError("arraytriple", null);
}
if (particles.length != distanceArr.length) {
particles.length = distanceArr.length;
speeds.length = particles.length;
for (var i = 0; i < particles.length; i++) {
particles[i] = 1.1;
}
}
if (inRandomize.get()) {
for (let i = 0; i < distanceArr.length; i++) {
particles[i] = -0.1;
speeds[i] = Math.random() / 10 + 0.1;
letterPos[i * 3 + 0] = Math.random() * 2 - 1;
letterPos[i * 3 + 1] = Math.random() * 2 - 1;
letterPos[i * 3 + 2] = Math.random() * 2 - 1;
}
}
for (let i = 0; i < distanceArr.length; i++) {
let dx = letterPos[i * 3 + 0] - centerX;
let dy = letterPos[i * 3 + 1] - centerY;
let dist = Math.sqrt(dx * dx + dy * dy);
distanceArr[i] = 0.0;
if (!inRandomize.get() && particles[i] == 0.0) {
particles[i] = -0.1;
speeds[i] = Math.random() / 10 + 0.1;
}
let distance = Math.sqrt(Math.pow(letterPos[i * 3 + 0] - centerX, 2) + Math.pow(letterPos[i * 3 + 1] - centerY, 2) + Math.pow(letterPos[i * 3 + 2], 2));
if (distance <= inMaxDistance.get()) {
if (inMaxDistance.get() < 0.0) {
if (particles[i] > -10) {
particles[i] = Math.max(particles[i] - Math.abs(inMaxDistance.get()) / inMaxLifetime.get(), -10);
}
for (let i = 0; i < distanceArr.length; i++) {
let dx = letterPos[i * 3 + 0] - centerX;
let dy = letterPos[i * 3 + 1] - centerY;
let dist = Math.sqrt(dx * dx + dy * dy);
distanceArr[i] = 0.0;
if (!inRandomize.get() && particles[i] == 0.0) {
particles[i] = -0.1;
speeds[i] = Math.random() / 10 + 0.1;
}
}
} else if (particles[i] < 0.0) {
particles[i] -= speeds[i];
}
if (particles[i] < -inMaxLifetime.get() || particles[i] > 0.0) {
particles[i] = 0.0;
}
} else if (inMaxDistance.get() < 0.0) {
particles[i] = 1.1;
}
if (dist < scale) {
// Move particle towards square
let factor = (scale - dist) / scale;
letterPos[i * 3 + 0] += dx * factor * 0.1;
letterPos[i * 3 + 1] += dy * factor * 0.1;
} else {
// Move particle towards origin
letterPos[i * 3 + 0] -= letterPos[i * 3 + 0] * 0.05;
letterPos[i * 3 + 1] -= letterPos[i * 3 + 1] * 0.05;
}
}
outArray.set(null);
outArray.set(particles);
}