protosprite-geom 0.2.1

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.
Files changed (37) hide show
  1. package/dist/core.js +7 -0
  2. package/dist/core.js.map +1 -0
  3. package/dist/proto_dist/sprite_geometry_pb.d.ts +383 -0
  4. package/dist/proto_dist/sprite_geometry_pb.js +52 -0
  5. package/dist/proto_dist/sprite_geometry_pb.js.map +1 -0
  6. package/dist/src/core/data.d.ts +68 -0
  7. package/dist/src/core/data.js +238 -0
  8. package/dist/src/core/data.js.map +1 -0
  9. package/dist/src/core/index.d.ts +6 -0
  10. package/dist/src/core/protosprite-geom.d.ts +12 -0
  11. package/dist/src/core/protosprite-geom.js +63 -0
  12. package/dist/src/core/protosprite-geom.js.map +1 -0
  13. package/dist/src/index.d.ts +2 -0
  14. package/dist/src/trace/convex.d.ts +2 -0
  15. package/dist/src/trace/convex.js +38 -0
  16. package/dist/src/trace/convex.js.map +1 -0
  17. package/dist/src/trace/index.d.ts +13 -0
  18. package/dist/src/trace/simplify.d.ts +2 -0
  19. package/dist/src/trace/simplify.js +18 -0
  20. package/dist/src/trace/simplify.js.map +1 -0
  21. package/dist/src/trace/trace.d.ts +7 -0
  22. package/dist/src/trace/trace.js +46 -0
  23. package/dist/src/trace/trace.js.map +1 -0
  24. package/dist/trace.js +139 -0
  25. package/dist/trace.js.map +1 -0
  26. package/package.json +61 -0
  27. package/proto_dist/sprite_geometry_pb.ts +424 -0
  28. package/src/core/data.ts +304 -0
  29. package/src/core/index.ts +18 -0
  30. package/src/core/protosprite-geom.ts +71 -0
  31. package/src/index.ts +2 -0
  32. package/src/trace/convex.ts +41 -0
  33. package/src/trace/index.ts +217 -0
  34. package/src/trace/simplify.ts +21 -0
  35. package/src/trace/trace.ts +60 -0
  36. package/src/types/marchingsquares.d.ts +22 -0
  37. package/src/types/poly-decomp.d.ts +10 -0
@@ -0,0 +1,238 @@
1
+ import { create } from '@bufbuild/protobuf';
2
+ import { Vec2Schema, PolygonSchema, ConvexDecompositionSchema, FrameLayerGeometrySchema, CompositeFrameGeometrySchema, FrameGeometrySchema, SpriteGeometryEntrySchema, SpriteGeometrySchema } from '../../proto_dist/sprite_geometry_pb.js';
3
+
4
+ class Vec2Data {
5
+ x = 0;
6
+ y = 0;
7
+ static fromProto(proto) {
8
+ const instance = new Vec2Data();
9
+ instance.x = proto.x;
10
+ instance.y = proto.y;
11
+ return instance;
12
+ }
13
+ toProto(protoIn) {
14
+ const proto = protoIn ?? create(Vec2Schema);
15
+ proto.x = this.x;
16
+ proto.y = this.y;
17
+ return proto;
18
+ }
19
+ clone() {
20
+ const other = new Vec2Data();
21
+ other.x = this.x;
22
+ other.y = this.y;
23
+ return other;
24
+ }
25
+ }
26
+ class PolygonData {
27
+ vertices = [];
28
+ static fromProto(proto) {
29
+ const instance = new PolygonData();
30
+ instance.vertices = proto.vertices.map(Vec2Data.fromProto);
31
+ return instance;
32
+ }
33
+ toProto(protoIn) {
34
+ const proto = protoIn ?? create(PolygonSchema);
35
+ proto.vertices = this.vertices.map((v) => v.toProto());
36
+ return proto;
37
+ }
38
+ clone() {
39
+ const other = new PolygonData();
40
+ other.vertices = this.vertices.map((v) => v.clone());
41
+ return other;
42
+ }
43
+ }
44
+ class ConvexDecompositionData {
45
+ components = [];
46
+ static fromProto(proto) {
47
+ const instance = new ConvexDecompositionData();
48
+ instance.components = proto.components.map(PolygonData.fromProto);
49
+ return instance;
50
+ }
51
+ toProto(protoIn) {
52
+ const proto = protoIn ?? create(ConvexDecompositionSchema);
53
+ proto.components = this.components.map((c) => c.toProto());
54
+ return proto;
55
+ }
56
+ clone() {
57
+ const other = new ConvexDecompositionData();
58
+ other.components = this.components.map((c) => c.clone());
59
+ return other;
60
+ }
61
+ }
62
+ class FrameLayerGeometryData {
63
+ layerIndex = 0;
64
+ polygons = [];
65
+ convexDecompositions = [];
66
+ static fromProto(proto) {
67
+ const instance = new FrameLayerGeometryData();
68
+ instance.layerIndex = proto.layerIndex;
69
+ instance.polygons = proto.polygons.map(PolygonData.fromProto);
70
+ instance.convexDecompositions = proto.convexDecompositions.map(ConvexDecompositionData.fromProto);
71
+ return instance;
72
+ }
73
+ toProto(protoIn) {
74
+ const proto = protoIn ?? create(FrameLayerGeometrySchema);
75
+ proto.layerIndex = this.layerIndex;
76
+ proto.polygons = this.polygons.map((p) => p.toProto());
77
+ proto.convexDecompositions = this.convexDecompositions.map((d) => d.toProto());
78
+ return proto;
79
+ }
80
+ clone() {
81
+ const other = new FrameLayerGeometryData();
82
+ other.layerIndex = this.layerIndex;
83
+ other.polygons = this.polygons.map((p) => p.clone());
84
+ other.convexDecompositions = this.convexDecompositions.map((d) => d.clone());
85
+ return other;
86
+ }
87
+ }
88
+ class CompositeFrameGeometryData {
89
+ polygons = [];
90
+ convexDecompositions = [];
91
+ static fromProto(proto) {
92
+ const instance = new CompositeFrameGeometryData();
93
+ instance.polygons = proto.polygons.map(PolygonData.fromProto);
94
+ instance.convexDecompositions = proto.convexDecompositions.map(ConvexDecompositionData.fromProto);
95
+ return instance;
96
+ }
97
+ toProto(protoIn) {
98
+ const proto = protoIn ?? create(CompositeFrameGeometrySchema);
99
+ proto.polygons = this.polygons.map((p) => p.toProto());
100
+ proto.convexDecompositions = this.convexDecompositions.map((d) => d.toProto());
101
+ return proto;
102
+ }
103
+ clone() {
104
+ const other = new CompositeFrameGeometryData();
105
+ other.polygons = this.polygons.map((p) => p.clone());
106
+ other.convexDecompositions = this.convexDecompositions.map((d) => d.clone());
107
+ return other;
108
+ }
109
+ }
110
+ class FrameGeometryData {
111
+ frameIndex = 0;
112
+ layers = [];
113
+ composite;
114
+ static fromProto(proto) {
115
+ const instance = new FrameGeometryData();
116
+ instance.frameIndex = proto.frameIndex;
117
+ instance.layers = proto.layers.map(FrameLayerGeometryData.fromProto);
118
+ instance.composite = proto.composite
119
+ ? CompositeFrameGeometryData.fromProto(proto.composite)
120
+ : undefined;
121
+ return instance;
122
+ }
123
+ toProto(protoIn) {
124
+ const proto = protoIn ?? create(FrameGeometrySchema);
125
+ proto.frameIndex = this.frameIndex;
126
+ proto.layers = this.layers.map((l) => l.toProto());
127
+ proto.composite = this.composite?.toProto();
128
+ return proto;
129
+ }
130
+ clone() {
131
+ const other = new FrameGeometryData();
132
+ other.frameIndex = this.frameIndex;
133
+ other.layers = this.layers.map((l) => l.clone());
134
+ other.composite = this.composite?.clone();
135
+ return other;
136
+ }
137
+ }
138
+ class SpriteGeometryEntryData {
139
+ spriteName = "";
140
+ frames = [];
141
+ simplifyTolerance = 1.0;
142
+ static fromProto(proto) {
143
+ const instance = new SpriteGeometryEntryData();
144
+ instance.spriteName = proto.spriteName;
145
+ instance.frames = proto.frames.map(FrameGeometryData.fromProto);
146
+ instance.simplifyTolerance = proto.simplifyTolerance;
147
+ return instance;
148
+ }
149
+ toProto(protoIn) {
150
+ const proto = protoIn ?? create(SpriteGeometryEntrySchema);
151
+ proto.spriteName = this.spriteName;
152
+ proto.frames = this.frames.map((f) => f.toProto());
153
+ proto.simplifyTolerance = this.simplifyTolerance;
154
+ return proto;
155
+ }
156
+ clone() {
157
+ const other = new SpriteGeometryEntryData();
158
+ other.spriteName = this.spriteName;
159
+ other.frames = this.frames.map((f) => f.clone());
160
+ other.simplifyTolerance = this.simplifyTolerance;
161
+ return other;
162
+ }
163
+ }
164
+ class SpriteGeometryData {
165
+ entries = [];
166
+ spriteSource;
167
+ static fromProto(proto) {
168
+ const instance = new SpriteGeometryData();
169
+ instance.entries = proto.entries.map(SpriteGeometryEntryData.fromProto);
170
+ switch (proto.spriteSource.case) {
171
+ case "embeddedPrs":
172
+ instance.spriteSource = {
173
+ type: "embedded",
174
+ prsData: proto.spriteSource.value
175
+ };
176
+ break;
177
+ case "externalPrsFile":
178
+ instance.spriteSource = {
179
+ type: "externalFile",
180
+ fileName: proto.spriteSource.value
181
+ };
182
+ break;
183
+ case "externalPrsUrl":
184
+ instance.spriteSource = {
185
+ type: "externalUrl",
186
+ url: proto.spriteSource.value
187
+ };
188
+ break;
189
+ }
190
+ return instance;
191
+ }
192
+ toProto(protoIn) {
193
+ const proto = protoIn ?? create(SpriteGeometrySchema);
194
+ proto.entries = this.entries.map((e) => e.toProto());
195
+ if (this.spriteSource) {
196
+ switch (this.spriteSource.type) {
197
+ case "embedded":
198
+ proto.spriteSource = {
199
+ case: "embeddedPrs",
200
+ value: this.spriteSource.prsData
201
+ };
202
+ break;
203
+ case "externalFile":
204
+ proto.spriteSource = {
205
+ case: "externalPrsFile",
206
+ value: this.spriteSource.fileName
207
+ };
208
+ break;
209
+ case "externalUrl":
210
+ proto.spriteSource = {
211
+ case: "externalPrsUrl",
212
+ value: this.spriteSource.url
213
+ };
214
+ break;
215
+ }
216
+ }
217
+ return proto;
218
+ }
219
+ clone() {
220
+ const other = new SpriteGeometryData();
221
+ other.entries = this.entries.map((e) => e.clone());
222
+ if (this.spriteSource) {
223
+ if (this.spriteSource.type === "embedded") {
224
+ other.spriteSource = {
225
+ type: "embedded",
226
+ prsData: new Uint8Array(this.spriteSource.prsData)
227
+ };
228
+ }
229
+ else {
230
+ other.spriteSource = { ...this.spriteSource };
231
+ }
232
+ }
233
+ return other;
234
+ }
235
+ }
236
+
237
+ export { CompositeFrameGeometryData, ConvexDecompositionData, FrameGeometryData, FrameLayerGeometryData, PolygonData, SpriteGeometryData, SpriteGeometryEntryData, Vec2Data };
238
+ //# sourceMappingURL=data.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data.js","sources":["../../../src/core/data.ts"],"sourcesContent":[null],"names":[],"mappings":";;;MAqBa,QAAQ,CAAA;IACZ,CAAC,GAAG,CAAC;IACL,CAAC,GAAG,CAAC;IAEZ,OAAO,SAAS,CAAC,KAAW,EAAA;AAC1B,QAAA,MAAM,QAAQ,GAAG,IAAI,QAAQ,EAAE;AAC/B,QAAA,QAAQ,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC;AACpB,QAAA,QAAQ,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC;AACpB,QA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AC,YAAY,GAAG;AACnB,oBAAA,IAAI,EAAE,UAAU;oBAChB,OAAO,EAAE,IAAI,UAAU,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO;iBAClD;YACH;iBAAO;gBACL,KAAK,CAAC,YAAY,GAAG,EAAE,GAAG,IAAI,CAAC,YAAY,EAAE;YAC/C;QACF;AACA,QAAA,OAAO,KAAK;IACd;AACD;;;;"}
@@ -0,0 +1,6 @@
1
+ import * as Protos from "../../proto_dist/sprite_geometry_pb.js";
2
+ import * as Data from "./data.js";
3
+ import { ProtoSpriteGeometry } from "./protosprite-geom.js";
4
+ export { ProtoSpriteGeometry, Protos, Data };
5
+ export { Vec2Data, PolygonData, ConvexDecompositionData, FrameLayerGeometryData, CompositeFrameGeometryData, FrameGeometryData, SpriteGeometryEntryData, SpriteGeometryData } from "./data.js";
6
+ export type { SpriteSourceData } from "./data.js";
@@ -0,0 +1,12 @@
1
+ import { ProtoSpriteSheet } from "protosprite-core";
2
+ import { SpriteGeometryData } from "./data.js";
3
+ export declare class ProtoSpriteGeometry {
4
+ data: SpriteGeometryData;
5
+ constructor(data?: SpriteGeometryData);
6
+ static fromArray(uint8Array: Uint8Array): ProtoSpriteGeometry;
7
+ toArray(): Uint8Array<ArrayBuffer>;
8
+ toJsonObject(): import("../../proto_dist/sprite_geometry_pb.js").SpriteGeometryJson;
9
+ getSpriteSheet(): ProtoSpriteSheet;
10
+ embedSpriteSheet(sheet: ProtoSpriteSheet): void;
11
+ referenceSpriteSheet(fileNameOrUrl: string): void;
12
+ }
@@ -0,0 +1,63 @@
1
+ import { fromBinary, toBinary, toJson } from '@bufbuild/protobuf';
2
+ import { ProtoSpriteSheet } from 'protosprite-core';
3
+ import { SpriteGeometrySchema } from '../../proto_dist/sprite_geometry_pb.js';
4
+ import { SpriteGeometryData } from './data.js';
5
+ import fs from 'fs';
6
+
7
+ class ProtoSpriteGeometry {
8
+ data;
9
+ constructor(data) {
10
+ this.data = data ?? new SpriteGeometryData();
11
+ }
12
+ static fromArray(uint8Array) {
13
+ const proto = fromBinary(SpriteGeometrySchema, uint8Array);
14
+ const resultData = SpriteGeometryData.fromProto(proto);
15
+ return new ProtoSpriteGeometry(resultData);
16
+ }
17
+ toArray() {
18
+ const proto = this.data.toProto();
19
+ return toBinary(SpriteGeometrySchema, proto);
20
+ }
21
+ toJsonObject() {
22
+ const proto = this.data.toProto();
23
+ return toJson(SpriteGeometrySchema, proto);
24
+ }
25
+ getSpriteSheet() {
26
+ if (!this.data.spriteSource) {
27
+ throw new Error("[ProtoSpriteGeometry] No sprite source defined in .prsg data.");
28
+ }
29
+ switch (this.data.spriteSource.type) {
30
+ case "embedded":
31
+ return ProtoSpriteSheet.fromArray(this.data.spriteSource.prsData);
32
+ case "externalFile": {
33
+ const rawBuff = fs.readFileSync(this.data.spriteSource.fileName);
34
+ return ProtoSpriteSheet.fromArray(new Uint8Array(rawBuff));
35
+ }
36
+ case "externalUrl":
37
+ throw new Error("[ProtoSpriteGeometry] URL-based sprite source loading is not yet supported. Use a local file path.");
38
+ }
39
+ }
40
+ embedSpriteSheet(sheet) {
41
+ this.data.spriteSource = {
42
+ type: "embedded",
43
+ prsData: sheet.toArray()
44
+ };
45
+ }
46
+ referenceSpriteSheet(fileNameOrUrl) {
47
+ if (fileNameOrUrl.startsWith("http://") || fileNameOrUrl.startsWith("https://")) {
48
+ this.data.spriteSource = {
49
+ type: "externalUrl",
50
+ url: fileNameOrUrl
51
+ };
52
+ }
53
+ else {
54
+ this.data.spriteSource = {
55
+ type: "externalFile",
56
+ fileName: fileNameOrUrl
57
+ };
58
+ }
59
+ }
60
+ }
61
+
62
+ export { ProtoSpriteGeometry };
63
+ //# sourceMappingURL=protosprite-geom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protosprite-geom.js","sources":["../../../src/core/protosprite-geom.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;MAMa,mBAAmB,CAAA;AACvB,IAAA,IAAI;AAEX,IAAA,WAAA,CAAY,IAAy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IAAI,CAAC,IAAI,CAAC,YAAY,GAAG;AACvB,YAAA,IAAI,EAAE,UAAU;AAChB,YAAA,OAAO,EAAE,KAAK,CAAC,OAAO;SACvB;IACH;AAEA,IAAA,oBAAoB,CAAC,aAAqB,EAAA;AACxC,QAAA,IAAI,aAAa,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,aAAa,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE;AAC/E,YAAA,IAAI,CAAC,IAAI,CAAC,YAAY,GAAG;AACvB,gBAAA,IAAI,EAAE,aAAa;AACnB,gBAAA,GAAG,EAAE;aACN;QACH;aAAO;AACL,YAAA,IAAI,CAAC,IAAI,CAAC,YAAY,GAAG;AACvB,gBAAA,IAAI,EAAE,cAAc;AACpB,gBAAA,QAAQ,EAAE;aACX;QACH;IACF;AACD;;;;"}
@@ -0,0 +1,2 @@
1
+ export * from "./core/index.js";
2
+ export * from "./trace/index.js";
@@ -0,0 +1,2 @@
1
+ import { Vec2Data } from "../core/data.js";
2
+ export declare function decomposeConvex(polygon: Vec2Data[]): Vec2Data[][];
@@ -0,0 +1,38 @@
1
+ import * as decomp from 'poly-decomp';
2
+ import { Vec2Data } from '../core/data.js';
3
+
4
+ function decomposeConvex(polygon) {
5
+ if (polygon.length < 3)
6
+ return [polygon];
7
+ // Convert to poly-decomp format: [[x, y], ...]
8
+ const polyDecompPolygon = polygon.map((v) => [v.x, v.y]);
9
+ // Ensure CCW winding order and clean up the polygon.
10
+ decomp.makeCCW(polyDecompPolygon);
11
+ decomp.removeDuplicatePoints(polyDecompPolygon, 0.01);
12
+ decomp.removeCollinearPoints(polyDecompPolygon, 0.01);
13
+ if (polyDecompPolygon.length < 3)
14
+ return [polygon];
15
+ // Attempt decomposition. quickDecomp is more robust than decomp for
16
+ // complex polygons.
17
+ let convexParts;
18
+ try {
19
+ convexParts = decomp.quickDecomp(polyDecompPolygon);
20
+ }
21
+ catch {
22
+ // Fallback: return the original polygon as a single "convex" part.
23
+ return [polygon];
24
+ }
25
+ if (!convexParts || convexParts.length === 0) {
26
+ return [polygon];
27
+ }
28
+ // Convert back to Vec2Data format.
29
+ return convexParts.map((part) => part.map((point) => {
30
+ const v = new Vec2Data();
31
+ v.x = point[0];
32
+ v.y = point[1];
33
+ return v;
34
+ }));
35
+ }
36
+
37
+ export { decomposeConvex };
38
+ //# sourceMappingURL=convex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convex.js","sources":["../../../src/trace/convex.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIM,SAAU,eAAe,CAAC,OAAmB,EAAA;AACjD,IAAA,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,CAAC,OAAO,CAAC;;IAGxC,MAAM,iBAAiB,GAAe,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;;AAGpE,IAAA,MAAM,CAAC,OAAO,CAAC,iBAAiB,CAAC;AACjC,IAAA,MAAM,CAAC,qBAAqB,CAAC,iBAAiB,EAAE,IAAI,CAAC;AACrD,IAAA,MAAM,CAAC,qBAAqB,CAAC,iBAAiB,EAAE,IAAI,CAAC;AAErD,IAAA,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,CAAC,OAAO,CAAC;;;AAIlD,IAAA,IAAI,WAAyB;AAC7B,IAAA,IAAI;AACF,QAAA,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC,iBAAiB,CAAC;IACrD;AAAE,IAAA,MAAM;;QAEN,OAAO,CAAC,OAAO,CAAC;IAClB;IAEA,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,EAAE;QAC5C,OAAO,CAAC,OAAO,CAAC;IAClB;;AAGA,IAAA,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,KAC1B,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,KAAI;AACjB,QAAA,MAAM,CAAC,GAAG,IAAI,QAAQ,EAAE;AACxB,QAAA,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC;AACd,QAAA,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC;AACd,QAAA,OAAO,CAAC;IACV,CAAC,CAAC,CACH;AACH;;;;"}
@@ -0,0 +1,13 @@
1
+ import { ProtoSpriteSheet } from "protosprite-core";
2
+ import { SpriteGeometryData } from "../core/data.js";
3
+ export { traceContours } from "./trace.js";
4
+ export { simplifyPolygon } from "./simplify.js";
5
+ export { decomposeConvex } from "./convex.js";
6
+ export interface TraceSpriteSheetOptions {
7
+ tolerance: number;
8
+ alphaThreshold?: number;
9
+ highQuality?: boolean;
10
+ composite?: boolean;
11
+ perLayer?: boolean;
12
+ }
13
+ export declare function traceSpriteSheet(sheet: ProtoSpriteSheet, options: TraceSpriteSheetOptions): Promise<SpriteGeometryData>;
@@ -0,0 +1,2 @@
1
+ import { Vec2Data } from "../core/data.js";
2
+ export declare function simplifyPolygon(polygon: Vec2Data[], tolerance: number, highQuality?: boolean): Vec2Data[];
@@ -0,0 +1,18 @@
1
+ import simplify from 'simplify-js';
2
+ import { Vec2Data } from '../core/data.js';
3
+
4
+ function simplifyPolygon(polygon, tolerance, highQuality = true) {
5
+ if (polygon.length < 3)
6
+ return polygon;
7
+ const points = polygon.map((v) => ({ x: v.x, y: v.y }));
8
+ const simplified = simplify(points, tolerance, highQuality);
9
+ return simplified.map((p) => {
10
+ const v = new Vec2Data();
11
+ v.x = p.x;
12
+ v.y = p.y;
13
+ return v;
14
+ });
15
+ }
16
+
17
+ export { simplifyPolygon };
18
+ //# sourceMappingURL=simplify.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"simplify.js","sources":["../../../src/trace/simplify.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIM,SAAU,eAAe,CAC7B,OAAmB,EACnB,SAAiB,EACjB,cAAuB,IAAI,EAAA;AAE3B,IAAA,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC;AAAE,QAAA,OAAO,OAAO;IAEtC,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IACvD,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,EAAE,SAAS,EAAE,WAAW,CAAC;AAE3D,IAAA,OAAO,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,KAAI;AAC1B,QAAA,MAAM,CAAC,GAAG,IAAI,QAAQ,EAAE;AACxB,QAAA,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AACT,QAAA,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AACT,QAAA,OAAO,CAAC;AACV,IAAA,CAAC,CAAC;AACJ;;;;"}
@@ -0,0 +1,7 @@
1
+ import { Vec2Data } from "../core/data.js";
2
+ export interface ImageDataLike {
3
+ width: number;
4
+ height: number;
5
+ data: Uint8Array | Buffer;
6
+ }
7
+ export declare function traceContours(imageData: ImageDataLike, alphaThreshold?: number): Vec2Data[][];
@@ -0,0 +1,46 @@
1
+ import { isoContours } from 'marchingsquares';
2
+ import { Vec2Data } from '../core/data.js';
3
+
4
+ function traceContours(imageData, alphaThreshold = 1) {
5
+ const { width, height, data } = imageData;
6
+ // Build a 2D grid of alpha values with 1px of zero-padding on all sides.
7
+ // This ensures marching squares can properly close contours when opaque
8
+ // pixels touch the image edge, without producing a spurious outer loop
9
+ // (which the library's built-in frame does).
10
+ const paddedW = width + 2;
11
+ const grid = [];
12
+ // Top padding row.
13
+ grid.push(new Array(paddedW).fill(0));
14
+ for (let y = 0; y < height; y++) {
15
+ const row = [0]; // left padding
16
+ for (let x = 0; x < width; x++) {
17
+ const idx = (y * width + x) * 4;
18
+ row.push(data[idx + 3]); // alpha channel
19
+ }
20
+ row.push(0); // right padding
21
+ grid.push(row);
22
+ }
23
+ // Bottom padding row.
24
+ grid.push(new Array(paddedW).fill(0));
25
+ // Run marching squares on the padded grid.
26
+ // noFrame: true since we added our own padding.
27
+ const contours = isoContours(grid, alphaThreshold, { noFrame: true });
28
+ // Convert to Vec2Data arrays, subtracting the 1px padding offset.
29
+ const result = [];
30
+ for (const contour of contours) {
31
+ if (contour.length < 3)
32
+ continue;
33
+ const polygon = [];
34
+ for (const point of contour) {
35
+ const v = new Vec2Data();
36
+ v.x = point[0] - 1;
37
+ v.y = point[1] - 1;
38
+ polygon.push(v);
39
+ }
40
+ result.push(polygon);
41
+ }
42
+ return result;
43
+ }
44
+
45
+ export { traceContours };
46
+ //# sourceMappingURL=trace.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trace.js","sources":["../../../src/trace/trace.ts"],"sourcesContent":[null],"names":[],"mappings":";;;SAUgB,aAAa,CAC3B,SAAwB,EACxB,iBAAyB,CAAC,EAAA;IAE1B,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,SAAS;;;;;AAMzC,IAAA,MAAM,OAAO,GAAG,KAAK,GAAG,CAAC;IAEzB,MAAM,IAAI,GAAe,EAAE;;AAG3B,IAAA,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAErC,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;AAC/B,QAAA,MAAM,GAAG,GAAa,CAAC,CAAC,CAAC,CAAC;AAC1B,QAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE;YAC9B,MAAM,GAAG,GAAG,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,IAAI,CAAC;AAC/B,YAAA,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;QAC1B;AACA,QAAA,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACZ,QAAA,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;IAChB;;AAGA,IAAA,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;;;AAIrC,IAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,EAAE,cAAc,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;IAGrE,MAAM,MAAM,GAAiB,EAAE;AAC/B,IAAA,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;AAC9B,QAAA,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC;YAAE;QACxB,MAAM,OAAO,GAAe,EAAE;AAC9B,QAAA,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE;AAC3B,YAAA,MAAM,CAAC,GAAG,IAAI,QAAQ,EAAE;YACxB,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC;YAClB,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC;AAClB,YAAA,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QACjB;AACA,QAAA,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;IACtB;AAEA,IAAA,OAAO,MAAM;AACf;;;;"}
package/dist/trace.js ADDED
@@ -0,0 +1,139 @@
1
+ import { Buffer } from 'buffer';
2
+ import { Jimp } from 'jimp';
3
+ import { ProtoSpriteInstance, Data } from 'protosprite-core';
4
+ import { renderSpriteInstance } from 'protosprite-core/transform';
5
+ import { SpriteGeometryEntryData, FrameGeometryData, FrameLayerGeometryData, CompositeFrameGeometryData, SpriteGeometryData, PolygonData, ConvexDecompositionData } from './src/core/data.js';
6
+ import { decomposeConvex } from './src/trace/convex.js';
7
+ import { simplifyPolygon } from './src/trace/simplify.js';
8
+ import { traceContours } from './src/trace/trace.js';
9
+
10
+ async function readPixelSource(pixelSource) {
11
+ if (Data.isEmbeddedSpriteSheetData(pixelSource)) {
12
+ if (pixelSource.pngData) {
13
+ const b64 = `data:image/png;base64,${Buffer.from(pixelSource.pngData).toString("base64")}`;
14
+ return Jimp.read(b64, { "image/png": {} });
15
+ }
16
+ return null;
17
+ }
18
+ if (Data.isExternalSpriteSheetData(pixelSource)) {
19
+ const urlOrFileName = pixelSource.url ?? pixelSource.fileName;
20
+ if (!urlOrFileName)
21
+ return null;
22
+ return Jimp.read(urlOrFileName, { "image/png": {} });
23
+ }
24
+ return null;
25
+ }
26
+ function traceAndProcess(imageData, tolerance, alphaThreshold, highQuality) {
27
+ const rawContours = traceContours(imageData, alphaThreshold);
28
+ const polygons = [];
29
+ const convexDecompositions = [];
30
+ for (const contour of rawContours) {
31
+ const simplified = simplifyPolygon(contour, tolerance, highQuality);
32
+ if (simplified.length < 3)
33
+ continue;
34
+ const poly = new PolygonData();
35
+ poly.vertices = simplified;
36
+ polygons.push(poly);
37
+ const convexParts = decomposeConvex(simplified);
38
+ const decomposition = new ConvexDecompositionData();
39
+ decomposition.components = convexParts.map((part) => {
40
+ const p = new PolygonData();
41
+ p.vertices = part;
42
+ return p;
43
+ });
44
+ convexDecompositions.push(decomposition);
45
+ }
46
+ return { polygons, convexDecompositions };
47
+ }
48
+ async function traceSpriteSheet(sheet, options) {
49
+ const { tolerance, alphaThreshold = 1, highQuality = true, composite = true, perLayer = false } = options;
50
+ const result = new SpriteGeometryData();
51
+ // Load the sheet-level pixel source image.
52
+ let sheetImg = null;
53
+ if (sheet.data.pixelSource) {
54
+ sheetImg = await readPixelSource(sheet.data.pixelSource);
55
+ }
56
+ for (const sprite of sheet.sprites) {
57
+ const entry = new SpriteGeometryEntryData();
58
+ entry.spriteName = sprite.data.name;
59
+ entry.simplifyTolerance = tolerance;
60
+ // Try sprite-level pixel source, fall back to sheet image.
61
+ let spriteImg = sheetImg;
62
+ if (sprite.data.pixelSource) {
63
+ const img = await readPixelSource(sprite.data.pixelSource);
64
+ if (img)
65
+ spriteImg = img;
66
+ }
67
+ for (const frame of sprite.data.frames) {
68
+ const frameGeom = new FrameGeometryData();
69
+ frameGeom.frameIndex = frame.index;
70
+ // Per-layer tracing.
71
+ if (perLayer && spriteImg) {
72
+ for (const frameLayer of frame.layers) {
73
+ const layerGeom = new FrameLayerGeometryData();
74
+ layerGeom.layerIndex = frameLayer.layerIndex;
75
+ // Extract this layer's pixel region from the sprite sheet.
76
+ const layerImg = new Jimp({
77
+ width: frameLayer.size.width,
78
+ height: frameLayer.size.height
79
+ });
80
+ layerImg.blit({
81
+ src: spriteImg,
82
+ srcX: frameLayer.sheetPosition.x,
83
+ srcY: frameLayer.sheetPosition.y,
84
+ srcW: frameLayer.size.width,
85
+ srcH: frameLayer.size.height,
86
+ x: 0,
87
+ y: 0
88
+ });
89
+ const imageData = {
90
+ width: layerImg.width,
91
+ height: layerImg.height,
92
+ data: new Uint8Array(layerImg.bitmap.data)
93
+ };
94
+ const { polygons, convexDecompositions } = traceAndProcess(imageData, tolerance, alphaThreshold, highQuality);
95
+ // Offset polygons to sprite-local coordinates.
96
+ for (const poly of polygons) {
97
+ for (const v of poly.vertices) {
98
+ v.x += frameLayer.spritePosition.x;
99
+ v.y += frameLayer.spritePosition.y;
100
+ }
101
+ }
102
+ for (const decomposition of convexDecompositions) {
103
+ for (const comp of decomposition.components) {
104
+ for (const v of comp.vertices) {
105
+ v.x += frameLayer.spritePosition.x;
106
+ v.y += frameLayer.spritePosition.y;
107
+ }
108
+ }
109
+ }
110
+ layerGeom.polygons = polygons;
111
+ layerGeom.convexDecompositions = convexDecompositions;
112
+ frameGeom.layers.push(layerGeom);
113
+ }
114
+ }
115
+ // Composite-frame tracing.
116
+ if (composite) {
117
+ const instance = new ProtoSpriteInstance(sprite);
118
+ instance.animationState.currentFrame = frame.index;
119
+ const compositeImg = await renderSpriteInstance(instance);
120
+ const compositeImageData = {
121
+ width: compositeImg.width,
122
+ height: compositeImg.height,
123
+ data: new Uint8Array(compositeImg.bitmap.data)
124
+ };
125
+ const { polygons, convexDecompositions } = traceAndProcess(compositeImageData, tolerance, alphaThreshold, highQuality);
126
+ const compositeGeom = new CompositeFrameGeometryData();
127
+ compositeGeom.polygons = polygons;
128
+ compositeGeom.convexDecompositions = convexDecompositions;
129
+ frameGeom.composite = compositeGeom;
130
+ }
131
+ entry.frames.push(frameGeom);
132
+ }
133
+ result.entries.push(entry);
134
+ }
135
+ return result;
136
+ }
137
+
138
+ export { decomposeConvex, simplifyPolygon, traceContours, traceSpriteSheet };
139
+ //# sourceMappingURL=trace.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trace.js","sources":["../src/trace/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;;;AAwCA,eAAe,eAAe,CAC5B,WAAwB,EAAA;AAExB,IAAA,IAAI,IAAI,CAAC,yBAAyB,CAAC,WAAW,CAAC,EAAE;AAC/C,QAAA,IAAI,WAAW,CAAC,OAAO,EAAE;AACvB,YAAA,MAAM,GAAG,GAAG,CAAA,sBAAA,EAAyB,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;AAC1F,YAAA,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,WAAW,EAAE,EAAE,EAAE,CAAC;QAC5C;AACA,QAAA,OAAO,IAAI;IACb;AACA,IAAA,IAAI,IAAI,CAAC,yBAAyB,CAAC,WAAW,CAAC,EAAE;QAC/C,MAAM,aAAa,GAAG,WAAW,CAAC,GAAG,IAAI,WAAW,CAAC,QAAQ;AAC7D,QAAA,IAAI,CAAC,aAAa;AAAE,YAAA,OAAO,IAAI;AAC/B,QAAA,OAAO,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,EAAE,WAAW,EAAE,EAAE,EAAE,CAAC;IACtD;AACA,IAAA,OAAO,IAAI;AACb;AAEA,SAAS,eAAe,CACtB,SAAuE,EACvE,SAAiB,E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