@cyclonium/canvas-3d 0.0.100

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.
@@ -0,0 +1,58 @@
1
+ import type { Vec3 } from 'cc';
2
+ import type { Canvas3DOptions, Canvas3DW2, Canvas3DW3 } from './types.js';
3
+ export { Canvas3DLineCap, Canvas3DLineJoin } from './types.js';
4
+ export type { Canvas3DBoxOptions, Canvas3DCapsuleOptions, Canvas3DCylinderOptions, Canvas3DDiscOptions, Canvas3DLineOptions, Canvas3DOptions, Canvas3DPrimitiveMode, Canvas3DPrimitiveOptions, Canvas3DQuadOptions, Canvas3DRingOptions, Canvas3DSphereOptions, Canvas3DW2, Canvas3DW3, Canvas3DW3Scope, } from './types.js';
5
+ export declare class Canvas3D {
6
+ constructor(opts: Canvas3DOptions);
7
+ get w2(): Canvas3DW2;
8
+ get w3(): Canvas3DW3;
9
+ get lineWidth(): number;
10
+ set lineWidth(value: number);
11
+ get strokeWidth(): number;
12
+ set strokeWidth(value: number);
13
+ get lineJoin(): import("./types.js").Canvas3DLineJoin;
14
+ set lineJoin(value: import("./types.js").Canvas3DLineJoin);
15
+ get lineCap(): import("./types.js").Canvas3DLineCap;
16
+ set lineCap(value: import("./types.js").Canvas3DLineCap);
17
+ get miterLimit(): number;
18
+ set miterLimit(value: number);
19
+ get lineDash(): number[];
20
+ set lineDash(value: number[]);
21
+ get lineDashOffset(): number;
22
+ set lineDashOffset(value: number);
23
+ get strokeColor(): import("cc").math.Color;
24
+ set strokeColor(value: import("cc").math.Color);
25
+ get fillColor(): import("cc").math.Color;
26
+ set fillColor(value: import("cc").math.Color);
27
+ beginPath(): this;
28
+ closePath(): this;
29
+ moveTo(point: Vec3): this;
30
+ moveTo(x: number, y: number, z?: number): this;
31
+ lineTo(point: Vec3): this;
32
+ lineTo(x: number, y: number, z?: number): this;
33
+ ray(dir: Vec3, length: number): this;
34
+ box(center: Vec3, halfExtents: number | Vec3): this;
35
+ circle(center: Vec3, radius: number): this;
36
+ stroke(): this;
37
+ fill(): this;
38
+ getLineDash(): number[];
39
+ setLineDash(value: readonly number[]): void;
40
+ /**
41
+ * 渲染当前画布。通常在帧结束时调用。
42
+ */
43
+ commit(): void;
44
+ destroy(): void;
45
+ private readonly _node;
46
+ private readonly _w2;
47
+ private readonly _w3;
48
+ private readonly _drawCommands;
49
+ private readonly _materials;
50
+ private _renderRecord;
51
+ private readonly _enqueueGeometry;
52
+ private _getMaterial;
53
+ private _canReuseRenderRecord;
54
+ private _createRenderingDrawCommands;
55
+ private _destroyRenderRecord;
56
+ private _clearPendingDraws;
57
+ }
58
+ //# sourceMappingURL=canvas-3d.d.ts.map
@@ -0,0 +1,254 @@
1
+ import { gfx, Material, renderer } from 'cc';
2
+ import { appendMeshGeometry, createOrUpdateRenderingSubMeshRecord, defaultDrawOptions, getMaterialKey, hasGeometry, } from './geometry.js';
3
+ import { Canvas3DW2Impl } from './w2.js';
4
+ import { Canvas3DW3Impl } from './w3.js';
5
+ export { Canvas3DLineCap, Canvas3DLineJoin } from './types.js';
6
+ export class Canvas3D {
7
+ constructor(opts) {
8
+ this._node = opts.node;
9
+ this._w2 = new Canvas3DW2Impl(this._enqueueGeometry);
10
+ this._w3 = new Canvas3DW3Impl(this._enqueueGeometry);
11
+ }
12
+ get w2() {
13
+ return this._w2;
14
+ }
15
+ get w3() {
16
+ return this._w3;
17
+ }
18
+ get lineWidth() {
19
+ return this._w2.lineWidth;
20
+ }
21
+ set lineWidth(value) {
22
+ this._w2.lineWidth = value;
23
+ }
24
+ get strokeWidth() {
25
+ return this._w2.strokeWidth;
26
+ }
27
+ set strokeWidth(value) {
28
+ this._w2.strokeWidth = value;
29
+ }
30
+ get lineJoin() {
31
+ return this._w2.lineJoin;
32
+ }
33
+ set lineJoin(value) {
34
+ this._w2.lineJoin = value;
35
+ }
36
+ get lineCap() {
37
+ return this._w2.lineCap;
38
+ }
39
+ set lineCap(value) {
40
+ this._w2.lineCap = value;
41
+ }
42
+ get miterLimit() {
43
+ return this._w2.miterLimit;
44
+ }
45
+ set miterLimit(value) {
46
+ this._w2.miterLimit = value;
47
+ }
48
+ get lineDash() {
49
+ return this.getLineDash();
50
+ }
51
+ set lineDash(value) {
52
+ this.setLineDash(value);
53
+ }
54
+ get lineDashOffset() {
55
+ return this._w2.lineDashOffset;
56
+ }
57
+ set lineDashOffset(value) {
58
+ this._w2.lineDashOffset = value;
59
+ }
60
+ get strokeColor() {
61
+ return this._w2.strokeColor;
62
+ }
63
+ set strokeColor(value) {
64
+ this._w2.strokeColor = value;
65
+ }
66
+ get fillColor() {
67
+ return this._w2.fillColor;
68
+ }
69
+ set fillColor(value) {
70
+ this._w2.fillColor = value;
71
+ }
72
+ beginPath() {
73
+ this._w2.beginPath();
74
+ return this;
75
+ }
76
+ closePath() {
77
+ this._w2.closePath();
78
+ return this;
79
+ }
80
+ moveTo(pointOrX, y, z) {
81
+ if (typeof pointOrX === 'number') {
82
+ this._w2.moveTo(pointOrX, y ?? 0, z);
83
+ }
84
+ else {
85
+ this._w2.moveTo(pointOrX);
86
+ }
87
+ return this;
88
+ }
89
+ lineTo(pointOrX, y, z) {
90
+ if (typeof pointOrX === 'number') {
91
+ this._w2.lineTo(pointOrX, y ?? 0, z);
92
+ }
93
+ else {
94
+ this._w2.lineTo(pointOrX);
95
+ }
96
+ return this;
97
+ }
98
+ ray(dir, length) {
99
+ this._w2.ray(dir, length);
100
+ return this;
101
+ }
102
+ box(center, halfExtents) {
103
+ this._w2.box(center, halfExtents);
104
+ return this;
105
+ }
106
+ circle(center, radius) {
107
+ this._w2.circle(center, radius);
108
+ return this;
109
+ }
110
+ stroke() {
111
+ this._w2.stroke();
112
+ return this;
113
+ }
114
+ fill() {
115
+ this._w2.fill();
116
+ return this;
117
+ }
118
+ getLineDash() {
119
+ return this._w2.getLineDash();
120
+ }
121
+ setLineDash(value) {
122
+ this._w2.setLineDash(value);
123
+ }
124
+ /**
125
+ * 渲染当前画布。通常在帧结束时调用。
126
+ */
127
+ commit() {
128
+ const renderScene = this._node.scene?.renderScene;
129
+ if (!renderScene) {
130
+ this._destroyRenderRecord();
131
+ this._clearPendingDraws();
132
+ return;
133
+ }
134
+ const canReuseRenderRecord = this._canReuseRenderRecord(renderScene);
135
+ if (!canReuseRenderRecord) {
136
+ this._destroyRenderRecord();
137
+ }
138
+ const renderingDrawCommands = this._createRenderingDrawCommands(renderScene.root.device, canReuseRenderRecord ? this._renderRecord?.renderingSubMeshRecords : undefined);
139
+ if (renderingDrawCommands.length === 0) {
140
+ this._destroyRenderRecord();
141
+ this._clearPendingDraws();
142
+ return;
143
+ }
144
+ const model = canReuseRenderRecord ? this._renderRecord.model : new renderer.scene.Model();
145
+ model.node = this._node;
146
+ model.transform = this._node;
147
+ model.visFlags = this._node.layer;
148
+ renderingDrawCommands.forEach((drawCommand, iSubModel) => {
149
+ model.initSubModel(iSubModel, drawCommand.renderingSubMeshRecord.renderingSubMesh, drawCommand.material);
150
+ });
151
+ if (!canReuseRenderRecord) {
152
+ renderScene.addModel(model);
153
+ }
154
+ this._renderRecord = {
155
+ renderScene,
156
+ renderingSubMeshRecords: renderingDrawCommands.map((drawCommand) => {
157
+ return drawCommand.renderingSubMeshRecord;
158
+ }),
159
+ model,
160
+ };
161
+ this._clearPendingDraws();
162
+ }
163
+ destroy() {
164
+ this._destroyRenderRecord();
165
+ for (const material of this._materials.values()) {
166
+ material.destroy();
167
+ }
168
+ this._materials.clear();
169
+ }
170
+ _node;
171
+ _w2;
172
+ _w3;
173
+ _drawCommands = [];
174
+ _materials = new Map();
175
+ _renderRecord = undefined;
176
+ _enqueueGeometry = (geometry, options = defaultDrawOptions) => {
177
+ if (!hasGeometry(geometry)) {
178
+ return;
179
+ }
180
+ const lastDrawCommand = this._drawCommands[this._drawCommands.length - 1];
181
+ if (lastDrawCommand && drawOptionsEqual(lastDrawCommand, options)) {
182
+ appendMeshGeometry(lastDrawCommand.geometry, geometry);
183
+ return;
184
+ }
185
+ this._drawCommands.push({
186
+ geometry,
187
+ depthTest: options.depthTest,
188
+ depthWrite: options.depthWrite,
189
+ });
190
+ };
191
+ _getMaterial(options) {
192
+ const materialKey = getMaterialKey(options);
193
+ let material = this._materials.get(materialKey);
194
+ if (!material) {
195
+ material = new Material();
196
+ material.reset({
197
+ effectName: 'builtin-unlit',
198
+ defines: {
199
+ USE_VERTEX_COLOR: true,
200
+ },
201
+ states: {
202
+ rasterizerState: {
203
+ cullMode: gfx.CullMode.NONE,
204
+ },
205
+ depthStencilState: {
206
+ depthTest: options.depthTest,
207
+ depthWrite: options.depthWrite,
208
+ },
209
+ },
210
+ });
211
+ this._materials.set(materialKey, material);
212
+ }
213
+ return material;
214
+ }
215
+ _canReuseRenderRecord(renderScene) {
216
+ return this._renderRecord !== undefined
217
+ && this._renderRecord.renderScene === renderScene
218
+ && this._renderRecord.renderingSubMeshRecords.length === this._drawCommands.length;
219
+ }
220
+ _createRenderingDrawCommands(device, reusableSubMeshRecords) {
221
+ const renderingDrawCommands = [];
222
+ for (let iDrawCommand = 0; iDrawCommand < this._drawCommands.length; iDrawCommand++) {
223
+ const drawCommand = this._drawCommands[iDrawCommand];
224
+ const subMeshRecord = createOrUpdateRenderingSubMeshRecord(device, drawCommand.geometry, reusableSubMeshRecords?.[iDrawCommand]);
225
+ if (subMeshRecord) {
226
+ renderingDrawCommands.push({
227
+ renderingSubMeshRecord: subMeshRecord,
228
+ material: this._getMaterial(drawCommand),
229
+ });
230
+ }
231
+ }
232
+ return renderingDrawCommands;
233
+ }
234
+ _destroyRenderRecord() {
235
+ if (!this._renderRecord) {
236
+ return;
237
+ }
238
+ const { renderScene, renderingSubMeshRecords, model, } = this._renderRecord;
239
+ this._renderRecord = undefined;
240
+ renderScene.removeModel(model);
241
+ for (const record of renderingSubMeshRecords) {
242
+ record.renderingSubMesh.destroy();
243
+ }
244
+ model.destroy();
245
+ }
246
+ _clearPendingDraws() {
247
+ this._drawCommands.length = 0;
248
+ this._w2.beginPath();
249
+ }
250
+ }
251
+ function drawOptionsEqual(a, b) {
252
+ return a.depthTest === b.depthTest && a.depthWrite === b.depthWrite;
253
+ }
254
+ //# sourceMappingURL=canvas-3d.js.map
@@ -0,0 +1,117 @@
1
+ import { Color, gfx, Vec2, Vec3 } from 'cc';
2
+ import type { Canvas3DCapsuleOptions, Canvas3DCylinderOptions, Canvas3DDiscOptions, Canvas3DPrimitiveOptions, Canvas3DQuadOptions, Canvas3DRingOptions, Canvas3DSphereOptions, DrawOptions, EnqueueGeometry, MeshGeometry, PrimitiveDrawState, RenderingSubMeshRecord, StrokeStyle, SubPath } from './types.js';
3
+ export declare enum PrimitiveEntryType {
4
+ line = "line",
5
+ box = "box",
6
+ quad = "quad",
7
+ cylinder = "cylinder",
8
+ disc = "disc",
9
+ ring = "ring",
10
+ sphere = "sphere",
11
+ capsule = "capsule"
12
+ }
13
+ export type PrimitiveEntry = {
14
+ readonly type: PrimitiveEntryType.line;
15
+ readonly from: Vec3;
16
+ readonly to: Vec3;
17
+ } | {
18
+ readonly type: PrimitiveEntryType.box;
19
+ readonly center: Vec3;
20
+ readonly halfExtents: Vec3;
21
+ } | {
22
+ readonly type: PrimitiveEntryType.quad;
23
+ readonly corners: readonly Vec3[];
24
+ } | {
25
+ readonly type: PrimitiveEntryType.cylinder;
26
+ readonly from: Vec3;
27
+ readonly to: Vec3;
28
+ readonly radius: number;
29
+ readonly radialSegments: number;
30
+ readonly wireSegments: number;
31
+ } | {
32
+ readonly type: PrimitiveEntryType.disc;
33
+ readonly center: Vec3;
34
+ readonly normal: Vec3;
35
+ readonly radius: number;
36
+ readonly segments: number;
37
+ } | {
38
+ readonly type: PrimitiveEntryType.ring;
39
+ readonly center: Vec3;
40
+ readonly normal: Vec3;
41
+ readonly innerRadius: number;
42
+ readonly outerRadius: number;
43
+ readonly segments: number;
44
+ } | {
45
+ readonly type: PrimitiveEntryType.sphere;
46
+ readonly center: Vec3;
47
+ readonly radius: number;
48
+ readonly latitudeSegments: number;
49
+ readonly longitudeSegments: number;
50
+ readonly wireSegments: number;
51
+ } | {
52
+ readonly type: PrimitiveEntryType.capsule;
53
+ readonly from: Vec3;
54
+ readonly to: Vec3;
55
+ readonly radius: number;
56
+ readonly radialSegments: number;
57
+ readonly wireSegments: number;
58
+ readonly capSegments: number;
59
+ };
60
+ export declare const epsilon = 0.000001;
61
+ export declare const roundSegmentRadians: number;
62
+ export declare const defaultDrawOptions: DrawOptions;
63
+ export declare function createMeshGeometry(): MeshGeometry;
64
+ export declare function hasGeometry(geometry: MeshGeometry): boolean;
65
+ export declare function appendMeshGeometry(out: MeshGeometry, geometry: MeshGeometry): void;
66
+ export declare function resolvePoint(pointOrX: Vec3 | number, y?: number, z?: number): Vec3;
67
+ export declare function setColor(out: Color, value: Readonly<Color> | string): void;
68
+ export declare function getMaterialKey(options: DrawOptions): string;
69
+ export declare function drawPrimitive(enqueueGeometry: EnqueueGeometry, baseState: PrimitiveDrawState, entries: readonly PrimitiveEntry[], options: Canvas3DPrimitiveOptions | undefined): void;
70
+ export declare function createPrimitiveDrawState(): PrimitiveDrawState;
71
+ export declare function clonePrimitiveDrawState(state: PrimitiveDrawState): PrimitiveDrawState;
72
+ export declare function copyPrimitiveDrawState(out: PrimitiveDrawState, state: PrimitiveDrawState): PrimitiveDrawState;
73
+ export declare function resolveCapsule(options: Canvas3DCapsuleOptions): {
74
+ readonly from: Vec3;
75
+ readonly to: Vec3;
76
+ readonly radius: number;
77
+ };
78
+ export declare function resolveCylinder(options: Canvas3DCylinderOptions): {
79
+ readonly from: Vec3;
80
+ readonly to: Vec3;
81
+ readonly radius: number;
82
+ };
83
+ export declare function createQuadCorners(options: Canvas3DQuadOptions): Vec3[];
84
+ export declare function resolveDiscRadius(options: Canvas3DDiscOptions): number;
85
+ export declare function resolveCircleSegments(value: number | undefined): number;
86
+ export declare function resolveCylinderSegments(options: Canvas3DCylinderOptions): {
87
+ readonly radialSegments: number;
88
+ readonly wireSegments: number;
89
+ };
90
+ export declare function resolveDiscSegments(options: Canvas3DDiscOptions): number;
91
+ export declare function resolveRingSegments(options: Canvas3DRingOptions): number;
92
+ export declare function resolveSphereSegments(options: Canvas3DSphereOptions): {
93
+ readonly latitudeSegments: number;
94
+ readonly longitudeSegments: number;
95
+ readonly wireSegments: number;
96
+ };
97
+ export declare function resolveCapsuleSegments(options: Canvas3DCapsuleOptions): {
98
+ readonly radialSegments: number;
99
+ readonly wireSegments: number;
100
+ readonly capSegments: number;
101
+ };
102
+ export declare function resolveRingRadii(options: Canvas3DRingOptions): {
103
+ readonly innerRadius: number;
104
+ readonly outerRadius: number;
105
+ };
106
+ export declare function resolveQuadHalfExtents(halfExtents: number | Vec2): Vec2;
107
+ export declare function normalizeLineDash(value: readonly number[]): number[];
108
+ export declare function appendSubPathStroke(geometry: MeshGeometry, subPath: SubPath, style: StrokeStyle, color: Color): void;
109
+ export declare function appendSubPathFill(geometry: MeshGeometry, subPath: SubPath, color: Color): void;
110
+ export declare function appendBoxStroke(geometry: MeshGeometry, center: Vec3, halfExtents: Vec3, style: StrokeStyle, color: Color): void;
111
+ export declare function appendBoxFill(geometry: MeshGeometry, center: Vec3, halfExtents: Vec3, color: Color): void;
112
+ export declare function appendCircleStroke(geometry: MeshGeometry, center: Vec3, radius: number, style: StrokeStyle, color: Color): void;
113
+ export declare function appendCircleFill(geometry: MeshGeometry, center: Vec3, radius: number, color: Color): void;
114
+ export declare function createOrUpdateRenderingSubMeshRecord(device: gfx.Device, geometry: MeshGeometry, record?: RenderingSubMeshRecord): RenderingSubMeshRecord | undefined;
115
+ export declare function resolveHalfExtents(halfExtents: number | Vec3): Vec3;
116
+ export declare function normalizeArcDelta(startAngle: number, endAngle: number, counterclockwise: boolean): number;
117
+ //# sourceMappingURL=geometry.d.ts.map