tela.js 1.1.14 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tela.js",
3
- "version": "1.1.14",
3
+ "version": "1.2.1",
4
4
  "author": "Pedroth",
5
5
  "repository": {
6
6
  "type": "git",
@@ -113,7 +113,7 @@ export default class Camera {
113
113
  return Promise
114
114
  .allSettled(rayMapWorkers(this, scene, canvas, lambdaWithRays, vars, dependencies))
115
115
  .then(() => {
116
- return canvas.paint();
116
+ return canvas;
117
117
  })
118
118
  }
119
119
  }
@@ -143,7 +143,7 @@ export default class Camera {
143
143
  return Promise
144
144
  .allSettled(rayTraceWorkers(this, scene, canvas, params))
145
145
  .then(() => {
146
- return canvas.paint();
146
+ return canvas;
147
147
  })
148
148
  }
149
149
  }
package/src/IO/IO.js CHANGED
@@ -80,7 +80,7 @@ export function saveImageStreamToVideo(fileAddress, streamWithImages, { imageGet
80
80
  return {
81
81
  while: async streamStatePredicate => {
82
82
  console.log("Generating video...");
83
- let s = streamWithImages;
83
+ let s = typeof streamWithImages === 'function' ? await streamWithImages() : streamWithImages;
84
84
  while (streamStatePredicate(s.head)) {
85
85
  const image = imageGetter(s.head);
86
86
  writeFileSync(`${fileName}_${ite++}.ppm`, createPPMFromImage(image));
@@ -147,7 +147,7 @@ export function saveParallelImageStreamToVideo(fileAddress, parallelStreamOfImag
147
147
  })
148
148
  return Promise.all(promises)
149
149
  .then(() => {
150
- execSync(`ffmpeg -framerate ${fps} -i ${fileName}_%d.ppm ${fileName}.${extension}`);
150
+ execSync(`ffmpeg -framerate ${fps} -i ${fileName}_%d.ppm -y ${fileName}.${extension}`);
151
151
  for (let i = 0; i < n; i++) {
152
152
  unlinkSync(`${fileName}_${i}.ppm`);
153
153
  }
@@ -39,8 +39,8 @@ export default class BScene extends NaiveScene {
39
39
  return this.boundingBoxScene.interceptWithRay(ray, level);
40
40
  }
41
41
 
42
- distanceOnRay(ray) {
43
- return this.boundingBoxScene.distanceOnRay(ray);
42
+ distanceOnRay(ray, combineLeafs = Math.min) {
43
+ return this.boundingBoxScene.distanceOnRay(ray, combineLeafs);
44
44
  }
45
45
 
46
46
  getElementNear(p) {
@@ -158,9 +158,9 @@ class Node {
158
158
  return children[index].distanceToPoint(p);
159
159
  }
160
160
 
161
- distanceOnRay(ray) {
161
+ distanceOnRay(ray, combineLeafs) {
162
162
  if (this.left.isLeaf && this.right.isLeaf) {
163
- return Math.min(
163
+ return combineLeafs(
164
164
  this.left.distanceToPoint(ray.init),
165
165
  this.right.distanceToPoint(ray.init)
166
166
  );
@@ -172,9 +172,9 @@ class Node {
172
172
  const second = leftT > rightT ? this.left : this.right;
173
173
  const firstT = Math.min(leftT, rightT);
174
174
  const secondT = Math.max(leftT, rightT);
175
- const firstHit = first.distanceOnRay(ray, firstT);
175
+ const firstHit = first.isLeaf ? first.distanceToPoint(ray.init) : first.distanceOnRay(ray, combineLeafs);
176
176
  if (firstHit < secondT) return firstHit;
177
- const secondHit = second.distanceOnRay(ray, secondT);
177
+ const secondHit = second.isLeaf ? second.distanceToPoint(ray.init) : second.distanceOnRay(ray, combineLeafs);
178
178
  return secondHit <= firstHit ? secondHit : firstHit;
179
179
  }
180
180
 
package/src/Tela/Tela.js CHANGED
@@ -41,7 +41,7 @@ export default class Tela {
41
41
  this.image[k + 2] = color.blue;
42
42
  this.image[k + 3] = color.alpha;
43
43
  }
44
- return this.paint();
44
+ return this;
45
45
  }
46
46
 
47
47
  mapBox(lambda, box) {
@@ -64,7 +64,7 @@ export default class Tela {
64
64
  return Promise
65
65
  .allSettled(workersPromises)
66
66
  .then(() => {
67
- return this.paint();
67
+ return this;
68
68
  })
69
69
  }
70
70
  }
@@ -140,6 +140,24 @@ export default class Tela {
140
140
  return drawConvexPolygon(this, [x1, x2, x3], shader);
141
141
  }
142
142
 
143
+ drawCircle(center, radius, shader) {
144
+ const box = new Box(center.sub(Vec2(radius, radius)), center.add(Vec2(radius, radius)));
145
+ const intersection = this.box.intersection(box);
146
+ if (intersection.isEmpty) return;
147
+ const [xMin, yMin] = intersection.min.map(Math.floor).toArray();
148
+ const [xMax, yMax] = intersection.max.map(Math.floor).toArray();
149
+ for (let x = xMin; x < xMax; x++) {
150
+ for (let y = yMin; y < yMax; y++) {
151
+ const d = center.sub(Vec2(x, y)).length();
152
+ if (d > radius) continue;
153
+ const color = shader(x, y);
154
+ if (!color) continue;
155
+ this.setPxl(x, y, color);
156
+ }
157
+ }
158
+ return this;
159
+ }
160
+
143
161
  grid2canvas(i, j) {
144
162
  const h = this.height;
145
163
  const x = j;
@@ -184,7 +202,7 @@ export default class Tela {
184
202
  this.image[k + 3] = this.image[k + 3] + (color.alpha - this.image[k + 3]) / it;
185
203
  }
186
204
  if (it < time) it++
187
- return this.paint();
205
+ return this;
188
206
  }
189
207
 
190
208
  ans.setPxl = (x, y, color) => {
@@ -96,10 +96,11 @@ export class MyWorker {
96
96
  constructor(path) {
97
97
  try {
98
98
  if (IS_NODE) {
99
- const workerPath = "/" + (import.meta.dirname).split('/').slice(1, -1).join('/');
99
+ let workerPath = "/" + (import.meta.dirname).split('/').slice(1, -1).join('/');
100
+ if (workerPath === "/") workerPath = "\\" + (import.meta.dirname).split('\\').slice(1, -1).join('\\')
100
101
  this.worker = new __Worker(`${workerPath}/${path}`, { type: "module" });
101
102
  } else {
102
- let workerPath = `${SOURCE}/src/${path}`;
103
+ const workerPath = `${SOURCE}/src/${path}`;
103
104
  this.worker = new __Worker(`${workerPath}`, { type: "module" });
104
105
  }
105
106
  } catch (e) {
@@ -17,4 +17,21 @@ export function video(file, lambda, { width = 640, height = 480, FPS = 25 }) {
17
17
  }
18
18
  );
19
19
  return saveImageStreamToVideo(file, stream, { fps: FPS });
20
+ }
21
+
22
+ export function videoAsync(file, lambda, { width = 640, height = 480, FPS = 25 }) {
23
+ const dt = 1 / FPS;
24
+ const lazyStream = async () => new Stream(
25
+ {
26
+ time: 0,
27
+ image: await lambda({ time: 0, image: Image.ofSize(width, height) })
28
+ },
29
+ async ({ time, image }) => {
30
+ return {
31
+ time: time + dt,
32
+ image: await lambda({ time, image })
33
+ }
34
+ }
35
+ );
36
+ return saveImageStreamToVideo(file, lazyStream, { fps: FPS });
20
37
  }