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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bg2e-js",
3
- "version": "2.5.1",
3
+ "version": "2.5.3",
4
4
  "description": "a graphics engine for productivity applications",
5
5
  "main": "./dist/bg2e-js.js",
6
6
  "types": "./src/index.ts",
@@ -109,12 +109,19 @@ interface Vertex {
109
109
  }
110
110
 
111
111
  function buildTangents(plist: PolyList): void {
112
- const result: number[] = [];
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(plist.vertex[index] * 3, plist.vertex[index + 1] * 3, plist.vertex[index + 2] * 3 ),
117
- uv: new Vec(plist.texCoord0[index] * 2, plist.texCoord0[index + 1] * 2 )
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
- for (let i = 0; i < plist.index.length - 2; i += 3) {
127
- let v0 = createVertex(plist.index[i]);
128
- let v1 = createVertex(plist.index[i + 1]);
129
- let v2 = createVertex(plist.index[i + 2]);
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 edge1 = Vec.Sub(v1.pos, v0.pos);
132
- let edge2 = Vec.Sub(v2.pos, v1.pos);
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
- tangent.normalize();
160
-
161
- result.push(tangent.x);
162
- result.push(tangent.y);
163
- result.push(tangent.z);
164
-
165
- result.push(tangent.x);
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
- (plist as any)._tangent = result;
213
+ (plist as any)._tangent = result;
214
+ }
183
215
  }
184
216
 
185
217
  export default class PolyList {
package/src/db/Loader.ts CHANGED
@@ -134,7 +134,7 @@ export default class Loader {
134
134
  return result;
135
135
  }
136
136
 
137
- async loadPolyList(path: string): Promise<PolyList> {
137
+ async loadPolyList(path: string): Promise<PolyList[]> {
138
138
  return await this.loadResource(path, ResourceType.PolyList);
139
139
  }
140
140