tela.js 1.2.9 → 1.2.10
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 +1 -1
- package/src/Geometry/Triangle.js +50 -20
- package/src/Tela/telaWorker.js +3 -1
package/package.json
CHANGED
package/src/Geometry/Triangle.js
CHANGED
|
@@ -57,7 +57,7 @@ export default class Triangle {
|
|
|
57
57
|
distanceToPoint(p) {
|
|
58
58
|
let alpha = this.getBarycentricCoords(p).map(x => Math.max(0, x));
|
|
59
59
|
const sum = alpha.fold((e, x) => e + x, 0);
|
|
60
|
-
|
|
60
|
+
|
|
61
61
|
// if (sum === 1) {
|
|
62
62
|
// const r0 = p.sub(this.positions[0]);
|
|
63
63
|
// const r1 = p.sub(this.positions[1]);
|
|
@@ -79,28 +79,58 @@ export default class Triangle {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
normalToPoint(p) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
82
|
+
if (this.radius === 0) {
|
|
83
|
+
const r = p.sub(this.positions[0]);
|
|
84
|
+
const dot = this.faceNormal.dot(r);
|
|
85
|
+
return dot < 1e-3 ? this.faceNormal : this.faceNormal.scale(-1);
|
|
86
|
+
}
|
|
87
|
+
const epsilon = 1e-6;
|
|
88
|
+
const f = this.distanceToPoint(p);
|
|
89
|
+
const sign = Math.sign(f);
|
|
90
|
+
const grad = Vec3(
|
|
91
|
+
this.distanceToPoint(p.add(Vec3(epsilon, 0, 0))) - f,
|
|
92
|
+
this.distanceToPoint(p.add(Vec3(0, epsilon, 0))) - f,
|
|
93
|
+
this.distanceToPoint(p.add(Vec3(0, 0, epsilon))) - f,
|
|
94
|
+
).normalize();
|
|
95
|
+
return grad.scale(sign);
|
|
86
96
|
}
|
|
87
97
|
|
|
88
98
|
interceptWithRay(ray) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
if(this.radius === 0) {
|
|
100
|
+
const epsilon = 1e-9
|
|
101
|
+
const v = ray.dir;
|
|
102
|
+
const p = ray.init.sub(this.positions[0]);
|
|
103
|
+
const n = this.faceNormal;
|
|
104
|
+
const t = - n.dot(p) / n.dot(v);
|
|
105
|
+
if (t <= epsilon) return;
|
|
106
|
+
const x = ray.trace(t);
|
|
107
|
+
for (let i = 0; i < this.positions.length; i++) {
|
|
108
|
+
const xi = this.positions[i];
|
|
109
|
+
const u = x.sub(xi);
|
|
110
|
+
const ni = n.cross(this.edges[i]);
|
|
111
|
+
const dot = ni.dot(u);
|
|
112
|
+
if (dot <= epsilon) return;
|
|
113
|
+
}
|
|
114
|
+
return [t - epsilon, x, this];
|
|
115
|
+
}
|
|
116
|
+
const maxIte = 20;
|
|
117
|
+
const epsilon = 1e-3;
|
|
118
|
+
let p = ray.init;
|
|
119
|
+
let t = this.distanceToPoint(p);
|
|
120
|
+
let minT = t;
|
|
121
|
+
for (let i = 0; i < maxIte; i++) {
|
|
122
|
+
p = ray.trace(t);
|
|
123
|
+
const d = this.distanceToPoint(p);
|
|
124
|
+
t += d;
|
|
125
|
+
if (d < epsilon) {
|
|
126
|
+
return [t, p, this];
|
|
127
|
+
}
|
|
128
|
+
if (d > minT) {
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
minT = d;
|
|
102
132
|
}
|
|
103
|
-
return
|
|
133
|
+
return;
|
|
104
134
|
}
|
|
105
135
|
|
|
106
136
|
sample() {
|
|
@@ -153,7 +183,7 @@ class TriangleBuilder {
|
|
|
153
183
|
this._colors = indx.map(() => Color.BLACK);
|
|
154
184
|
this._positions = indx.map(() => Vec3());
|
|
155
185
|
this._texCoords = [Vec2(), Vec2(1, 0), Vec2(0, 1)];
|
|
156
|
-
this._radius =
|
|
186
|
+
this._radius = 0.0;
|
|
157
187
|
this._emissive = false;
|
|
158
188
|
this._material = Diffuse();
|
|
159
189
|
}
|
package/src/Tela/telaWorker.js
CHANGED
|
@@ -3,6 +3,8 @@ import { memoize } from "../Utils/Utils.js";
|
|
|
3
3
|
// necessary to load this even though is not used in the code, it might be used in the eval.
|
|
4
4
|
import Color from "../Color/Color.js";
|
|
5
5
|
import Box from "../Geometry/Box.js";
|
|
6
|
+
import Triangle from "../Geometry/Triangle.js";
|
|
7
|
+
import Line from "../Geometry/Line.js";
|
|
6
8
|
import Vec, { Vec2, Vec3 } from "../Vector/Vector.js";
|
|
7
9
|
import Camera from "../Camera/Camera.js";
|
|
8
10
|
import Ray from "../Ray/Ray.js";
|
|
@@ -27,7 +29,7 @@ async function main(inputs) {
|
|
|
27
29
|
for (let i = __startRow; i < __endRow; i++) {
|
|
28
30
|
for (let x = 0; x < __width; x++) {
|
|
29
31
|
const y = __height - 1 - i;
|
|
30
|
-
const color = func(x, y, __vars);
|
|
32
|
+
const color = await func(x, y, __vars);
|
|
31
33
|
if (!color) continue;
|
|
32
34
|
image[index++] = color.red;
|
|
33
35
|
image[index++] = color.green;
|