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/base/index.ts
CHANGED
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
import Color from './Color';
|
|
2
|
-
import Environment from './Environment';
|
|
3
|
-
import Light, {
|
|
4
|
-
LightType
|
|
5
|
-
} from './Light';
|
|
6
|
-
import Material from './Material';
|
|
7
|
-
import PolyList, {
|
|
8
|
-
BufferType,
|
|
9
|
-
DrawMode,
|
|
10
|
-
RenderLayer,
|
|
11
|
-
getLayers,
|
|
12
|
-
PolyListFrontFace,
|
|
13
|
-
PolyListCullFace
|
|
14
|
-
} from './PolyList';
|
|
15
|
-
import Texture, {
|
|
16
|
-
TextureDataType,
|
|
17
|
-
TextureWrap,
|
|
18
|
-
TextureFilter,
|
|
19
|
-
TextureTarget,
|
|
20
|
-
ProceduralTextureFunction,
|
|
21
|
-
TextureRenderTargetAttachment,
|
|
22
|
-
TextureComponentFormat,
|
|
23
|
-
TextureChannel,
|
|
24
|
-
TextureDataTypeName,
|
|
25
|
-
TextureWrapName,
|
|
26
|
-
TextureFilterName,
|
|
27
|
-
TextureTargetName,
|
|
28
|
-
ProceduralTextureFunctionName,
|
|
29
|
-
TextureRenderTargetAttachmentNames,
|
|
30
|
-
TextureComponentFormatNames,
|
|
31
|
-
TextureChannelNames,
|
|
32
|
-
textureWrapString,
|
|
33
|
-
textureDataTypeString,
|
|
34
|
-
textureFilterString,
|
|
35
|
-
textureTargetString,
|
|
36
|
-
proceduralTextureFunctionString,
|
|
37
|
-
textureRenderTargetAttachmentString,
|
|
38
|
-
textureComponentFormatString,
|
|
39
|
-
textureChannelString,
|
|
40
|
-
} from './Texture';
|
|
41
|
-
|
|
42
|
-
export default {
|
|
43
|
-
Color,
|
|
44
|
-
Environment,
|
|
45
|
-
Light,
|
|
46
|
-
LightType,
|
|
47
|
-
Material,
|
|
48
|
-
PolyList,
|
|
49
|
-
BufferType,
|
|
50
|
-
DrawMode,
|
|
51
|
-
RenderLayer,
|
|
52
|
-
getLayers,
|
|
53
|
-
PolyListFrontFace,
|
|
54
|
-
PolyListCullFace,
|
|
55
|
-
Texture,
|
|
56
|
-
TextureDataType,
|
|
57
|
-
TextureWrap,
|
|
58
|
-
TextureFilter,
|
|
59
|
-
TextureTarget,
|
|
60
|
-
ProceduralTextureFunction,
|
|
61
|
-
TextureRenderTargetAttachment,
|
|
62
|
-
TextureComponentFormat,
|
|
63
|
-
TextureChannel,
|
|
64
|
-
TextureDataTypeName,
|
|
65
|
-
TextureWrapName,
|
|
66
|
-
TextureFilterName,
|
|
67
|
-
TextureTargetName,
|
|
68
|
-
ProceduralTextureFunctionName,
|
|
69
|
-
TextureRenderTargetAttachmentNames,
|
|
70
|
-
TextureComponentFormatNames,
|
|
71
|
-
TextureChannelNames,
|
|
72
|
-
textureWrapString,
|
|
73
|
-
textureDataTypeString,
|
|
74
|
-
textureFilterString,
|
|
75
|
-
textureTargetString,
|
|
76
|
-
proceduralTextureFunctionString,
|
|
77
|
-
textureRenderTargetAttachmentString,
|
|
78
|
-
textureComponentFormatString,
|
|
79
|
-
textureChannelString,
|
|
80
|
-
}
|
|
81
|
-
|
|
1
|
+
import Color from './Color';
|
|
2
|
+
import Environment from './Environment';
|
|
3
|
+
import Light, {
|
|
4
|
+
LightType
|
|
5
|
+
} from './Light';
|
|
6
|
+
import Material from './Material';
|
|
7
|
+
import PolyList, {
|
|
8
|
+
BufferType,
|
|
9
|
+
DrawMode,
|
|
10
|
+
RenderLayer,
|
|
11
|
+
getLayers,
|
|
12
|
+
PolyListFrontFace,
|
|
13
|
+
PolyListCullFace
|
|
14
|
+
} from './PolyList';
|
|
15
|
+
import Texture, {
|
|
16
|
+
TextureDataType,
|
|
17
|
+
TextureWrap,
|
|
18
|
+
TextureFilter,
|
|
19
|
+
TextureTarget,
|
|
20
|
+
ProceduralTextureFunction,
|
|
21
|
+
TextureRenderTargetAttachment,
|
|
22
|
+
TextureComponentFormat,
|
|
23
|
+
TextureChannel,
|
|
24
|
+
TextureDataTypeName,
|
|
25
|
+
TextureWrapName,
|
|
26
|
+
TextureFilterName,
|
|
27
|
+
TextureTargetName,
|
|
28
|
+
ProceduralTextureFunctionName,
|
|
29
|
+
TextureRenderTargetAttachmentNames,
|
|
30
|
+
TextureComponentFormatNames,
|
|
31
|
+
TextureChannelNames,
|
|
32
|
+
textureWrapString,
|
|
33
|
+
textureDataTypeString,
|
|
34
|
+
textureFilterString,
|
|
35
|
+
textureTargetString,
|
|
36
|
+
proceduralTextureFunctionString,
|
|
37
|
+
textureRenderTargetAttachmentString,
|
|
38
|
+
textureComponentFormatString,
|
|
39
|
+
textureChannelString,
|
|
40
|
+
} from './Texture';
|
|
41
|
+
|
|
42
|
+
export default {
|
|
43
|
+
Color,
|
|
44
|
+
Environment,
|
|
45
|
+
Light,
|
|
46
|
+
LightType,
|
|
47
|
+
Material,
|
|
48
|
+
PolyList,
|
|
49
|
+
BufferType,
|
|
50
|
+
DrawMode,
|
|
51
|
+
RenderLayer,
|
|
52
|
+
getLayers,
|
|
53
|
+
PolyListFrontFace,
|
|
54
|
+
PolyListCullFace,
|
|
55
|
+
Texture,
|
|
56
|
+
TextureDataType,
|
|
57
|
+
TextureWrap,
|
|
58
|
+
TextureFilter,
|
|
59
|
+
TextureTarget,
|
|
60
|
+
ProceduralTextureFunction,
|
|
61
|
+
TextureRenderTargetAttachment,
|
|
62
|
+
TextureComponentFormat,
|
|
63
|
+
TextureChannel,
|
|
64
|
+
TextureDataTypeName,
|
|
65
|
+
TextureWrapName,
|
|
66
|
+
TextureFilterName,
|
|
67
|
+
TextureTargetName,
|
|
68
|
+
ProceduralTextureFunctionName,
|
|
69
|
+
TextureRenderTargetAttachmentNames,
|
|
70
|
+
TextureComponentFormatNames,
|
|
71
|
+
TextureChannelNames,
|
|
72
|
+
textureWrapString,
|
|
73
|
+
textureDataTypeString,
|
|
74
|
+
textureFilterString,
|
|
75
|
+
textureTargetString,
|
|
76
|
+
proceduralTextureFunctionString,
|
|
77
|
+
textureRenderTargetAttachmentString,
|
|
78
|
+
textureComponentFormatString,
|
|
79
|
+
textureChannelString,
|
|
80
|
+
}
|
|
81
|
+
|
|
@@ -1,143 +1,143 @@
|
|
|
1
|
-
import LoaderPlugin from "./LoaderPlugin";
|
|
2
|
-
import { ResourceType, getFileName, removeExtension, removeFileName } from "./../tools/Resource";
|
|
3
|
-
import Resource from "../tools/Resource";
|
|
4
|
-
import PolyList from "../base/PolyList";
|
|
5
|
-
import Drawable from "../scene/Drawable";
|
|
6
|
-
import Node from "../scene/Node";
|
|
7
|
-
import Material from "../base/Material";
|
|
8
|
-
import { deserializeComponent } from "../scene/Component";
|
|
9
|
-
|
|
10
|
-
// @ts-ignore: module not typed
|
|
11
|
-
import Bg2ioWrapper from 'bg2io/Bg2ioBrowser';
|
|
12
|
-
import Loader from './Loader';
|
|
13
|
-
|
|
14
|
-
let g_bg2Wrapper: any = null;
|
|
15
|
-
|
|
16
|
-
const bg2ioFactory = async (path?: string | null): Promise<any> => {
|
|
17
|
-
if (g_bg2Wrapper === null) {
|
|
18
|
-
const params = path ? { wasmPath: path } : {};
|
|
19
|
-
g_bg2Wrapper = await Bg2ioWrapper(params);
|
|
20
|
-
if (!g_bg2Wrapper) {
|
|
21
|
-
throw new Error("Bg2LoaderPlugin: unable to initialize bg2io library");
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return g_bg2Wrapper;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const createPolyList = (jsonData: any, loader: Loader): Array<{ plist: PolyList, materialData: any }> => {
|
|
28
|
-
const result = jsonData.polyLists.map((plData: any) => {
|
|
29
|
-
const plist = new PolyList();
|
|
30
|
-
const materialData = jsonData.materials.find((m: any) => m.name === plData.matName);
|
|
31
|
-
plist.name = plData.name
|
|
32
|
-
plist.visible = plData.visible;
|
|
33
|
-
if (materialData) {
|
|
34
|
-
plist.groupName = materialData.groupName || "";
|
|
35
|
-
plist.enableCullFace = materialData.cullFace;
|
|
36
|
-
}
|
|
37
|
-
plist.vertex = plData.vertex;
|
|
38
|
-
plist.normal = plData.normal;
|
|
39
|
-
plist.texCoord0 = plData.texCoord0
|
|
40
|
-
plist.texCoord1 = plData.texCoord1
|
|
41
|
-
plist.texCoord2 = plData.texCoord2
|
|
42
|
-
plist.index = plData.index;
|
|
43
|
-
return { plist, materialData };
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
return result;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const createDrawable = async (jsonData: any, filePath: string, loader: Loader): Promise<Drawable> => {
|
|
50
|
-
const name = removeExtension(getFileName(filePath));
|
|
51
|
-
const relativePath = removeFileName(filePath);
|
|
52
|
-
const drawable = new Drawable(name);
|
|
53
|
-
for (const item of createPolyList(jsonData, loader)) {
|
|
54
|
-
const mat = new Material(loader.canvas);
|
|
55
|
-
await mat.deserialize(item.materialData, relativePath);
|
|
56
|
-
drawable.addPolyList(item.plist, mat);
|
|
57
|
-
}
|
|
58
|
-
//createPolyList(jsonData).forEach(item => {
|
|
59
|
-
// const mat = new Material();
|
|
60
|
-
// mat.deserialize(item.materialData, relativePath);
|
|
61
|
-
// drawable.addPolyList(item.plist, mat);
|
|
62
|
-
//});
|
|
63
|
-
return drawable;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const createNode = async (jsonData: any, filePath: string, loader: Loader): Promise<Node> => {
|
|
67
|
-
const name = removeExtension(getFileName(filePath));
|
|
68
|
-
const drawable = await createDrawable(jsonData,filePath,loader);
|
|
69
|
-
const node = new Node(name);
|
|
70
|
-
node.addComponent(drawable);
|
|
71
|
-
for (const compData of jsonData.components) {
|
|
72
|
-
try {
|
|
73
|
-
const comp = await deserializeComponent(compData,loader);
|
|
74
|
-
comp && node.addComponent(comp);
|
|
75
|
-
}
|
|
76
|
-
catch (err: any) {
|
|
77
|
-
console.warn(err.message);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
console.log(jsonData);
|
|
81
|
-
return node;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export type MaterialImportCallback = (matData: any) => any;
|
|
85
|
-
|
|
86
|
-
export default class Bg2LoaderPlugin extends LoaderPlugin {
|
|
87
|
-
private _bg2ioPath: string | null;
|
|
88
|
-
private _resource: Resource;
|
|
89
|
-
private _materialImportCallback?: MaterialImportCallback;
|
|
90
|
-
|
|
91
|
-
constructor( {
|
|
92
|
-
bg2ioPath = null,
|
|
93
|
-
materialImportCallback
|
|
94
|
-
}: {
|
|
95
|
-
bg2ioPath?: string | null,
|
|
96
|
-
materialImportCallback?: MaterialImportCallback
|
|
97
|
-
} = {}) {
|
|
98
|
-
super();
|
|
99
|
-
this._bg2ioPath = bg2ioPath;
|
|
100
|
-
this._materialImportCallback = materialImportCallback;
|
|
101
|
-
this._resource = new Resource();
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
get supportedExtensions(): string[] { return ["bg2","vwglb"]; }
|
|
105
|
-
|
|
106
|
-
get resourceTypes(): ResourceType[] {
|
|
107
|
-
return [
|
|
108
|
-
ResourceType.PolyList,
|
|
109
|
-
ResourceType.Drawable,
|
|
110
|
-
ResourceType.Node
|
|
111
|
-
];
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async load(path: string, resourceType: ResourceType, loader: Loader): Promise<any> {
|
|
115
|
-
const bg2io = await bg2ioFactory(this._bg2ioPath);
|
|
116
|
-
|
|
117
|
-
const buffer = await this._resource.load(path);
|
|
118
|
-
const jsonData = bg2io.loadBg2FileAsJson(buffer);
|
|
119
|
-
|
|
120
|
-
// Compatibility with 1.4 models
|
|
121
|
-
jsonData.materials.forEach((mat: any) => {
|
|
122
|
-
if (!mat.type) {
|
|
123
|
-
mat.type = mat["class"];
|
|
124
|
-
delete mat["class"];
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
if (this._materialImportCallback) {
|
|
129
|
-
jsonData.materials = jsonData.materials.map((mat: any) => this._materialImportCallback!(mat));
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
switch (resourceType) {
|
|
133
|
-
case ResourceType.PolyList:
|
|
134
|
-
return createPolyList(jsonData,loader).map(item => item.plist);
|
|
135
|
-
case ResourceType.Drawable:
|
|
136
|
-
return createDrawable(jsonData,path,loader);
|
|
137
|
-
case ResourceType.Node:
|
|
138
|
-
return await createNode(jsonData,path,loader);
|
|
139
|
-
default:
|
|
140
|
-
throw new Error(`Bg2LoaderPlugin.load() unexpected resource type received: ${resourceType}`);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
1
|
+
import LoaderPlugin from "./LoaderPlugin";
|
|
2
|
+
import { ResourceType, getFileName, removeExtension, removeFileName } from "./../tools/Resource";
|
|
3
|
+
import Resource from "../tools/Resource";
|
|
4
|
+
import PolyList from "../base/PolyList";
|
|
5
|
+
import Drawable from "../scene/Drawable";
|
|
6
|
+
import Node from "../scene/Node";
|
|
7
|
+
import Material from "../base/Material";
|
|
8
|
+
import { deserializeComponent } from "../scene/Component";
|
|
9
|
+
|
|
10
|
+
// @ts-ignore: module not typed
|
|
11
|
+
import Bg2ioWrapper from 'bg2io/Bg2ioBrowser';
|
|
12
|
+
import Loader from './Loader';
|
|
13
|
+
|
|
14
|
+
let g_bg2Wrapper: any = null;
|
|
15
|
+
|
|
16
|
+
const bg2ioFactory = async (path?: string | null): Promise<any> => {
|
|
17
|
+
if (g_bg2Wrapper === null) {
|
|
18
|
+
const params = path ? { wasmPath: path } : {};
|
|
19
|
+
g_bg2Wrapper = await Bg2ioWrapper(params);
|
|
20
|
+
if (!g_bg2Wrapper) {
|
|
21
|
+
throw new Error("Bg2LoaderPlugin: unable to initialize bg2io library");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return g_bg2Wrapper;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const createPolyList = (jsonData: any, loader: Loader): Array<{ plist: PolyList, materialData: any }> => {
|
|
28
|
+
const result = jsonData.polyLists.map((plData: any) => {
|
|
29
|
+
const plist = new PolyList();
|
|
30
|
+
const materialData = jsonData.materials.find((m: any) => m.name === plData.matName);
|
|
31
|
+
plist.name = plData.name
|
|
32
|
+
plist.visible = plData.visible;
|
|
33
|
+
if (materialData) {
|
|
34
|
+
plist.groupName = materialData.groupName || "";
|
|
35
|
+
plist.enableCullFace = materialData.cullFace;
|
|
36
|
+
}
|
|
37
|
+
plist.vertex = plData.vertex;
|
|
38
|
+
plist.normal = plData.normal;
|
|
39
|
+
plist.texCoord0 = plData.texCoord0
|
|
40
|
+
plist.texCoord1 = plData.texCoord1
|
|
41
|
+
plist.texCoord2 = plData.texCoord2
|
|
42
|
+
plist.index = plData.index;
|
|
43
|
+
return { plist, materialData };
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const createDrawable = async (jsonData: any, filePath: string, loader: Loader): Promise<Drawable> => {
|
|
50
|
+
const name = removeExtension(getFileName(filePath));
|
|
51
|
+
const relativePath = removeFileName(filePath);
|
|
52
|
+
const drawable = new Drawable(name);
|
|
53
|
+
for (const item of createPolyList(jsonData, loader)) {
|
|
54
|
+
const mat = new Material(loader.canvas);
|
|
55
|
+
await mat.deserialize(item.materialData, relativePath);
|
|
56
|
+
drawable.addPolyList(item.plist, mat);
|
|
57
|
+
}
|
|
58
|
+
//createPolyList(jsonData).forEach(item => {
|
|
59
|
+
// const mat = new Material();
|
|
60
|
+
// mat.deserialize(item.materialData, relativePath);
|
|
61
|
+
// drawable.addPolyList(item.plist, mat);
|
|
62
|
+
//});
|
|
63
|
+
return drawable;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const createNode = async (jsonData: any, filePath: string, loader: Loader): Promise<Node> => {
|
|
67
|
+
const name = removeExtension(getFileName(filePath));
|
|
68
|
+
const drawable = await createDrawable(jsonData,filePath,loader);
|
|
69
|
+
const node = new Node(name);
|
|
70
|
+
node.addComponent(drawable);
|
|
71
|
+
for (const compData of jsonData.components) {
|
|
72
|
+
try {
|
|
73
|
+
const comp = await deserializeComponent(compData,loader);
|
|
74
|
+
comp && node.addComponent(comp);
|
|
75
|
+
}
|
|
76
|
+
catch (err: any) {
|
|
77
|
+
console.warn(err.message);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
console.log(jsonData);
|
|
81
|
+
return node;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export type MaterialImportCallback = (matData: any) => any;
|
|
85
|
+
|
|
86
|
+
export default class Bg2LoaderPlugin extends LoaderPlugin {
|
|
87
|
+
private _bg2ioPath: string | null;
|
|
88
|
+
private _resource: Resource;
|
|
89
|
+
private _materialImportCallback?: MaterialImportCallback;
|
|
90
|
+
|
|
91
|
+
constructor( {
|
|
92
|
+
bg2ioPath = null,
|
|
93
|
+
materialImportCallback
|
|
94
|
+
}: {
|
|
95
|
+
bg2ioPath?: string | null,
|
|
96
|
+
materialImportCallback?: MaterialImportCallback
|
|
97
|
+
} = {}) {
|
|
98
|
+
super();
|
|
99
|
+
this._bg2ioPath = bg2ioPath;
|
|
100
|
+
this._materialImportCallback = materialImportCallback;
|
|
101
|
+
this._resource = new Resource();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
get supportedExtensions(): string[] { return ["bg2","vwglb"]; }
|
|
105
|
+
|
|
106
|
+
get resourceTypes(): ResourceType[] {
|
|
107
|
+
return [
|
|
108
|
+
ResourceType.PolyList,
|
|
109
|
+
ResourceType.Drawable,
|
|
110
|
+
ResourceType.Node
|
|
111
|
+
];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async load(path: string, resourceType: ResourceType, loader: Loader): Promise<any> {
|
|
115
|
+
const bg2io = await bg2ioFactory(this._bg2ioPath);
|
|
116
|
+
|
|
117
|
+
const buffer = await this._resource.load(path);
|
|
118
|
+
const jsonData = bg2io.loadBg2FileAsJson(buffer);
|
|
119
|
+
|
|
120
|
+
// Compatibility with 1.4 models
|
|
121
|
+
jsonData.materials.forEach((mat: any) => {
|
|
122
|
+
if (!mat.type) {
|
|
123
|
+
mat.type = mat["class"];
|
|
124
|
+
delete mat["class"];
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
if (this._materialImportCallback) {
|
|
129
|
+
jsonData.materials = jsonData.materials.map((mat: any) => this._materialImportCallback!(mat));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
switch (resourceType) {
|
|
133
|
+
case ResourceType.PolyList:
|
|
134
|
+
return createPolyList(jsonData,loader).map(item => item.plist);
|
|
135
|
+
case ResourceType.Drawable:
|
|
136
|
+
return createDrawable(jsonData,path,loader);
|
|
137
|
+
case ResourceType.Node:
|
|
138
|
+
return await createNode(jsonData,path,loader);
|
|
139
|
+
default:
|
|
140
|
+
throw new Error(`Bg2LoaderPlugin.load() unexpected resource type received: ${resourceType}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
package/src/db/DBPluginApi.ts
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import { getExtension, ResourceType } from '../tools/Resource';
|
|
2
|
-
import LoaderPlugin from './LoaderPlugin';
|
|
3
|
-
|
|
4
|
-
export const PluginOperationType = {
|
|
5
|
-
Read: "read",
|
|
6
|
-
Write: "write"
|
|
7
|
-
} as const;
|
|
8
|
-
|
|
9
|
-
export type PluginOperationTypeValue = typeof PluginOperationType[keyof typeof PluginOperationType];
|
|
10
|
-
|
|
11
|
-
export interface PluginDatabase {
|
|
12
|
-
operationType: PluginOperationTypeValue;
|
|
13
|
-
plugins: Partial<Record<ResourceType, LoaderPlugin[]>>;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export const createPluginDatabase = (operationType: PluginOperationTypeValue): PluginDatabase => {
|
|
17
|
-
return {
|
|
18
|
-
operationType,
|
|
19
|
-
plugins: {}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const registerPluginInDatabase = (pluginInstance: LoaderPlugin, pluginDatabase: PluginDatabase): void => {
|
|
24
|
-
pluginInstance.resourceTypes.forEach(type => {
|
|
25
|
-
pluginDatabase.plugins[type] = pluginDatabase.plugins[type] || [];
|
|
26
|
-
pluginDatabase.plugins[type]!.push(pluginInstance);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export const getPluginFromDatabase = function(path: string, type: ResourceType, pluginDatabase: PluginDatabase): LoaderPlugin {
|
|
31
|
-
const ext = getExtension(path);
|
|
32
|
-
const extCheck = new RegExp(ext, "i");
|
|
33
|
-
const errMsg = `Could not find a plugin to ${pluginDatabase.operationType} file '${path}' of type '${type}'.`;
|
|
34
|
-
|
|
35
|
-
const plugins = pluginDatabase.plugins[type];
|
|
36
|
-
if (!plugins) {
|
|
37
|
-
throw new Error(errMsg);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
for (const plugin of plugins) {
|
|
41
|
-
if (plugin.supportedExtensions.find(e => extCheck.test(e))) {
|
|
42
|
-
return plugin;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
throw new Error(errMsg);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
1
|
+
import { getExtension, ResourceType } from '../tools/Resource';
|
|
2
|
+
import LoaderPlugin from './LoaderPlugin';
|
|
3
|
+
|
|
4
|
+
export const PluginOperationType = {
|
|
5
|
+
Read: "read",
|
|
6
|
+
Write: "write"
|
|
7
|
+
} as const;
|
|
8
|
+
|
|
9
|
+
export type PluginOperationTypeValue = typeof PluginOperationType[keyof typeof PluginOperationType];
|
|
10
|
+
|
|
11
|
+
export interface PluginDatabase {
|
|
12
|
+
operationType: PluginOperationTypeValue;
|
|
13
|
+
plugins: Partial<Record<ResourceType, LoaderPlugin[]>>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const createPluginDatabase = (operationType: PluginOperationTypeValue): PluginDatabase => {
|
|
17
|
+
return {
|
|
18
|
+
operationType,
|
|
19
|
+
plugins: {}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const registerPluginInDatabase = (pluginInstance: LoaderPlugin, pluginDatabase: PluginDatabase): void => {
|
|
24
|
+
pluginInstance.resourceTypes.forEach(type => {
|
|
25
|
+
pluginDatabase.plugins[type] = pluginDatabase.plugins[type] || [];
|
|
26
|
+
pluginDatabase.plugins[type]!.push(pluginInstance);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const getPluginFromDatabase = function(path: string, type: ResourceType, pluginDatabase: PluginDatabase): LoaderPlugin {
|
|
31
|
+
const ext = getExtension(path);
|
|
32
|
+
const extCheck = new RegExp(ext, "i");
|
|
33
|
+
const errMsg = `Could not find a plugin to ${pluginDatabase.operationType} file '${path}' of type '${type}'.`;
|
|
34
|
+
|
|
35
|
+
const plugins = pluginDatabase.plugins[type];
|
|
36
|
+
if (!plugins) {
|
|
37
|
+
throw new Error(errMsg);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
for (const plugin of plugins) {
|
|
41
|
+
if (plugin.supportedExtensions.find(e => extCheck.test(e))) {
|
|
42
|
+
return plugin;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
throw new Error(errMsg);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|