freeform-modeling-mcp 1.0.26
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/README.md +61 -0
- package/index.cjs +21 -0
- package/package.json +48 -0
- package/src/cli.ts +47 -0
- package/src/commands/start.ts +88 -0
- package/src/commands/status.ts +81 -0
- package/src/dev.ts +24 -0
- package/src/lib/fileproxy/index.ts +505 -0
- package/src/lib/fileproxy/proxy-server.js +347 -0
- package/src/lib/gltf2obj/compress.js +33 -0
- package/src/lib/gltf2obj/engine/core/core.js +34 -0
- package/src/lib/gltf2obj/engine/core/eventnotifier.js +39 -0
- package/src/lib/gltf2obj/engine/core/taskrunner.js +88 -0
- package/src/lib/gltf2obj/engine/export/exporter.js +38 -0
- package/src/lib/gltf2obj/engine/export/exporterbase.js +86 -0
- package/src/lib/gltf2obj/engine/export/exportermodel.js +117 -0
- package/src/lib/gltf2obj/engine/export/exporterobj.js +147 -0
- package/src/lib/gltf2obj/engine/geometry/box3d.js +60 -0
- package/src/lib/gltf2obj/engine/geometry/coord2d.js +35 -0
- package/src/lib/gltf2obj/engine/geometry/coord3d.js +126 -0
- package/src/lib/gltf2obj/engine/geometry/coord4d.js +15 -0
- package/src/lib/gltf2obj/engine/geometry/geometry.js +56 -0
- package/src/lib/gltf2obj/engine/geometry/matrix.js +440 -0
- package/src/lib/gltf2obj/engine/geometry/octree.js +160 -0
- package/src/lib/gltf2obj/engine/geometry/quaternion.js +83 -0
- package/src/lib/gltf2obj/engine/geometry/transformation.js +63 -0
- package/src/lib/gltf2obj/engine/geometry/tween.js +31 -0
- package/src/lib/gltf2obj/engine/import/importer.js +270 -0
- package/src/lib/gltf2obj/engine/import/importerbase.js +115 -0
- package/src/lib/gltf2obj/engine/import/importerfiles.js +139 -0
- package/src/lib/gltf2obj/engine/import/importergltf.js +1045 -0
- package/src/lib/gltf2obj/engine/import/importerutils.js +102 -0
- package/src/lib/gltf2obj/engine/io/binaryreader.js +93 -0
- package/src/lib/gltf2obj/engine/io/binarywriter.js +92 -0
- package/src/lib/gltf2obj/engine/io/bufferutils.js +85 -0
- package/src/lib/gltf2obj/engine/io/externallibs.js +42 -0
- package/src/lib/gltf2obj/engine/io/fileutils.js +120 -0
- package/src/lib/gltf2obj/engine/io/textwriter.js +41 -0
- package/src/lib/gltf2obj/engine/main.js +19 -0
- package/src/lib/gltf2obj/engine/model/color.js +130 -0
- package/src/lib/gltf2obj/engine/model/generator.js +433 -0
- package/src/lib/gltf2obj/engine/model/material.js +243 -0
- package/src/lib/gltf2obj/engine/model/mesh.js +173 -0
- package/src/lib/gltf2obj/engine/model/meshbuffer.js +233 -0
- package/src/lib/gltf2obj/engine/model/meshinstance.js +128 -0
- package/src/lib/gltf2obj/engine/model/meshutils.js +64 -0
- package/src/lib/gltf2obj/engine/model/model.js +195 -0
- package/src/lib/gltf2obj/engine/model/modelfinalization.js +377 -0
- package/src/lib/gltf2obj/engine/model/modelutils.js +117 -0
- package/src/lib/gltf2obj/engine/model/node.js +178 -0
- package/src/lib/gltf2obj/engine/model/object.js +90 -0
- package/src/lib/gltf2obj/engine/model/property.js +86 -0
- package/src/lib/gltf2obj/engine/model/quantities.js +46 -0
- package/src/lib/gltf2obj/engine/model/topology.js +139 -0
- package/src/lib/gltf2obj/engine/model/triangle.js +99 -0
- package/src/lib/gltf2obj/engine/threejs/threemodelloader.js +85 -0
- package/src/lib/gltf2obj/handler.js +63 -0
- package/src/lib/gltf2obj/index.js +47 -0
- package/src/lib/logger.ts +304 -0
- package/src/lib/print.ts +37 -0
- package/src/lib/utils.ts +34 -0
- package/src/lib/wsbridge/bridge.ts +639 -0
- package/src/lib/wsbridge/lock.ts +258 -0
- package/src/llmClient.ts +245 -0
- package/src/prompt.ts +187 -0
- package/src/server.ts +125 -0
- package/src/tools/assets-tool.ts +314 -0
- package/src/tools/basic-tools.ts +531 -0
- package/src/tools/index.ts +2 -0
- package/src/tools/material-tools.ts +88 -0
- package/src/tools/modeling/auxiliary-curve-tool.ts +44 -0
- package/src/tools/modeling/find-face.ts +38 -0
- package/src/tools/modeling/sweep-tool.ts +60 -0
- package/src/tools/modeling/utils.ts +181 -0
- package/src/tools/modeling-tools.ts +135 -0
- package/src/tools/screenshot-tools.ts +107 -0
- package/src/tools/tools-info.ts +107 -0
- package/src/tools/tripo3d-tools.ts +496 -0
- package/src/types/index.ts +25 -0
- package/src/types/tripo3d.ts +38 -0
- package/tsconfig.json +22 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { Coord2D, CoordIsEqual2D } from '../geometry/coord2d.js';
|
|
2
|
+
import { IsEqual } from '../geometry/geometry.js';
|
|
3
|
+
import { RGBColor, RGBColorIsEqual } from './color.js';
|
|
4
|
+
|
|
5
|
+
export class TextureMap
|
|
6
|
+
{
|
|
7
|
+
constructor ()
|
|
8
|
+
{
|
|
9
|
+
this.name = null;
|
|
10
|
+
this.mimeType = null;
|
|
11
|
+
this.buffer = null;
|
|
12
|
+
this.offset = new Coord2D (0.0, 0.0);
|
|
13
|
+
this.scale = new Coord2D (1.0, 1.0);
|
|
14
|
+
this.rotation = 0.0; // radians
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
IsValid ()
|
|
18
|
+
{
|
|
19
|
+
return this.name !== null && this.buffer !== null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
HasTransformation ()
|
|
23
|
+
{
|
|
24
|
+
if (!CoordIsEqual2D (this.offset, new Coord2D (0.0, 0.0))) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
if (!CoordIsEqual2D (this.scale, new Coord2D (1.0, 1.0))) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
if (!IsEqual (this.rotation, 0.0)) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
IsEqual (rhs)
|
|
37
|
+
{
|
|
38
|
+
if (this.name !== rhs.name) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if (this.mimeType !== rhs.mimeType) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
if (!CoordIsEqual2D (this.offset, rhs.offset)) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if (!CoordIsEqual2D (this.scale, rhs.scale)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
if (!IsEqual (this.rotation, rhs.rotation)) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function TextureMapIsEqual (aTex, bTex)
|
|
58
|
+
{
|
|
59
|
+
if (aTex === null && bTex === null) {
|
|
60
|
+
return true;
|
|
61
|
+
} else if (aTex === null || bTex === null) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
return aTex.IsEqual (bTex);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const MaterialType =
|
|
68
|
+
{
|
|
69
|
+
Phong : 1,
|
|
70
|
+
Physical : 2
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export class MaterialBase
|
|
74
|
+
{
|
|
75
|
+
constructor (type)
|
|
76
|
+
{
|
|
77
|
+
this.type = type;
|
|
78
|
+
this.isDefault = false;
|
|
79
|
+
|
|
80
|
+
this.name = '';
|
|
81
|
+
this.color = new RGBColor (0, 0, 0);
|
|
82
|
+
|
|
83
|
+
this.vertexColors = false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
IsEqual (rhs)
|
|
87
|
+
{
|
|
88
|
+
if (this.type !== rhs.type) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
if (this.isDefault !== rhs.isDefault) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
if (this.name !== rhs.name) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
if (!RGBColorIsEqual (this.color, rhs.color)) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (this.vertexColors !== rhs.vertexColors) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export class FaceMaterial extends MaterialBase
|
|
108
|
+
{
|
|
109
|
+
constructor (type)
|
|
110
|
+
{
|
|
111
|
+
super (type);
|
|
112
|
+
|
|
113
|
+
this.emissive = new RGBColor (0, 0, 0);
|
|
114
|
+
|
|
115
|
+
this.opacity = 1.0; // 0.0 .. 1.0
|
|
116
|
+
this.transparent = false;
|
|
117
|
+
|
|
118
|
+
this.diffuseMap = null;
|
|
119
|
+
this.bumpMap = null;
|
|
120
|
+
this.normalMap = null;
|
|
121
|
+
this.emissiveMap = null;
|
|
122
|
+
|
|
123
|
+
this.alphaTest = 0.0; // 0.0 .. 1.0
|
|
124
|
+
this.multiplyDiffuseMap = false;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
IsEqual (rhs)
|
|
128
|
+
{
|
|
129
|
+
if (!super.IsEqual (rhs)) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
if (!RGBColorIsEqual (this.emissive, rhs.emissive)) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
if (!IsEqual (this.opacity, rhs.opacity)) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
if (this.transparent !== rhs.transparent) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
if (!TextureMapIsEqual (this.diffuseMap, rhs.diffuseMap)) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
if (!TextureMapIsEqual (this.bumpMap, rhs.bumpMap)) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
if (!TextureMapIsEqual (this.normalMap, rhs.normalMap)) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
if (!TextureMapIsEqual (this.emissiveMap, rhs.emissiveMap)) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
if (!IsEqual (this.alphaTest, rhs.alphaTest)) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
if (this.multiplyDiffuseMap !== rhs.multiplyDiffuseMap) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export class PhongMaterial extends FaceMaterial
|
|
164
|
+
{
|
|
165
|
+
constructor ()
|
|
166
|
+
{
|
|
167
|
+
super (MaterialType.Phong);
|
|
168
|
+
|
|
169
|
+
this.ambient = new RGBColor (0, 0, 0);
|
|
170
|
+
this.specular = new RGBColor (0, 0, 0);
|
|
171
|
+
this.shininess = 0.0; // 0.0 .. 1.0
|
|
172
|
+
this.specularMap = null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
IsEqual (rhs)
|
|
176
|
+
{
|
|
177
|
+
if (!super.IsEqual (rhs)) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
if (!RGBColorIsEqual (this.ambient, rhs.ambient)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
if (!RGBColorIsEqual (this.specular, rhs.specular)) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
if (!IsEqual (this.shininess, rhs.shininess)) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
if (!TextureMapIsEqual (this.specularMap, rhs.specularMap)) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export class PhysicalMaterial extends FaceMaterial
|
|
197
|
+
{
|
|
198
|
+
constructor ()
|
|
199
|
+
{
|
|
200
|
+
super (MaterialType.Physical);
|
|
201
|
+
|
|
202
|
+
this.metalness = 0.0; // 0.0 .. 1.0
|
|
203
|
+
this.roughness = 1.0; // 0.0 .. 1.0
|
|
204
|
+
this.metalnessMap = null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
IsEqual (rhs)
|
|
208
|
+
{
|
|
209
|
+
if (!super.IsEqual (rhs)) {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
if (!IsEqual (this.metalness, rhs.metalness)) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
if (!IsEqual (this.roughness, rhs.roughness)) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
if (!TextureMapIsEqual (this.metalnessMap, rhs.metalnessMap)) {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function TextureIsEqual (a, b)
|
|
226
|
+
{
|
|
227
|
+
if (a.name !== b.name) {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
if (a.mimeType !== b.mimeType) {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
if (!CoordIsEqual2D (a.offset, b.offset)) {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
if (!CoordIsEqual2D (a.scale, b.scale)) {
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
if (!IsEqual (a.rotation, b.rotation)) {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { ModelObject3D } from './object.js';
|
|
2
|
+
|
|
3
|
+
export class Mesh extends ModelObject3D
|
|
4
|
+
{
|
|
5
|
+
constructor ()
|
|
6
|
+
{
|
|
7
|
+
super ();
|
|
8
|
+
this.vertices = [];
|
|
9
|
+
this.vertexColors = [];
|
|
10
|
+
this.normals = [];
|
|
11
|
+
this.uvs = [];
|
|
12
|
+
this.triangles = [];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
VertexCount ()
|
|
16
|
+
{
|
|
17
|
+
return this.vertices.length;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
VertexColorCount ()
|
|
21
|
+
{
|
|
22
|
+
return this.vertexColors.length;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
NormalCount ()
|
|
26
|
+
{
|
|
27
|
+
return this.normals.length;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
TextureUVCount ()
|
|
31
|
+
{
|
|
32
|
+
return this.uvs.length;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
TriangleCount ()
|
|
36
|
+
{
|
|
37
|
+
return this.triangles.length;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
AddVertex (vertex)
|
|
41
|
+
{
|
|
42
|
+
this.vertices.push (vertex);
|
|
43
|
+
return this.vertices.length - 1;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
SetVertex (index, vertex)
|
|
47
|
+
{
|
|
48
|
+
this.vertices[index] = vertex;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
GetVertex (index)
|
|
52
|
+
{
|
|
53
|
+
return this.vertices[index];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
AddVertexColor (color)
|
|
57
|
+
{
|
|
58
|
+
this.vertexColors.push (color);
|
|
59
|
+
return this.vertexColors.length - 1;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
SetVertexColor (index, color)
|
|
63
|
+
{
|
|
64
|
+
this.vertexColors[index] = color;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
GetVertexColor (index)
|
|
68
|
+
{
|
|
69
|
+
return this.vertexColors[index];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
AddNormal (normal)
|
|
73
|
+
{
|
|
74
|
+
this.normals.push (normal);
|
|
75
|
+
return this.normals.length - 1;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
SetNormal (index, normal)
|
|
79
|
+
{
|
|
80
|
+
this.normals[index] = normal;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
GetNormal (index)
|
|
84
|
+
{
|
|
85
|
+
return this.normals[index];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
AddTextureUV (uv)
|
|
89
|
+
{
|
|
90
|
+
this.uvs.push (uv);
|
|
91
|
+
return this.uvs.length - 1;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
SetTextureUV (index, uv)
|
|
95
|
+
{
|
|
96
|
+
this.uvs[index] = uv;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
GetTextureUV (index)
|
|
100
|
+
{
|
|
101
|
+
return this.uvs[index];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
AddTriangle (triangle)
|
|
105
|
+
{
|
|
106
|
+
this.triangles.push (triangle);
|
|
107
|
+
return this.triangles.length - 1;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
GetTriangle (index)
|
|
111
|
+
{
|
|
112
|
+
return this.triangles[index];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
EnumerateVertices (onVertex)
|
|
116
|
+
{
|
|
117
|
+
for (const vertex of this.vertices) {
|
|
118
|
+
onVertex (vertex);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
EnumerateTriangleVertexIndices (onTriangleVertexIndices)
|
|
123
|
+
{
|
|
124
|
+
for (const triangle of this.triangles) {
|
|
125
|
+
onTriangleVertexIndices (triangle.v0, triangle.v1, triangle.v2);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
EnumerateTriangleVertices (onTriangleVertices)
|
|
130
|
+
{
|
|
131
|
+
for (const triangle of this.triangles) {
|
|
132
|
+
let v0 = this.vertices[triangle.v0];
|
|
133
|
+
let v1 = this.vertices[triangle.v1];
|
|
134
|
+
let v2 = this.vertices[triangle.v2];
|
|
135
|
+
onTriangleVertices (v0, v1, v2);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
Clone ()
|
|
140
|
+
{
|
|
141
|
+
let cloned = new Mesh ();
|
|
142
|
+
|
|
143
|
+
cloned.SetName (this.GetName ());
|
|
144
|
+
this.CloneProperties (cloned);
|
|
145
|
+
|
|
146
|
+
for (let i = 0; i < this.VertexCount (); i++) {
|
|
147
|
+
let vertex = this.GetVertex (i);
|
|
148
|
+
cloned.AddVertex (vertex.Clone ());
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
for (let i = 0; i < this.VertexColorCount (); i++) {
|
|
152
|
+
let color = this.GetVertexColor (i);
|
|
153
|
+
cloned.AddVertexColor (color.Clone ());
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
for (let i = 0; i < this.NormalCount (); i++) {
|
|
157
|
+
let normal = this.GetNormal (i);
|
|
158
|
+
cloned.AddNormal (normal.Clone ());
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
for (let i = 0; i < this.TextureUVCount (); i++) {
|
|
162
|
+
let uv = this.GetTextureUV (i);
|
|
163
|
+
cloned.AddTextureUV (uv.Clone ());
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
for (let i = 0; i < this.TriangleCount (); i++) {
|
|
167
|
+
let triangle = this.GetTriangle (i);
|
|
168
|
+
cloned.AddTriangle (triangle.Clone ());
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return cloned;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { Coord2D, CoordIsEqual2D } from '../geometry/coord2d.js';
|
|
2
|
+
import { CoordIsEqual3D } from '../geometry/coord3d.js';
|
|
3
|
+
import { RGBColor, RGBColorIsEqual } from './color.js';
|
|
4
|
+
|
|
5
|
+
export class MeshPrimitiveBuffer
|
|
6
|
+
{
|
|
7
|
+
constructor ()
|
|
8
|
+
{
|
|
9
|
+
this.indices = [];
|
|
10
|
+
this.vertices = [];
|
|
11
|
+
this.colors = [];
|
|
12
|
+
this.normals = [];
|
|
13
|
+
this.uvs = [];
|
|
14
|
+
this.material = null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
GetBounds ()
|
|
18
|
+
{
|
|
19
|
+
let min = [Infinity, Infinity, Infinity];
|
|
20
|
+
let max = [-Infinity, -Infinity, -Infinity];
|
|
21
|
+
for (let i = 0; i < this.vertices.length / 3; i++) {
|
|
22
|
+
for (let j = 0; j < 3; j++) {
|
|
23
|
+
min[j] = Math.min (min[j], this.vertices[i * 3 + j]);
|
|
24
|
+
max[j] = Math.max (max[j], this.vertices[i * 3 + j]);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
min : min,
|
|
29
|
+
max : max
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
GetByteLength (indexTypeSize, numberTypeSize)
|
|
34
|
+
{
|
|
35
|
+
let indexCount = this.indices.length;
|
|
36
|
+
let numberCount = this.vertices.length + this.colors.length + this.normals.length + this.uvs.length;
|
|
37
|
+
return indexCount * indexTypeSize + numberCount * numberTypeSize;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class MeshBuffer
|
|
42
|
+
{
|
|
43
|
+
constructor ()
|
|
44
|
+
{
|
|
45
|
+
this.primitives = [];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
PrimitiveCount ()
|
|
49
|
+
{
|
|
50
|
+
return this.primitives.length;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
GetPrimitive (index)
|
|
54
|
+
{
|
|
55
|
+
return this.primitives[index];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
GetByteLength (indexTypeSize, numberTypeSize)
|
|
59
|
+
{
|
|
60
|
+
let byteLength = 0;
|
|
61
|
+
for (let i = 0; i < this.primitives.length; i++) {
|
|
62
|
+
let primitive = this.primitives[i];
|
|
63
|
+
byteLength += primitive.GetByteLength (indexTypeSize, numberTypeSize);
|
|
64
|
+
}
|
|
65
|
+
return byteLength;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function ConvertMeshToMeshBuffer (mesh)
|
|
70
|
+
{
|
|
71
|
+
function AddVertexToPrimitiveBuffer (mesh, indices, primitiveBuffer, meshVertexToPrimitiveVertices)
|
|
72
|
+
{
|
|
73
|
+
function GetColorOrDefault (mesh, colorIndex, forceColors)
|
|
74
|
+
{
|
|
75
|
+
if (colorIndex !== null) {
|
|
76
|
+
return mesh.GetVertexColor (colorIndex);
|
|
77
|
+
} else if (forceColors) {
|
|
78
|
+
return new RGBColor (0, 0, 0);
|
|
79
|
+
} else {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function GetUVOrDefault (mesh, uvIndex, forceUVs)
|
|
85
|
+
{
|
|
86
|
+
if (uvIndex !== null) {
|
|
87
|
+
return mesh.GetTextureUV (uvIndex);
|
|
88
|
+
} else if (forceUVs) {
|
|
89
|
+
return new Coord2D (0.0, 0.0);
|
|
90
|
+
} else {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function AddVertex (mesh, indices, primitiveBuffer)
|
|
96
|
+
{
|
|
97
|
+
let forceColors = mesh.VertexColorCount () > 0;
|
|
98
|
+
let forceUVs = mesh.TextureUVCount () > 0;
|
|
99
|
+
|
|
100
|
+
let vertex = mesh.GetVertex (indices.vertex);
|
|
101
|
+
let normal = mesh.GetNormal (indices.normal);
|
|
102
|
+
|
|
103
|
+
let primitiveVertexIndex = primitiveBuffer.vertices.length / 3;
|
|
104
|
+
primitiveBuffer.indices.push (primitiveVertexIndex);
|
|
105
|
+
primitiveBuffer.vertices.push (vertex.x, vertex.y, vertex.z);
|
|
106
|
+
|
|
107
|
+
let color = GetColorOrDefault (mesh, indices.color, forceColors);
|
|
108
|
+
if (color !== null) {
|
|
109
|
+
primitiveBuffer.colors.push (color.r / 255.0, color.g / 255.0, color.b / 255.0);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
primitiveBuffer.normals.push (normal.x, normal.y, normal.z);
|
|
113
|
+
|
|
114
|
+
let uv = GetUVOrDefault (mesh, indices.uv, forceUVs);
|
|
115
|
+
if (uv !== null) {
|
|
116
|
+
primitiveBuffer.uvs.push (uv.x, uv.y);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
index : primitiveVertexIndex,
|
|
121
|
+
color : color,
|
|
122
|
+
normal : normal,
|
|
123
|
+
uv : uv
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function FindMatchingPrimitiveVertex (mesh, primitiveVertices, indices)
|
|
128
|
+
{
|
|
129
|
+
function IsEqualColor (mesh, colorIndex, existingColor)
|
|
130
|
+
{
|
|
131
|
+
if (existingColor === null && colorIndex === null) {
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
let color = GetColorOrDefault (mesh, colorIndex, true);
|
|
135
|
+
return RGBColorIsEqual (existingColor, color);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function IsEqualNormal (mesh, normalIndex, existingNormal)
|
|
139
|
+
{
|
|
140
|
+
let normal = mesh.GetNormal (normalIndex);
|
|
141
|
+
return CoordIsEqual3D (existingNormal, normal);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function IsEqualUV (mesh, uvIndex, existingUv)
|
|
145
|
+
{
|
|
146
|
+
if (existingUv === null && uvIndex === null) {
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
let uv = GetUVOrDefault (mesh, uvIndex, true);
|
|
150
|
+
return CoordIsEqual2D (existingUv, uv);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
for (let i = 0; i < primitiveVertices.length; i++) {
|
|
154
|
+
let primitiveVertex = primitiveVertices[i];
|
|
155
|
+
let equalColor = IsEqualColor (mesh, indices.color, primitiveVertex.color);
|
|
156
|
+
let equalNormal = IsEqualNormal (mesh, indices.normal, primitiveVertex.normal);
|
|
157
|
+
let equalUv = IsEqualUV (mesh, indices.uv, primitiveVertex.uv);
|
|
158
|
+
if (equalColor && equalNormal && equalUv) {
|
|
159
|
+
return primitiveVertex;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (meshVertexToPrimitiveVertices.has (indices.vertex)) {
|
|
166
|
+
let primitiveVertices = meshVertexToPrimitiveVertices.get (indices.vertex);
|
|
167
|
+
let existingPrimitiveVertex = FindMatchingPrimitiveVertex (mesh, primitiveVertices, indices);
|
|
168
|
+
if (existingPrimitiveVertex !== null) {
|
|
169
|
+
primitiveBuffer.indices.push (existingPrimitiveVertex.index);
|
|
170
|
+
} else {
|
|
171
|
+
let primitiveVertex = AddVertex (mesh, indices, primitiveBuffer);
|
|
172
|
+
primitiveVertices.push (primitiveVertex);
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
let primitiveVertex = AddVertex (mesh, indices, primitiveBuffer);
|
|
176
|
+
meshVertexToPrimitiveVertices.set (indices.vertex, [primitiveVertex]);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
let meshBuffer = new MeshBuffer ();
|
|
181
|
+
|
|
182
|
+
let triangleCount = mesh.TriangleCount ();
|
|
183
|
+
if (triangleCount === 0) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let triangleIndices = [];
|
|
188
|
+
for (let i = 0; i < triangleCount; i++) {
|
|
189
|
+
triangleIndices.push (i);
|
|
190
|
+
}
|
|
191
|
+
triangleIndices.sort ((a, b) => {
|
|
192
|
+
let aTriangle = mesh.GetTriangle (a);
|
|
193
|
+
let bTriangle = mesh.GetTriangle (b);
|
|
194
|
+
return aTriangle.mat - bTriangle.mat;
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
let primitiveBuffer = null;
|
|
198
|
+
let meshVertexToPrimitiveVertices = null;
|
|
199
|
+
for (let i = 0; i < triangleIndices.length; i++) {
|
|
200
|
+
let triangleIndex = triangleIndices[i];
|
|
201
|
+
let triangle = mesh.GetTriangle (triangleIndex);
|
|
202
|
+
if (primitiveBuffer === null || primitiveBuffer.material !== triangle.mat) {
|
|
203
|
+
primitiveBuffer = new MeshPrimitiveBuffer ();
|
|
204
|
+
primitiveBuffer.material = triangle.mat;
|
|
205
|
+
meshVertexToPrimitiveVertices = new Map ();
|
|
206
|
+
meshBuffer.primitives.push (primitiveBuffer);
|
|
207
|
+
}
|
|
208
|
+
let v0Indices = {
|
|
209
|
+
vertex : triangle.v0,
|
|
210
|
+
color : triangle.c0,
|
|
211
|
+
normal : triangle.n0,
|
|
212
|
+
uv : triangle.u0
|
|
213
|
+
};
|
|
214
|
+
let v1Indices = {
|
|
215
|
+
vertex : triangle.v1,
|
|
216
|
+
color : triangle.c1,
|
|
217
|
+
normal : triangle.n1,
|
|
218
|
+
uv : triangle.u1
|
|
219
|
+
};
|
|
220
|
+
let v2Indices = {
|
|
221
|
+
vertex : triangle.v2,
|
|
222
|
+
color : triangle.c2,
|
|
223
|
+
normal : triangle.n2,
|
|
224
|
+
uv : triangle.u2
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
AddVertexToPrimitiveBuffer (mesh, v0Indices, primitiveBuffer, meshVertexToPrimitiveVertices);
|
|
228
|
+
AddVertexToPrimitiveBuffer (mesh, v1Indices, primitiveBuffer, meshVertexToPrimitiveVertices);
|
|
229
|
+
AddVertexToPrimitiveBuffer (mesh, v2Indices, primitiveBuffer, meshVertexToPrimitiveVertices);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return meshBuffer;
|
|
233
|
+
}
|