bg2e-js 2.5.1 → 2.5.3
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/dist/bg2e-js.js +364 -347
- package/dist/bg2e-js.js.map +1 -1
- package/package.json +1 -1
- package/src/base/PolyList.ts +57 -25
- package/src/db/Loader.ts +1 -1
package/package.json
CHANGED
package/src/base/PolyList.ts
CHANGED
|
@@ -109,12 +109,19 @@ interface Vertex {
|
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
function buildTangents(plist: PolyList): void {
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
// `index` is a vertex index: positions live in the flat vertex array at
|
|
113
|
+
// offset index*3 and texture coordinates at offset index*2.
|
|
114
114
|
const createVertex = (index: number): Vertex => {
|
|
115
115
|
return {
|
|
116
|
-
pos: new Vec(
|
|
117
|
-
|
|
116
|
+
pos: new Vec(
|
|
117
|
+
plist.vertex[index * 3],
|
|
118
|
+
plist.vertex[index * 3 + 1],
|
|
119
|
+
plist.vertex[index * 3 + 2]
|
|
120
|
+
),
|
|
121
|
+
uv: new Vec(
|
|
122
|
+
plist.texCoord0[index * 2],
|
|
123
|
+
plist.texCoord0[index * 2 + 1]
|
|
124
|
+
)
|
|
118
125
|
}
|
|
119
126
|
}
|
|
120
127
|
|
|
@@ -123,13 +130,21 @@ function buildTangents(plist: PolyList): void {
|
|
|
123
130
|
const calcR = (uv1: Vec, uv2: Vec): number => 1.0 / (uv1.x * uv2.y - uv1.y * uv2.x);
|
|
124
131
|
|
|
125
132
|
if (plist.index.length % 3 === 0) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
133
|
+
// One tangent per vertex, parallel to the vertex array. Tangents from
|
|
134
|
+
// every triangle sharing a vertex are accumulated, then normalized.
|
|
135
|
+
const result: number[] = new Array(plist.vertex.length).fill(0);
|
|
136
|
+
|
|
137
|
+
for (let i = 0; i < plist.index.length; i += 3) {
|
|
138
|
+
const i0 = plist.index[i];
|
|
139
|
+
const i1 = plist.index[i + 1];
|
|
140
|
+
const i2 = plist.index[i + 2];
|
|
130
141
|
|
|
131
|
-
let
|
|
132
|
-
let
|
|
142
|
+
let v0 = createVertex(i0);
|
|
143
|
+
let v1 = createVertex(i1);
|
|
144
|
+
let v2 = createVertex(i2);
|
|
145
|
+
|
|
146
|
+
const edge1 = Vec.Sub(v1.pos, v0.pos);
|
|
147
|
+
const edge2 = Vec.Sub(v2.pos, v0.pos);
|
|
133
148
|
|
|
134
149
|
let uv1 = createUV(v1, v0);
|
|
135
150
|
let uv2 = createUV(v2, v0);
|
|
@@ -151,35 +166,52 @@ function buildTangents(plist: PolyList): void {
|
|
|
151
166
|
r = calcR(uv1, uv2);
|
|
152
167
|
}
|
|
153
168
|
|
|
169
|
+
if (!isFinite(r)) {
|
|
170
|
+
r = 0;
|
|
171
|
+
}
|
|
172
|
+
|
|
154
173
|
const tangent = new Vec(
|
|
155
174
|
((edge1.x * uv2.y) - (edge2.x * uv1.y)) * r,
|
|
156
175
|
((edge1.y * uv2.y) - (edge2.y * uv1.y)) * r,
|
|
157
176
|
((edge1.z * uv2.y) - (edge2.z * uv1.y)) * r
|
|
158
177
|
);
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
result.push(tangent.y);
|
|
167
|
-
result.push(tangent.z);
|
|
168
|
-
|
|
169
|
-
result.push(tangent.x);
|
|
170
|
-
result.push(tangent.y);
|
|
171
|
-
result.push(tangent.z);
|
|
178
|
+
|
|
179
|
+
// Accumulate the (unnormalized) tangent on each vertex of the triangle
|
|
180
|
+
for (const index of [i0, i1, i2]) {
|
|
181
|
+
result[index * 3] += tangent.x;
|
|
182
|
+
result[index * 3 + 1] += tangent.y;
|
|
183
|
+
result[index * 3 + 2] += tangent.z;
|
|
184
|
+
}
|
|
172
185
|
}
|
|
186
|
+
|
|
187
|
+
// Normalize the accumulated tangents
|
|
188
|
+
for (let i = 0; i < result.length; i += 3) {
|
|
189
|
+
const t = new Vec(result[i], result[i + 1], result[i + 2]);
|
|
190
|
+
if (Vec.Magnitude(t) > 0) {
|
|
191
|
+
t.normalize();
|
|
192
|
+
result[i] = t.x;
|
|
193
|
+
result[i + 1] = t.y;
|
|
194
|
+
result[i + 2] = t.z;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
result[i] = 0;
|
|
198
|
+
result[i + 1] = 0;
|
|
199
|
+
result[i + 2] = 1;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
(plist as any)._tangent = result;
|
|
173
204
|
}
|
|
174
205
|
else {
|
|
206
|
+
const result: number[] = [];
|
|
175
207
|
for (let i=0; i<plist.vertex.length; i+=3) {
|
|
176
208
|
result.push(0,0,1);
|
|
177
209
|
}
|
|
178
210
|
|
|
179
211
|
console.warn("Could not generate tangents: invalid type of faces found.");
|
|
180
|
-
}
|
|
181
212
|
|
|
182
|
-
|
|
213
|
+
(plist as any)._tangent = result;
|
|
214
|
+
}
|
|
183
215
|
}
|
|
184
216
|
|
|
185
217
|
export default class PolyList {
|
package/src/db/Loader.ts
CHANGED