bg2e-js 2.3.11 → 2.3.13
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 +356 -326
- package/dist/bg2e-js.js.map +1 -1
- package/package.json +56 -56
- package/src/app/AppController.ts +39 -39
- package/src/app/Bg2KeyboardEvent.ts +54 -54
- package/src/app/Bg2MouseEvent.ts +82 -82
- package/src/app/Bg2TouchEvent.ts +18 -18
- package/src/app/Canvas.ts +108 -108
- package/src/app/EventBase.ts +10 -10
- package/src/app/MainLoop.ts +273 -273
- package/src/app/index.ts +24 -24
- package/src/base/Color.ts +134 -134
- package/src/base/Environment.ts +183 -183
- package/src/base/Light.ts +192 -192
- package/src/base/Material.ts +620 -620
- package/src/base/PolyList.ts +365 -365
- package/src/base/Texture.ts +620 -620
- package/src/base/index.ts +81 -81
- package/src/db/Bg2LoaderPlugin.ts +143 -143
- package/src/db/DBPluginApi.ts +48 -48
- package/src/db/Loader.ts +116 -116
- package/src/db/LoaderPlugin.ts +34 -34
- package/src/db/MtlParser.ts +7 -7
- package/src/db/ObjLoaderPlugin.ts +54 -54
- package/src/db/ObjParser.ts +252 -252
- package/src/db/ObjWriterPlugin.ts +18 -18
- package/src/db/VitscnjLoaderPlugin.ts +112 -112
- package/src/db/Writer.ts +52 -52
- package/src/db/WriterPlugin.ts +22 -22
- package/src/db/index.ts +44 -44
- package/src/debug/DebugRenderer.ts +173 -173
- package/src/debug/WebGLTextureViewer.ts +75 -75
- package/src/debug/index.ts +6 -6
- package/src/index.html +11 -11
- package/src/index.ts +33 -33
- package/src/manipulation/SelectionBuffer.ts +81 -81
- package/src/manipulation/SelectionHighlight.ts +105 -84
- package/src/manipulation/SelectionIdAssignVisitor.ts +96 -96
- package/src/manipulation/SelectionManager.ts +196 -188
- package/src/manipulation/SelectionMode.ts +6 -6
- package/src/math/Mat3.ts +259 -259
- package/src/math/Mat4.ts +710 -710
- package/src/math/MatrixStrategy.ts +25 -25
- package/src/math/Quat.ts +65 -65
- package/src/math/Vec.ts +753 -753
- package/src/math/constants.ts +46 -46
- package/src/math/functions.ts +103 -103
- package/src/math/index.ts +74 -74
- package/src/phsics/joint.ts +137 -137
- package/src/primitives/arrow.ts +57 -57
- package/src/primitives/cone.ts +138 -138
- package/src/primitives/cube.ts +60 -60
- package/src/primitives/cylinder.ts +216 -216
- package/src/primitives/index.ts +13 -13
- package/src/primitives/plane.ts +31 -31
- package/src/primitives/sphere.ts +809 -809
- package/src/react/useBg2e.ts +69 -69
- package/src/render/BRDFIntegrationMap.ts +4 -4
- package/src/render/Environment.ts +135 -135
- package/src/render/FrameBuffer.ts +35 -35
- package/src/render/MaterialRenderer.ts +34 -34
- package/src/render/Pipeline.ts +108 -108
- package/src/render/PolyListRenderer.ts +47 -47
- package/src/render/RenderBuffer.ts +197 -197
- package/src/render/RenderQueue.ts +198 -198
- package/src/render/RenderState.ts +116 -116
- package/src/render/Renderer.ts +248 -248
- package/src/render/SceneAppController.ts +250 -250
- package/src/render/SceneRenderer.ts +387 -387
- package/src/render/Shader.ts +32 -32
- package/src/render/ShadowRenderer.ts +176 -176
- package/src/render/SkyCube.ts +105 -105
- package/src/render/SkySphere.ts +117 -117
- package/src/render/TextureMergerRenderer.ts +70 -70
- package/src/render/TextureRenderer.ts +34 -34
- package/src/render/index.ts +67 -67
- package/src/render/webgl/FrameBuffer.ts +9 -9
- package/src/render/webgl/MaterialRenderer.ts +112 -112
- package/src/render/webgl/Pipeline.ts +88 -88
- package/src/render/webgl/PolyListRenderer.ts +260 -260
- package/src/render/webgl/RenderBuffer.ts +226 -226
- package/src/render/webgl/Renderer.ts +262 -262
- package/src/render/webgl/SceneRenderer.ts +67 -67
- package/src/render/webgl/ShaderProgram.ts +424 -424
- package/src/render/webgl/ShadowRenderer.ts +6 -6
- package/src/render/webgl/SkyCube.ts +15 -15
- package/src/render/webgl/SkySphere.ts +15 -15
- package/src/render/webgl/State.ts +152 -152
- package/src/render/webgl/TextureRenderer.ts +167 -167
- package/src/render/webgl/VertexBuffer.ts +137 -137
- package/src/render/webgl/index.ts +35 -35
- package/src/scene/Camera.ts +458 -458
- package/src/scene/Chain.ts +44 -44
- package/src/scene/ChainJoint.ts +58 -58
- package/src/scene/Component.ts +177 -177
- package/src/scene/ComponentMap.ts +106 -106
- package/src/scene/Drawable.ts +154 -154
- package/src/scene/EnvironmentComponent.ts +141 -141
- package/src/scene/FindNodeVisitor.ts +59 -59
- package/src/scene/LightComponent.ts +154 -154
- package/src/scene/MatrixState.ts +46 -46
- package/src/scene/Node.ts +328 -328
- package/src/scene/NodeVisitor.ts +15 -15
- package/src/scene/OrbitCameraController.ts +450 -450
- package/src/scene/SmoothOrbitCameraController.ts +99 -99
- package/src/scene/Transform.ts +73 -73
- package/src/scene/index.ts +60 -60
- package/src/shaders/BasicDiffuseColorShader.ts +111 -111
- package/src/shaders/BasicPBRLightShader.ts +276 -276
- package/src/shaders/DebugRenderShader.ts +97 -97
- package/src/shaders/DepthRenderShader.ts +127 -127
- package/src/shaders/IrradianceMapCubeShader.ts +115 -115
- package/src/shaders/PBRLightIBLShader.ts +486 -486
- package/src/shaders/PickSelectionShader.ts +101 -101
- package/src/shaders/PresentDebugFramebufferShader.ts +118 -118
- package/src/shaders/PresentTextureShader.ts +99 -99
- package/src/shaders/SelectionHighlightShader.ts +143 -127
- package/src/shaders/ShaderFunction.ts +318 -318
- package/src/shaders/SkyCubeShader.ts +93 -93
- package/src/shaders/SkySphereShader.ts +102 -102
- package/src/shaders/SpecularMapCubeShader.ts +164 -164
- package/src/shaders/TextureMergerShader.ts +171 -171
- package/src/shaders/index.ts +36 -36
- package/src/shaders/webgl/color_correction.glsl +47 -47
- package/src/shaders/webgl/constants.glsl +6 -6
- package/src/shaders/webgl/index.ts +70 -70
- package/src/shaders/webgl/normal_map.glsl +9 -9
- package/src/shaders/webgl/pbr.glsl +173 -173
- package/src/shaders/webgl/uniforms.glsl +91 -91
- package/src/shaders/webgl_shader_lib.ts +213 -213
- package/src/tools/BinaryResourceProvider.ts +14 -14
- package/src/tools/ImageResourceProvider.ts +66 -66
- package/src/tools/MaterialModifier.ts +446 -446
- package/src/tools/Resource.ts +203 -203
- package/src/tools/ResourceProvider.ts +69 -69
- package/src/tools/TextResourceProvider.ts +24 -24
- package/src/tools/TextureCache.ts +51 -51
- package/src/tools/TextureResourceDatabase.ts +100 -100
- package/src/tools/UserAgent.ts +362 -362
- package/src/tools/VideoResourceProvider.ts +50 -50
- package/src/tools/WriteStrategy.ts +22 -22
- package/src/tools/base64.ts +11 -11
- package/src/tools/crypto.ts +19 -19
- package/src/tools/endiantess.ts +13 -13
- package/src/tools/image.ts +18 -18
- package/src/tools/index.ts +41 -41
- package/src/tools/processType.ts +39 -39
- package/src/vite-env.d.ts +12 -12
package/src/db/ObjParser.ts
CHANGED
|
@@ -1,252 +1,252 @@
|
|
|
1
|
-
|
|
2
|
-
import PolyList from "../base/PolyList";
|
|
3
|
-
|
|
4
|
-
function parseM(this: ObjParser, line: string) {
|
|
5
|
-
// mtllib
|
|
6
|
-
let res = /mtllib\s+(.*)/.exec(line);
|
|
7
|
-
if (res) {
|
|
8
|
-
this._mtlLib = res[1];
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function parseG(this: ObjParser, line: string) {
|
|
13
|
-
// g
|
|
14
|
-
let res = /g\s+(.*)/.exec(line);
|
|
15
|
-
if (res && this._currentPlist) {
|
|
16
|
-
this._currentPlist.name = res[1];
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function parseU(this: ObjParser, line: string) {
|
|
21
|
-
// usemtl
|
|
22
|
-
let res = /usemtl\s+(.*)/.exec(line);
|
|
23
|
-
if (res && this._currentPlist) {
|
|
24
|
-
// Temporarily store the material name in a custom property
|
|
25
|
-
(this._currentPlist as any)._matName = res[1];
|
|
26
|
-
if (this._currentPlist.name=="") {
|
|
27
|
-
this._currentPlist.name = res[1];
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function parseS(this: ObjParser, line: string) {
|
|
33
|
-
// s
|
|
34
|
-
let res = /s\s+(.*)/.exec(line);
|
|
35
|
-
if (res) {
|
|
36
|
-
// TODO: Do something with smoothing groups
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function addPoint(this: ObjParser, pointData: any) {
|
|
41
|
-
if (!this._currentPlist) return;
|
|
42
|
-
this._currentPlist.vertex.push(pointData.vertex[0],pointData.vertex[1],pointData.vertex[2]);
|
|
43
|
-
if (pointData.normal) {
|
|
44
|
-
this._currentPlist.normal.push(pointData.normal[0],pointData.normal[1],pointData.normal[2]);
|
|
45
|
-
}
|
|
46
|
-
if (pointData.tex) {
|
|
47
|
-
this._currentPlist.texCoord0.push(pointData.tex[0],pointData.tex[1]);
|
|
48
|
-
}
|
|
49
|
-
this._currentPlist.index.push(this._currentPlist.index.length);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function isValid(point: any): boolean {
|
|
53
|
-
return point && point.vertex && point.tex && point.normal;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function addPolygon(this: ObjParser, polygonData: any[]) {
|
|
57
|
-
let currentVertex = 0;
|
|
58
|
-
let sides = polygonData.length;
|
|
59
|
-
if (sides<3) return;
|
|
60
|
-
while (currentVertex<sides) {
|
|
61
|
-
let i0 = currentVertex;
|
|
62
|
-
let i1 = currentVertex + 1;
|
|
63
|
-
let i2 = currentVertex + 2;
|
|
64
|
-
if (i2==sides) {
|
|
65
|
-
i2 = 0;
|
|
66
|
-
}
|
|
67
|
-
else if (i1==sides) {
|
|
68
|
-
i1 = 0;
|
|
69
|
-
i2 = 2;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
let p0 = polygonData[i0];
|
|
73
|
-
let p1 = polygonData[i1];
|
|
74
|
-
let p2 = polygonData[i2];
|
|
75
|
-
|
|
76
|
-
if (isValid(p0) && isValid(p1) && isValid(p2)) {
|
|
77
|
-
addPoint.apply(this,[p0]);
|
|
78
|
-
addPoint.apply(this,[p1]);
|
|
79
|
-
addPoint.apply(this,[p2]);
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
console.warn("Invalid point data found loading OBJ file");
|
|
83
|
-
}
|
|
84
|
-
currentVertex+=3;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function parseF(this: ObjParser, line: string) {
|
|
89
|
-
// f
|
|
90
|
-
this._addPlist = true;
|
|
91
|
-
let res = /f\s+(.*)/.exec(line);
|
|
92
|
-
if (res) {
|
|
93
|
-
let params = res[1];
|
|
94
|
-
let vtnRE = /([\d\-]+)\/([\d\-]*)\/([\d\-]*)/g;
|
|
95
|
-
if (params.indexOf('/')==-1) {
|
|
96
|
-
let vRE = /([\d\-]+)/g;
|
|
97
|
-
}
|
|
98
|
-
let polygon = [];
|
|
99
|
-
while ( (res=vtnRE.exec(params)) ) {
|
|
100
|
-
let iV = Number(res[1]);
|
|
101
|
-
let iN = res[3] ? Number(res[3]):null;
|
|
102
|
-
let iT = res[2] ? Number(res[2]):null;
|
|
103
|
-
iV = iV<0 ? this._vertexArray.length + iV : iV - 1;
|
|
104
|
-
|
|
105
|
-
let v = this._vertexArray[iV];
|
|
106
|
-
let n: number[] | null = null;
|
|
107
|
-
let t: number[] | null = null;
|
|
108
|
-
|
|
109
|
-
if (iN !== null) {
|
|
110
|
-
iN = iN<0 ? this._normalArray.length + iN : (iN===null ? null : iN - 1);
|
|
111
|
-
n = iN!==null ? this._normalArray[iN] : null;
|
|
112
|
-
}
|
|
113
|
-
if (iT !== null) {
|
|
114
|
-
iT = iT<0 ? this._texCoordArray.length + iT : (iT===null ? null : iT - 1);
|
|
115
|
-
t = iT!==null ? this._texCoordArray[iT] : null;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
polygon.push({
|
|
119
|
-
vertex:v,
|
|
120
|
-
normal:n,
|
|
121
|
-
tex:t
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
addPolygon.apply(this,[polygon]);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function parseO(this: ObjParser, line: string) {
|
|
129
|
-
// o
|
|
130
|
-
let res = /s\s+(.*)/.exec(line);
|
|
131
|
-
if (res && this._currentPlist?.name=="") {
|
|
132
|
-
this._currentPlist.name = res[1];
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function checkAddPlist(this: ObjParser) {
|
|
137
|
-
if (this._addPlist) {
|
|
138
|
-
if (this._currentPlist) {
|
|
139
|
-
this._plistArray.push(this._currentPlist);
|
|
140
|
-
}
|
|
141
|
-
this._currentPlist = new PolyList();
|
|
142
|
-
this._addPlist = false;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function loadObjData(this: ObjParser) {
|
|
147
|
-
let lines = this._textData.split('\n');
|
|
148
|
-
let multiLine = "";
|
|
149
|
-
lines.forEach(line => {
|
|
150
|
-
line = line.trim();
|
|
151
|
-
|
|
152
|
-
// This section controls the break line character \
|
|
153
|
-
// to concatenate this new line with the next one
|
|
154
|
-
if (multiLine) {
|
|
155
|
-
line = multiLine + line;
|
|
156
|
-
}
|
|
157
|
-
if (line[line.length - 1] === '\\') {
|
|
158
|
-
line = line.substring(0,line.length - 1);
|
|
159
|
-
multiLine += line;
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
multiLine = "";
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// First optimization: parse the first character and string length
|
|
167
|
-
if (line.length>1 && line[0] !== '#') {
|
|
168
|
-
// Second optimization: parse by the first character
|
|
169
|
-
switch (line[0]) {
|
|
170
|
-
case 'v':
|
|
171
|
-
let res = /v\s+([\d\.\-e]+)\s+([\d\.\-e]+)\s+([\d\.\-e]+)/.exec(line);
|
|
172
|
-
if (res) {
|
|
173
|
-
this._vertexArray.push(
|
|
174
|
-
[ Number(res[1]), Number(res[2]), Number(res[3]) ]
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
else if ( (res = /vn\s+([\d\.\-e]+)\s+([\d\.\-e]+)\s+([\d\.\-e]+)/.exec(line)) ) {
|
|
178
|
-
this._normalArray.push(
|
|
179
|
-
[ Number(res[1]), Number(res[2]), Number(res[3]) ]
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
else if ( (res = /vt\s+([\d\.\-e]+)\s+([\d\.\-e]+)/.exec(line)) ) {
|
|
183
|
-
this._texCoordArray.push(
|
|
184
|
-
[ Number(res[1]), Number(res[2]) ]
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
else {
|
|
188
|
-
console.warn("Error parsing line " + line);
|
|
189
|
-
}
|
|
190
|
-
break;
|
|
191
|
-
case 'm':
|
|
192
|
-
checkAddPlist.apply(this);
|
|
193
|
-
parseM.apply(this,[line]);
|
|
194
|
-
break;
|
|
195
|
-
case 'g':
|
|
196
|
-
checkAddPlist.apply(this);
|
|
197
|
-
parseG.apply(this,[line]);
|
|
198
|
-
break;
|
|
199
|
-
case 'u':
|
|
200
|
-
checkAddPlist.apply(this);
|
|
201
|
-
parseU.apply(this,[line]);
|
|
202
|
-
break;
|
|
203
|
-
case 's':
|
|
204
|
-
parseS.apply(this,[line]);
|
|
205
|
-
break;
|
|
206
|
-
case 'f':
|
|
207
|
-
parseF.apply(this,[line]);
|
|
208
|
-
break;
|
|
209
|
-
case 'o':
|
|
210
|
-
checkAddPlist.apply(this);
|
|
211
|
-
parseO.apply(this,[line]);
|
|
212
|
-
break;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
if (this._currentPlist && this._addPlist) {
|
|
218
|
-
this._plistArray.push(this._currentPlist);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export default class ObjParser {
|
|
223
|
-
_textData: string;
|
|
224
|
-
_plistArray: PolyList[];
|
|
225
|
-
_vertexArray: number[][];
|
|
226
|
-
_normalArray: number[][];
|
|
227
|
-
_texCoordArray: number[][];
|
|
228
|
-
_mtlLib: string;
|
|
229
|
-
_currentPlist?: PolyList | null;
|
|
230
|
-
_addPlist: boolean;
|
|
231
|
-
|
|
232
|
-
constructor(objText: string) {
|
|
233
|
-
this._textData = objText;
|
|
234
|
-
|
|
235
|
-
this._plistArray = [];
|
|
236
|
-
|
|
237
|
-
this._vertexArray = [];
|
|
238
|
-
this._normalArray = [];
|
|
239
|
-
this._texCoordArray = [];
|
|
240
|
-
|
|
241
|
-
this._mtlLib = "";
|
|
242
|
-
|
|
243
|
-
this._addPlist = true;
|
|
244
|
-
|
|
245
|
-
loadObjData.apply(this);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
get polyListArray() {
|
|
250
|
-
return this._plistArray;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
1
|
+
|
|
2
|
+
import PolyList from "../base/PolyList";
|
|
3
|
+
|
|
4
|
+
function parseM(this: ObjParser, line: string) {
|
|
5
|
+
// mtllib
|
|
6
|
+
let res = /mtllib\s+(.*)/.exec(line);
|
|
7
|
+
if (res) {
|
|
8
|
+
this._mtlLib = res[1];
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function parseG(this: ObjParser, line: string) {
|
|
13
|
+
// g
|
|
14
|
+
let res = /g\s+(.*)/.exec(line);
|
|
15
|
+
if (res && this._currentPlist) {
|
|
16
|
+
this._currentPlist.name = res[1];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function parseU(this: ObjParser, line: string) {
|
|
21
|
+
// usemtl
|
|
22
|
+
let res = /usemtl\s+(.*)/.exec(line);
|
|
23
|
+
if (res && this._currentPlist) {
|
|
24
|
+
// Temporarily store the material name in a custom property
|
|
25
|
+
(this._currentPlist as any)._matName = res[1];
|
|
26
|
+
if (this._currentPlist.name=="") {
|
|
27
|
+
this._currentPlist.name = res[1];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function parseS(this: ObjParser, line: string) {
|
|
33
|
+
// s
|
|
34
|
+
let res = /s\s+(.*)/.exec(line);
|
|
35
|
+
if (res) {
|
|
36
|
+
// TODO: Do something with smoothing groups
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function addPoint(this: ObjParser, pointData: any) {
|
|
41
|
+
if (!this._currentPlist) return;
|
|
42
|
+
this._currentPlist.vertex.push(pointData.vertex[0],pointData.vertex[1],pointData.vertex[2]);
|
|
43
|
+
if (pointData.normal) {
|
|
44
|
+
this._currentPlist.normal.push(pointData.normal[0],pointData.normal[1],pointData.normal[2]);
|
|
45
|
+
}
|
|
46
|
+
if (pointData.tex) {
|
|
47
|
+
this._currentPlist.texCoord0.push(pointData.tex[0],pointData.tex[1]);
|
|
48
|
+
}
|
|
49
|
+
this._currentPlist.index.push(this._currentPlist.index.length);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isValid(point: any): boolean {
|
|
53
|
+
return point && point.vertex && point.tex && point.normal;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function addPolygon(this: ObjParser, polygonData: any[]) {
|
|
57
|
+
let currentVertex = 0;
|
|
58
|
+
let sides = polygonData.length;
|
|
59
|
+
if (sides<3) return;
|
|
60
|
+
while (currentVertex<sides) {
|
|
61
|
+
let i0 = currentVertex;
|
|
62
|
+
let i1 = currentVertex + 1;
|
|
63
|
+
let i2 = currentVertex + 2;
|
|
64
|
+
if (i2==sides) {
|
|
65
|
+
i2 = 0;
|
|
66
|
+
}
|
|
67
|
+
else if (i1==sides) {
|
|
68
|
+
i1 = 0;
|
|
69
|
+
i2 = 2;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let p0 = polygonData[i0];
|
|
73
|
+
let p1 = polygonData[i1];
|
|
74
|
+
let p2 = polygonData[i2];
|
|
75
|
+
|
|
76
|
+
if (isValid(p0) && isValid(p1) && isValid(p2)) {
|
|
77
|
+
addPoint.apply(this,[p0]);
|
|
78
|
+
addPoint.apply(this,[p1]);
|
|
79
|
+
addPoint.apply(this,[p2]);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
console.warn("Invalid point data found loading OBJ file");
|
|
83
|
+
}
|
|
84
|
+
currentVertex+=3;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function parseF(this: ObjParser, line: string) {
|
|
89
|
+
// f
|
|
90
|
+
this._addPlist = true;
|
|
91
|
+
let res = /f\s+(.*)/.exec(line);
|
|
92
|
+
if (res) {
|
|
93
|
+
let params = res[1];
|
|
94
|
+
let vtnRE = /([\d\-]+)\/([\d\-]*)\/([\d\-]*)/g;
|
|
95
|
+
if (params.indexOf('/')==-1) {
|
|
96
|
+
let vRE = /([\d\-]+)/g;
|
|
97
|
+
}
|
|
98
|
+
let polygon = [];
|
|
99
|
+
while ( (res=vtnRE.exec(params)) ) {
|
|
100
|
+
let iV = Number(res[1]);
|
|
101
|
+
let iN = res[3] ? Number(res[3]):null;
|
|
102
|
+
let iT = res[2] ? Number(res[2]):null;
|
|
103
|
+
iV = iV<0 ? this._vertexArray.length + iV : iV - 1;
|
|
104
|
+
|
|
105
|
+
let v = this._vertexArray[iV];
|
|
106
|
+
let n: number[] | null = null;
|
|
107
|
+
let t: number[] | null = null;
|
|
108
|
+
|
|
109
|
+
if (iN !== null) {
|
|
110
|
+
iN = iN<0 ? this._normalArray.length + iN : (iN===null ? null : iN - 1);
|
|
111
|
+
n = iN!==null ? this._normalArray[iN] : null;
|
|
112
|
+
}
|
|
113
|
+
if (iT !== null) {
|
|
114
|
+
iT = iT<0 ? this._texCoordArray.length + iT : (iT===null ? null : iT - 1);
|
|
115
|
+
t = iT!==null ? this._texCoordArray[iT] : null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
polygon.push({
|
|
119
|
+
vertex:v,
|
|
120
|
+
normal:n,
|
|
121
|
+
tex:t
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
addPolygon.apply(this,[polygon]);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function parseO(this: ObjParser, line: string) {
|
|
129
|
+
// o
|
|
130
|
+
let res = /s\s+(.*)/.exec(line);
|
|
131
|
+
if (res && this._currentPlist?.name=="") {
|
|
132
|
+
this._currentPlist.name = res[1];
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function checkAddPlist(this: ObjParser) {
|
|
137
|
+
if (this._addPlist) {
|
|
138
|
+
if (this._currentPlist) {
|
|
139
|
+
this._plistArray.push(this._currentPlist);
|
|
140
|
+
}
|
|
141
|
+
this._currentPlist = new PolyList();
|
|
142
|
+
this._addPlist = false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function loadObjData(this: ObjParser) {
|
|
147
|
+
let lines = this._textData.split('\n');
|
|
148
|
+
let multiLine = "";
|
|
149
|
+
lines.forEach(line => {
|
|
150
|
+
line = line.trim();
|
|
151
|
+
|
|
152
|
+
// This section controls the break line character \
|
|
153
|
+
// to concatenate this new line with the next one
|
|
154
|
+
if (multiLine) {
|
|
155
|
+
line = multiLine + line;
|
|
156
|
+
}
|
|
157
|
+
if (line[line.length - 1] === '\\') {
|
|
158
|
+
line = line.substring(0,line.length - 1);
|
|
159
|
+
multiLine += line;
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
multiLine = "";
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// First optimization: parse the first character and string length
|
|
167
|
+
if (line.length>1 && line[0] !== '#') {
|
|
168
|
+
// Second optimization: parse by the first character
|
|
169
|
+
switch (line[0]) {
|
|
170
|
+
case 'v':
|
|
171
|
+
let res = /v\s+([\d\.\-e]+)\s+([\d\.\-e]+)\s+([\d\.\-e]+)/.exec(line);
|
|
172
|
+
if (res) {
|
|
173
|
+
this._vertexArray.push(
|
|
174
|
+
[ Number(res[1]), Number(res[2]), Number(res[3]) ]
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
else if ( (res = /vn\s+([\d\.\-e]+)\s+([\d\.\-e]+)\s+([\d\.\-e]+)/.exec(line)) ) {
|
|
178
|
+
this._normalArray.push(
|
|
179
|
+
[ Number(res[1]), Number(res[2]), Number(res[3]) ]
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
else if ( (res = /vt\s+([\d\.\-e]+)\s+([\d\.\-e]+)/.exec(line)) ) {
|
|
183
|
+
this._texCoordArray.push(
|
|
184
|
+
[ Number(res[1]), Number(res[2]) ]
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
console.warn("Error parsing line " + line);
|
|
189
|
+
}
|
|
190
|
+
break;
|
|
191
|
+
case 'm':
|
|
192
|
+
checkAddPlist.apply(this);
|
|
193
|
+
parseM.apply(this,[line]);
|
|
194
|
+
break;
|
|
195
|
+
case 'g':
|
|
196
|
+
checkAddPlist.apply(this);
|
|
197
|
+
parseG.apply(this,[line]);
|
|
198
|
+
break;
|
|
199
|
+
case 'u':
|
|
200
|
+
checkAddPlist.apply(this);
|
|
201
|
+
parseU.apply(this,[line]);
|
|
202
|
+
break;
|
|
203
|
+
case 's':
|
|
204
|
+
parseS.apply(this,[line]);
|
|
205
|
+
break;
|
|
206
|
+
case 'f':
|
|
207
|
+
parseF.apply(this,[line]);
|
|
208
|
+
break;
|
|
209
|
+
case 'o':
|
|
210
|
+
checkAddPlist.apply(this);
|
|
211
|
+
parseO.apply(this,[line]);
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
if (this._currentPlist && this._addPlist) {
|
|
218
|
+
this._plistArray.push(this._currentPlist);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export default class ObjParser {
|
|
223
|
+
_textData: string;
|
|
224
|
+
_plistArray: PolyList[];
|
|
225
|
+
_vertexArray: number[][];
|
|
226
|
+
_normalArray: number[][];
|
|
227
|
+
_texCoordArray: number[][];
|
|
228
|
+
_mtlLib: string;
|
|
229
|
+
_currentPlist?: PolyList | null;
|
|
230
|
+
_addPlist: boolean;
|
|
231
|
+
|
|
232
|
+
constructor(objText: string) {
|
|
233
|
+
this._textData = objText;
|
|
234
|
+
|
|
235
|
+
this._plistArray = [];
|
|
236
|
+
|
|
237
|
+
this._vertexArray = [];
|
|
238
|
+
this._normalArray = [];
|
|
239
|
+
this._texCoordArray = [];
|
|
240
|
+
|
|
241
|
+
this._mtlLib = "";
|
|
242
|
+
|
|
243
|
+
this._addPlist = true;
|
|
244
|
+
|
|
245
|
+
loadObjData.apply(this);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
get polyListArray() {
|
|
250
|
+
return this._plistArray;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { ResourceType } from "../tools/Resource";
|
|
2
|
-
import WriterPlugin from "./WriterPlugin";
|
|
3
|
-
import Writer from "./Writer";
|
|
4
|
-
|
|
5
|
-
export default class ObjWriterPlugin extends WriterPlugin {
|
|
6
|
-
get supportedExtensions(): string[] {
|
|
7
|
-
return ["obj"];
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
get resourceTypes(): ResourceType[] {
|
|
11
|
-
return [ResourceType.Drawable];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async write(path: string, data: any, type: ResourceType, writer: Writer): Promise<void> {
|
|
15
|
-
console.log(path);
|
|
16
|
-
console.log(data);
|
|
17
|
-
console.log(type);
|
|
18
|
-
}
|
|
1
|
+
import { ResourceType } from "../tools/Resource";
|
|
2
|
+
import WriterPlugin from "./WriterPlugin";
|
|
3
|
+
import Writer from "./Writer";
|
|
4
|
+
|
|
5
|
+
export default class ObjWriterPlugin extends WriterPlugin {
|
|
6
|
+
get supportedExtensions(): string[] {
|
|
7
|
+
return ["obj"];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
get resourceTypes(): ResourceType[] {
|
|
11
|
+
return [ResourceType.Drawable];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async write(path: string, data: any, type: ResourceType, writer: Writer): Promise<void> {
|
|
15
|
+
console.log(path);
|
|
16
|
+
console.log(data);
|
|
17
|
+
console.log(type);
|
|
18
|
+
}
|
|
19
19
|
}
|