@combos-fun/plugin-renderer-img 0.1.14-beta.0 → 0.1.14-beta.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.
|
@@ -29,116 +29,6 @@ tslib.__decorate([
|
|
|
29
29
|
})
|
|
30
30
|
], Img$1.prototype, "resource", void 0);
|
|
31
31
|
|
|
32
|
-
/** Compressed targets only. RGBA is omitted so Basis is never expanded to uncompressed pixels. */
|
|
33
|
-
const COMPRESSED_TARGETS = [
|
|
34
|
-
'astc-4x4-unorm',
|
|
35
|
-
'bc7-rgba-unorm',
|
|
36
|
-
'bc3-rgba-unorm',
|
|
37
|
-
'etc2-rgba8unorm',
|
|
38
|
-
];
|
|
39
|
-
function asArrayBuffer(data) {
|
|
40
|
-
if (data instanceof ArrayBuffer)
|
|
41
|
-
return data;
|
|
42
|
-
if (ArrayBuffer.isView(data)) {
|
|
43
|
-
const view = data;
|
|
44
|
-
return view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
function loadHtmlImage(url) {
|
|
49
|
-
return new Promise((resolve, reject) => {
|
|
50
|
-
const image = new Image();
|
|
51
|
-
image.crossOrigin = 'anonymous';
|
|
52
|
-
image.onload = () => resolve(image);
|
|
53
|
-
image.onerror = () => reject(new Error(`Failed to load image ${url}`));
|
|
54
|
-
image.src = url;
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
async function textureFromElement(image) {
|
|
58
|
-
if (typeof createImageBitmap !== 'function')
|
|
59
|
-
return pixi_js.Texture.from(image);
|
|
60
|
-
let bitmap = null;
|
|
61
|
-
try {
|
|
62
|
-
bitmap = await createImageBitmap(image);
|
|
63
|
-
return pixi_js.Texture.from(bitmap);
|
|
64
|
-
}
|
|
65
|
-
catch {
|
|
66
|
-
try {
|
|
67
|
-
bitmap?.close();
|
|
68
|
-
}
|
|
69
|
-
catch {
|
|
70
|
-
// ImageBitmap.close throws once the bitmap is already detached.
|
|
71
|
-
}
|
|
72
|
-
return pixi_js.Texture.from(image);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
async function loadPixiKtx2(url) {
|
|
76
|
-
try {
|
|
77
|
-
const supported = await pixi_js.getSupportedTextureFormats();
|
|
78
|
-
const compressed = COMPRESSED_TARGETS.filter((format) => supported.includes(format));
|
|
79
|
-
if (!compressed.length)
|
|
80
|
-
return null;
|
|
81
|
-
const options = await pixi_js.loadKTX2onWorker(url, compressed);
|
|
82
|
-
if (!options?.resource || !COMPRESSED_TARGETS.includes(options.format)) {
|
|
83
|
-
return null;
|
|
84
|
-
}
|
|
85
|
-
return new pixi_js.Texture({ source: new pixi_js.CompressedSource(options) });
|
|
86
|
-
}
|
|
87
|
-
catch {
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
async function loadCompressed(res, imageData) {
|
|
92
|
-
const slot = res.src?.image;
|
|
93
|
-
const url = engine.compressedTextureUrl(slot);
|
|
94
|
-
if (!url)
|
|
95
|
-
return null;
|
|
96
|
-
const buffer = asArrayBuffer(imageData);
|
|
97
|
-
if (!buffer)
|
|
98
|
-
return loadPixiKtx2(url);
|
|
99
|
-
const blobUrl = URL.createObjectURL(new Blob([buffer]));
|
|
100
|
-
try {
|
|
101
|
-
const texture = await loadPixiKtx2(blobUrl);
|
|
102
|
-
if (texture)
|
|
103
|
-
return texture;
|
|
104
|
-
}
|
|
105
|
-
finally {
|
|
106
|
-
URL.revokeObjectURL(blobUrl);
|
|
107
|
-
}
|
|
108
|
-
return loadPixiKtx2(url);
|
|
109
|
-
}
|
|
110
|
-
/** PNG/JPG/WebP stay on Texture.from. A KTX2 companion is used only when the GPU can take it. */
|
|
111
|
-
async function createImageTexture(res) {
|
|
112
|
-
const imageData = res.data?.image;
|
|
113
|
-
const compressed = await loadCompressed(res, imageData);
|
|
114
|
-
if (compressed)
|
|
115
|
-
return compressed;
|
|
116
|
-
if (typeof HTMLImageElement !== 'undefined' && imageData instanceof HTMLImageElement) {
|
|
117
|
-
return textureFromElement(imageData);
|
|
118
|
-
}
|
|
119
|
-
const fallback = engine.imageFallbackUrl(res.src?.image);
|
|
120
|
-
if (!fallback)
|
|
121
|
-
return undefined;
|
|
122
|
-
try {
|
|
123
|
-
return textureFromElement(await loadHtmlImage(fallback));
|
|
124
|
-
}
|
|
125
|
-
catch {
|
|
126
|
-
return undefined;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
function releaseImageTexture(texture) {
|
|
130
|
-
const source = texture.source?.resource;
|
|
131
|
-
texture.destroy(true);
|
|
132
|
-
if (typeof ImageBitmap !== 'undefined' && source instanceof ImageBitmap) {
|
|
133
|
-
try {
|
|
134
|
-
source.close();
|
|
135
|
-
}
|
|
136
|
-
catch {
|
|
137
|
-
// Already closed by the texture source.
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
32
|
let imgSystem = null;
|
|
143
33
|
function holdsTexture(sprite, texture) {
|
|
144
34
|
return sprite.image === texture || sprite.sprite.texture === texture;
|
|
@@ -175,7 +65,7 @@ function attachTexture(name, texture) {
|
|
|
175
65
|
}
|
|
176
66
|
}
|
|
177
67
|
engine.resource.registerInstance(engine.RESOURCE_TYPE.IMAGE, async (res) => {
|
|
178
|
-
const texture = await createImageTexture(res);
|
|
68
|
+
const texture = await pluginRenderer.createImageTexture(res);
|
|
179
69
|
if (texture)
|
|
180
70
|
attachTexture(res.name, texture);
|
|
181
71
|
return texture;
|
|
@@ -185,7 +75,7 @@ engine.resource.registerDestroy(engine.RESOURCE_TYPE.IMAGE, ({ instance }) => {
|
|
|
185
75
|
return;
|
|
186
76
|
const texture = instance;
|
|
187
77
|
detachTexture(texture);
|
|
188
|
-
releaseImageTexture(texture);
|
|
78
|
+
pluginRenderer.releaseImageTexture(texture);
|
|
189
79
|
});
|
|
190
80
|
let Img = class Img extends pluginRenderer.Renderer {
|
|
191
81
|
constructor() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin-renderer-img.cjs.js","sources":["../lib/component.ts","../lib/imageTexture.ts","../lib/system.ts"],"sourcesContent":["import { Field } from '@combos-fun/inspector-decorator';\nimport { Component } from '@combos-fun/engine';\n\nexport interface ImgParams {\n resource: string;\n}\n\nexport default class Img extends Component<ImgParams> {\n static componentName: string = 'Img';\n\n @Field({\n type: 'string',\n group: 'Img',\n label: 'resource',\n description: 'Image resource id from the asset registry.',\n editor: 'text',\n })\n resource: string = '';\n\n init(obj?: ImgParams) {\n if (obj && obj.resource) {\n this.resource = obj.resource;\n }\n }\n}\n","import { compressedTextureUrl, imageFallbackUrl } from '@combos-fun/engine';\nimport type { ResourceStruct } from '@combos-fun/engine';\nimport { CompressedSource, Texture, getSupportedTextureFormats, loadKTX2onWorker } from 'pixi.js';\nimport type { TEXTURE_FORMATS } from 'pixi.js';\n\n/** Compressed targets only. RGBA is omitted so Basis is never expanded to uncompressed pixels. */\nconst COMPRESSED_TARGETS = [\n 'astc-4x4-unorm',\n 'bc7-rgba-unorm',\n 'bc3-rgba-unorm',\n 'etc2-rgba8unorm',\n] as const satisfies readonly TEXTURE_FORMATS[];\n\nfunction asArrayBuffer(data: unknown): ArrayBuffer | null {\n if (data instanceof ArrayBuffer) return data;\n if (ArrayBuffer.isView(data)) {\n const view = data as ArrayBufferView;\n return view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength) as ArrayBuffer;\n }\n return null;\n}\n\nfunction loadHtmlImage(url: string): Promise<HTMLImageElement> {\n return new Promise((resolve, reject) => {\n const image = new Image();\n image.crossOrigin = 'anonymous';\n image.onload = () => resolve(image);\n image.onerror = () => reject(new Error(`Failed to load image ${url}`));\n image.src = url;\n });\n}\n\nasync function textureFromElement(image: HTMLImageElement): Promise<Texture> {\n if (typeof createImageBitmap !== 'function') return Texture.from(image);\n let bitmap: ImageBitmap | null = null;\n try {\n bitmap = await createImageBitmap(image);\n return Texture.from(bitmap);\n } catch {\n try {\n bitmap?.close();\n } catch {\n // ImageBitmap.close throws once the bitmap is already detached.\n }\n return Texture.from(image);\n }\n}\n\nasync function loadPixiKtx2(url: string): Promise<Texture | null> {\n try {\n const supported = await getSupportedTextureFormats();\n const compressed = COMPRESSED_TARGETS.filter((format) => supported.includes(format));\n if (!compressed.length) return null;\n const options = await loadKTX2onWorker(url, compressed);\n if (!options?.resource || !COMPRESSED_TARGETS.includes(options.format as typeof COMPRESSED_TARGETS[number])) {\n return null;\n }\n return new Texture({ source: new CompressedSource(options) });\n } catch {\n return null;\n }\n}\n\nasync function loadCompressed(res: ResourceStruct, imageData: unknown): Promise<Texture | null> {\n const slot = res.src?.image;\n const url = compressedTextureUrl(slot);\n if (!url) return null;\n const buffer = asArrayBuffer(imageData);\n if (!buffer) return loadPixiKtx2(url);\n const blobUrl = URL.createObjectURL(new Blob([buffer]));\n try {\n const texture = await loadPixiKtx2(blobUrl);\n if (texture) return texture;\n } finally {\n URL.revokeObjectURL(blobUrl);\n }\n return loadPixiKtx2(url);\n}\n\n/** PNG/JPG/WebP stay on Texture.from. A KTX2 companion is used only when the GPU can take it. */\nexport async function createImageTexture(res: ResourceStruct): Promise<Texture | undefined> {\n const imageData = res.data?.image;\n const compressed = await loadCompressed(res, imageData);\n if (compressed) return compressed;\n if (typeof HTMLImageElement !== 'undefined' && imageData instanceof HTMLImageElement) {\n return textureFromElement(imageData);\n }\n const fallback = imageFallbackUrl(res.src?.image);\n if (!fallback) return undefined;\n try {\n return textureFromElement(await loadHtmlImage(fallback));\n } catch {\n return undefined;\n }\n}\n\nexport function releaseImageTexture(texture: Texture) {\n const source = texture.source?.resource;\n texture.destroy(true);\n if (typeof ImageBitmap !== 'undefined' && source instanceof ImageBitmap) {\n try {\n source.close();\n } catch {\n // Already closed by the texture source.\n }\n }\n}\n","import { GameObject, decorators, resource, ComponentChanged, RESOURCE_TYPE, OBSERVER_TYPE } from '@combos-fun/engine';\nimport { RendererManager, ContainerManager, RendererSystem, Renderer, Transform2D } from '@combos-fun/plugin-renderer';\nimport { Sprite } from '@combos-fun/renderer-adapter';\nimport { Texture } from 'pixi.js';\nimport ImgComponent from './component';\nimport { createImageTexture, releaseImageTexture } from './imageTexture';\n\nlet imgSystem: Img | null = null;\n\nfunction holdsTexture(sprite: Sprite, texture: Texture) {\n return sprite.image === texture || sprite.sprite.texture === texture;\n}\n\nfunction detachTexture(texture: Texture) {\n if (!imgSystem || texture === Texture.EMPTY) return;\n for (const key in imgSystem.imgs) {\n const sprite = imgSystem.imgs[key];\n if (!sprite?.sprite || sprite.sprite.destroyed) continue;\n if (!holdsTexture(sprite, texture)) continue;\n // Assign directly. The adapter setter's HTMLImageElement branch calls destroy(false).\n sprite.sprite.texture = Texture.EMPTY;\n sprite._image = Texture.EMPTY;\n }\n}\n\nfunction attachTexture(name: string, texture: Texture) {\n if (!imgSystem || !name || texture === Texture.EMPTY) return;\n const gameObjects = imgSystem.game?.gameObjects;\n if (!gameObjects) return;\n for (const key in imgSystem.imgs) {\n const sprite = imgSystem.imgs[key];\n if (!sprite?.sprite || sprite.sprite.destroyed) continue;\n const gameObject = gameObjects.find((item) => item.id === parseInt(key));\n if (gameObject?.getComponent(ImgComponent)?.resource !== name) continue;\n sprite.sprite.texture = texture;\n sprite._image = texture;\n }\n}\n\nresource.registerInstance(RESOURCE_TYPE.IMAGE, async (res) => {\n const texture = await createImageTexture(res);\n if (texture) attachTexture(res.name, texture);\n return texture;\n});\nresource.registerDestroy(RESOURCE_TYPE.IMAGE, ({ instance }) => {\n if (!instance || instance === Texture.EMPTY) return;\n const texture = instance as Texture;\n detachTexture(texture);\n releaseImageTexture(texture);\n});\n\n@decorators.componentObserver({\n Img: [{ prop: ['resource'], deep: false }],\n})\nexport default class Img extends Renderer {\n static systemName = 'Img';\n name: string = 'Img';\n imgs: { [propName: number]: Sprite } = {};\n renderSystem: RendererSystem;\n rendererManager: RendererManager;\n containerManager: ContainerManager;\n init() {\n imgSystem = this;\n this.renderSystem = this.game.getSystem(RendererSystem) as RendererSystem;\n this.renderSystem.rendererManager.register(this);\n }\n rendererUpdate(gameObject: GameObject) {\n const { width, height } = gameObject.getComponent(Transform2D)?.size ?? { width: 0, height: 0 };\n if (this.imgs[gameObject.id]) {\n this.imgs[gameObject.id].sprite.width = width;\n this.imgs[gameObject.id].sprite.height = height;\n }\n }\n async componentChanged(changed: ComponentChanged) {\n if (changed.componentName === 'Img') {\n const component: ImgComponent = changed.component as ImgComponent;\n if (changed.type === OBSERVER_TYPE.ADD) {\n const sprite = new Sprite(null);\n this.imgs[changed.gameObject.id] = sprite;\n this.containerManager.getContainer(changed.gameObject.id).addChildAt(sprite.sprite, 0);\n const asyncId = this.increaseAsyncId(changed.gameObject.id);\n const { instance } = await resource.getResource(component.resource);\n if (!this.validateAsyncId(changed.gameObject.id, asyncId)) return;\n if (!instance) {\n console.error(`GameObject:${changed.gameObject.name}'s Img resource load error`);\n }\n this.imgs[changed.gameObject.id].image = instance;\n } else if (changed.type === OBSERVER_TYPE.CHANGE) {\n const asyncId = this.increaseAsyncId(changed.gameObject.id);\n const { instance } = await resource.getResource(component.resource);\n if (!this.validateAsyncId(changed.gameObject.id, asyncId)) return;\n if (!instance) {\n console.error(`GameObject:${changed.gameObject.name}'s Img resource load error`);\n }\n this.imgs[changed.gameObject.id].image = instance;\n } else if (changed.type === OBSERVER_TYPE.REMOVE) {\n this.increaseAsyncId(changed.gameObject.id);\n const sprite = this.imgs[changed.gameObject.id];\n if (!sprite) return;\n this.containerManager?.getContainer(changed.gameObject.id)?.removeChild(sprite.sprite);\n sprite.sprite.destroy({ children: true });\n delete this.imgs[changed.gameObject.id];\n }\n }\n }\n destroy(): void {\n for (const key in this.imgs) {\n const sprite = this.imgs[key]\n this.containerManager?.getContainer(parseInt(key))?.removeChild(sprite.sprite);\n sprite.sprite.destroy({ children: true });\n delete this.imgs[key];\n }\n if (imgSystem === this) {\n imgSystem = null;\n }\n }\n}\n"],"names":["Component","__decorate","Field","Img","Texture","getSupportedTextureFormats","loadKTX2onWorker","CompressedSource","compressedTextureUrl","imageFallbackUrl","ImgComponent","resource","RESOURCE_TYPE","Renderer","RendererSystem","Transform2D","OBSERVER_TYPE","Sprite","decorators"],"mappings":";;;;;;;;;YAOc,MAAO,GAAI,SAAQA,gBAAoB,CAAA;AAArD,IAAA,WAAA,GAAA;;QAUE,IAAA,CAAA,QAAQ,GAAW,EAAE;IAOvB;aAhBS,IAAA,CAAA,aAAa,GAAW,KAAX,CAAiB;AAWrC,IAAA,IAAI,CAAC,GAAe,EAAA;AAClB,QAAA,IAAI,GAAG,IAAI,GAAG,CAAC,QAAQ,EAAE;AACvB,YAAA,IAAI,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ;QAC9B;IACF;;AANAC,gBAAA,CAAA;AAPC,IAAAC,wBAAK,CAAC;AACL,QAAA,IAAI,EAAE,QAAQ;AACd,QAAA,KAAK,EAAE,KAAK;AACZ,QAAA,KAAK,EAAE,UAAU;AACjB,QAAA,WAAW,EAAE,4CAA4C;AACzD,QAAA,MAAM,EAAE,MAAM;KACf;AACqB,CAAA,EAAAC,KAAA,CAAA,SAAA,EAAA,UAAA,EAAA,MAAA,CAAA;;ACZxB;AACA,MAAM,kBAAkB,GAAG;IACzB,gBAAgB;IAChB,gBAAgB;IAChB,gBAAgB;IAChB,iBAAiB;CAC4B;AAE/C,SAAS,aAAa,CAAC,IAAa,EAAA;IAClC,IAAI,IAAI,YAAY,WAAW;AAAE,QAAA,OAAO,IAAI;AAC5C,IAAA,IAAI,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE;QAC5B,MAAM,IAAI,GAAG,IAAuB;AACpC,QAAA,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAgB;IAC7F;AACA,IAAA,OAAO,IAAI;AACb;AAEA,SAAS,aAAa,CAAC,GAAW,EAAA;IAChC,OAAO,IAAI,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ACtB,SAAS,GAAG,IAAI;QAClB;IACF;;AA7DmB,GAAG,GAAAf,gBAAA,CAAA;IAHvBiB,iBAAU,CAAC,iBAAiB,CAAC;AAC5B,QAAA,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;KAC3C;AACoB,CAAA,EAAA,GAAG,CA8DvB;kBA9DoB,GAAG;;;;;"}
|
|
1
|
+
{"version":3,"file":"plugin-renderer-img.cjs.js","sources":["../lib/component.ts","../lib/system.ts"],"sourcesContent":["import { Field } from '@combos-fun/inspector-decorator';\nimport { Component } from '@combos-fun/engine';\n\nexport interface ImgParams {\n resource: string;\n}\n\nexport default class Img extends Component<ImgParams> {\n static componentName: string = 'Img';\n\n @Field({\n type: 'string',\n group: 'Img',\n label: 'resource',\n description: 'Image resource id from the asset registry.',\n editor: 'text',\n })\n resource: string = '';\n\n init(obj?: ImgParams) {\n if (obj && obj.resource) {\n this.resource = obj.resource;\n }\n }\n}\n","import { GameObject, decorators, resource, ComponentChanged, RESOURCE_TYPE, OBSERVER_TYPE } from '@combos-fun/engine';\nimport { RendererManager, ContainerManager, RendererSystem, Renderer, Transform2D, createImageTexture, releaseImageTexture } from '@combos-fun/plugin-renderer';\nimport { Sprite } from '@combos-fun/renderer-adapter';\nimport { Texture } from 'pixi.js';\nimport ImgComponent from './component';\n\nlet imgSystem: Img | null = null;\n\nfunction holdsTexture(sprite: Sprite, texture: Texture) {\n return sprite.image === texture || sprite.sprite.texture === texture;\n}\n\nfunction detachTexture(texture: Texture) {\n if (!imgSystem || texture === Texture.EMPTY) return;\n for (const key in imgSystem.imgs) {\n const sprite = imgSystem.imgs[key];\n if (!sprite?.sprite || sprite.sprite.destroyed) continue;\n if (!holdsTexture(sprite, texture)) continue;\n // Assign directly. The adapter setter's HTMLImageElement branch calls destroy(false).\n sprite.sprite.texture = Texture.EMPTY;\n sprite._image = Texture.EMPTY;\n }\n}\n\nfunction attachTexture(name: string, texture: Texture) {\n if (!imgSystem || !name || texture === Texture.EMPTY) return;\n const gameObjects = imgSystem.game?.gameObjects;\n if (!gameObjects) return;\n for (const key in imgSystem.imgs) {\n const sprite = imgSystem.imgs[key];\n if (!sprite?.sprite || sprite.sprite.destroyed) continue;\n const gameObject = gameObjects.find((item) => item.id === parseInt(key));\n if (gameObject?.getComponent(ImgComponent)?.resource !== name) continue;\n sprite.sprite.texture = texture;\n sprite._image = texture;\n }\n}\n\nresource.registerInstance(RESOURCE_TYPE.IMAGE, async (res) => {\n const texture = await createImageTexture(res);\n if (texture) attachTexture(res.name, texture);\n return texture;\n});\nresource.registerDestroy(RESOURCE_TYPE.IMAGE, ({ instance }) => {\n if (!instance || instance === Texture.EMPTY) return;\n const texture = instance as Texture;\n detachTexture(texture);\n releaseImageTexture(texture);\n});\n\n@decorators.componentObserver({\n Img: [{ prop: ['resource'], deep: false }],\n})\nexport default class Img extends Renderer {\n static systemName = 'Img';\n name: string = 'Img';\n imgs: { [propName: number]: Sprite } = {};\n renderSystem: RendererSystem;\n rendererManager: RendererManager;\n containerManager: ContainerManager;\n init() {\n imgSystem = this;\n this.renderSystem = this.game.getSystem(RendererSystem) as RendererSystem;\n this.renderSystem.rendererManager.register(this);\n }\n rendererUpdate(gameObject: GameObject) {\n const { width, height } = gameObject.getComponent(Transform2D)?.size ?? { width: 0, height: 0 };\n if (this.imgs[gameObject.id]) {\n this.imgs[gameObject.id].sprite.width = width;\n this.imgs[gameObject.id].sprite.height = height;\n }\n }\n async componentChanged(changed: ComponentChanged) {\n if (changed.componentName === 'Img') {\n const component: ImgComponent = changed.component as ImgComponent;\n if (changed.type === OBSERVER_TYPE.ADD) {\n const sprite = new Sprite(null);\n this.imgs[changed.gameObject.id] = sprite;\n this.containerManager.getContainer(changed.gameObject.id).addChildAt(sprite.sprite, 0);\n const asyncId = this.increaseAsyncId(changed.gameObject.id);\n const { instance } = await resource.getResource(component.resource);\n if (!this.validateAsyncId(changed.gameObject.id, asyncId)) return;\n if (!instance) {\n console.error(`GameObject:${changed.gameObject.name}'s Img resource load error`);\n }\n this.imgs[changed.gameObject.id].image = instance;\n } else if (changed.type === OBSERVER_TYPE.CHANGE) {\n const asyncId = this.increaseAsyncId(changed.gameObject.id);\n const { instance } = await resource.getResource(component.resource);\n if (!this.validateAsyncId(changed.gameObject.id, asyncId)) return;\n if (!instance) {\n console.error(`GameObject:${changed.gameObject.name}'s Img resource load error`);\n }\n this.imgs[changed.gameObject.id].image = instance;\n } else if (changed.type === OBSERVER_TYPE.REMOVE) {\n this.increaseAsyncId(changed.gameObject.id);\n const sprite = this.imgs[changed.gameObject.id];\n if (!sprite) return;\n this.containerManager?.getContainer(changed.gameObject.id)?.removeChild(sprite.sprite);\n sprite.sprite.destroy({ children: true });\n delete this.imgs[changed.gameObject.id];\n }\n }\n }\n destroy(): void {\n for (const key in this.imgs) {\n const sprite = this.imgs[key]\n this.containerManager?.getContainer(parseInt(key))?.removeChild(sprite.sprite);\n sprite.sprite.destroy({ children: true });\n delete this.imgs[key];\n }\n if (imgSystem === this) {\n imgSystem = null;\n }\n }\n}\n"],"names":["Component","__decorate","Field","Img","Texture","ImgComponent","resource","RESOURCE_TYPE","createImageTexture","releaseImageTexture","Renderer","RendererSystem","Transform2D","OBSERVER_TYPE","Sprite","decorators"],"mappings":";;;;;;;;;YAOc,MAAO,GAAI,SAAQA,gBAAoB,CAAA;AAArD,IAAA,WAAA,GAAA;;QAUE,IAAA,CAAA,QAAQ,GAAW,EAAE;IAOvB;aAhBS,IAAA,CAAA,aAAa,GAAW,KAAX,CAAiB;AAWrC,IAAA,IAAI,CAAC,GAAe,EAAA;AAClB,QAAA,IAAI,GAAG,IAAI,GAAG,CAAC,QAAQ,EAAE;AACvB,YAAA,IAAI,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ;QAC9B;IACF;;AANAC,gBAAA,CAAA;AAPC,IAAAC,wBAAK,CAAC;AACL,QAAA,IAAI,EAAE,QAAQ;AACd,QAAA,KAA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I,CAAC,MAAM;oBAAE;AACb,gBAAA,IAAI,CAAC,gBAAgB,EAAE,YAAY,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC;gBACtF,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;gBACzC,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;YACzC;QACF;IACF;IACA,OAAO,GAAA;AACL,QAAA,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,IAAI,EAAE;YAC3B,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;AAC7B,YAAA,IAAI,CAAC,gBAAgB,EAAE,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC;YAC9E,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;AACzC,YAAA,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;QACvB;AACA,QAAA,IAAI,SAAS,KAAK,IAAI,EAAE;YACtB,SAAS,GAAG,IAAI;QAClB;IACF;;AA7DmB,GAAG,GAAAZ,gBAAA,CAAA;IAHvBc,iBAAU,CAAC,iBAAiB,CAAC;AAC5B,QAAA,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;KAC3C;AACoB,CAAA,EAAA,GAAG,CA8DvB;kBA9DoB,GAAG;;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("tslib"),t=require("@combos-fun/inspector-decorator"),r=require("@combos-fun/engine"),s=require("@combos-fun/plugin-renderer"),i=require("@combos-fun/renderer-adapter"),n=require("pixi.js");let o=class extends r.Component{constructor(){super(...arguments),this.resource=""}static{this.componentName="Img"}init(e){e&&e.resource&&(this.resource=e.resource)}};e.__decorate([t.Field({type:"string",group:"Img",label:"resource",description:"Image resource id from the asset registry.",editor:"text"})],o.prototype,"resource",void 0);
|
|
1
|
+
"use strict";var e=require("tslib"),t=require("@combos-fun/inspector-decorator"),r=require("@combos-fun/engine"),s=require("@combos-fun/plugin-renderer"),i=require("@combos-fun/renderer-adapter"),n=require("pixi.js");let o=class extends r.Component{constructor(){super(...arguments),this.resource=""}static{this.componentName="Img"}init(e){e&&e.resource&&(this.resource=e.resource)}};e.__decorate([t.Field({type:"string",group:"Img",label:"resource",description:"Image resource id from the asset registry.",editor:"text"})],o.prototype,"resource",void 0);let c=null;function a(e,t){return e.image===t||e.sprite.texture===t}r.resource.registerInstance(r.RESOURCE_TYPE.IMAGE,async e=>{const t=await s.createImageTexture(e);return t&&function(e,t){if(!c||!e||t===n.Texture.EMPTY)return;const r=c.game?.gameObjects;if(r)for(const s in c.imgs){const i=c.imgs[s];if(!i?.sprite||i.sprite.destroyed)continue;const n=r.find(e=>e.id===parseInt(s));n?.getComponent(o)?.resource===e&&(i.sprite.texture=t,i._image=t)}}(e.name,t),t}),r.resource.registerDestroy(r.RESOURCE_TYPE.IMAGE,({instance:e})=>{if(!e||e===n.Texture.EMPTY)return;const t=e;!function(e){if(c&&e!==n.Texture.EMPTY)for(const t in c.imgs){const r=c.imgs[t];r?.sprite&&!r.sprite.destroyed&&a(r,e)&&(r.sprite.texture=n.Texture.EMPTY,r._image=n.Texture.EMPTY)}}(t),s.releaseImageTexture(t)});let m=class extends s.Renderer{constructor(){super(...arguments),this.name="Img",this.imgs={}}static{this.systemName="Img"}init(){c=this,this.renderSystem=this.game.getSystem(s.RendererSystem),this.renderSystem.rendererManager.register(this)}rendererUpdate(e){const{width:t,height:r}=e.getComponent(s.Transform2D)?.size??{width:0,height:0};this.imgs[e.id]&&(this.imgs[e.id].sprite.width=t,this.imgs[e.id].sprite.height=r)}async componentChanged(e){if("Img"===e.componentName){const t=e.component;if(e.type===r.OBSERVER_TYPE.ADD){const s=new i.Sprite(null);this.imgs[e.gameObject.id]=s,this.containerManager.getContainer(e.gameObject.id).addChildAt(s.sprite,0);const n=this.increaseAsyncId(e.gameObject.id),{instance:o}=await r.resource.getResource(t.resource);if(!this.validateAsyncId(e.gameObject.id,n))return;o||console.error(`GameObject:${e.gameObject.name}'s Img resource load error`),this.imgs[e.gameObject.id].image=o}else if(e.type===r.OBSERVER_TYPE.CHANGE){const s=this.increaseAsyncId(e.gameObject.id),{instance:i}=await r.resource.getResource(t.resource);if(!this.validateAsyncId(e.gameObject.id,s))return;i||console.error(`GameObject:${e.gameObject.name}'s Img resource load error`),this.imgs[e.gameObject.id].image=i}else if(e.type===r.OBSERVER_TYPE.REMOVE){this.increaseAsyncId(e.gameObject.id);const t=this.imgs[e.gameObject.id];if(!t)return;this.containerManager?.getContainer(e.gameObject.id)?.removeChild(t.sprite),t.sprite.destroy({children:!0}),delete this.imgs[e.gameObject.id]}}}destroy(){for(const e in this.imgs){const t=this.imgs[e];this.containerManager?.getContainer(parseInt(e))?.removeChild(t.sprite),t.sprite.destroy({children:!0}),delete this.imgs[e]}c===this&&(c=null)}};m=e.__decorate([r.decorators.componentObserver({Img:[{prop:["resource"],deep:!1}]})],m);var g=m;exports.Img=o,exports.ImgSystem=g;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { __decorate } from 'tslib';
|
|
2
2
|
import { Field } from '@combos-fun/inspector-decorator';
|
|
3
|
-
import { Component,
|
|
4
|
-
import { Renderer, RendererSystem, Transform2D } from '@combos-fun/plugin-renderer';
|
|
3
|
+
import { Component, resource, RESOURCE_TYPE, OBSERVER_TYPE, decorators } from '@combos-fun/engine';
|
|
4
|
+
import { createImageTexture, releaseImageTexture, Renderer, RendererSystem, Transform2D } from '@combos-fun/plugin-renderer';
|
|
5
5
|
import { Sprite } from '@combos-fun/renderer-adapter';
|
|
6
|
-
import { Texture
|
|
6
|
+
import { Texture } from 'pixi.js';
|
|
7
7
|
|
|
8
8
|
let Img$1 = class Img extends Component {
|
|
9
9
|
constructor() {
|
|
@@ -27,116 +27,6 @@ __decorate([
|
|
|
27
27
|
})
|
|
28
28
|
], Img$1.prototype, "resource", void 0);
|
|
29
29
|
|
|
30
|
-
/** Compressed targets only. RGBA is omitted so Basis is never expanded to uncompressed pixels. */
|
|
31
|
-
const COMPRESSED_TARGETS = [
|
|
32
|
-
'astc-4x4-unorm',
|
|
33
|
-
'bc7-rgba-unorm',
|
|
34
|
-
'bc3-rgba-unorm',
|
|
35
|
-
'etc2-rgba8unorm',
|
|
36
|
-
];
|
|
37
|
-
function asArrayBuffer(data) {
|
|
38
|
-
if (data instanceof ArrayBuffer)
|
|
39
|
-
return data;
|
|
40
|
-
if (ArrayBuffer.isView(data)) {
|
|
41
|
-
const view = data;
|
|
42
|
-
return view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
|
|
43
|
-
}
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
function loadHtmlImage(url) {
|
|
47
|
-
return new Promise((resolve, reject) => {
|
|
48
|
-
const image = new Image();
|
|
49
|
-
image.crossOrigin = 'anonymous';
|
|
50
|
-
image.onload = () => resolve(image);
|
|
51
|
-
image.onerror = () => reject(new Error(`Failed to load image ${url}`));
|
|
52
|
-
image.src = url;
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
async function textureFromElement(image) {
|
|
56
|
-
if (typeof createImageBitmap !== 'function')
|
|
57
|
-
return Texture.from(image);
|
|
58
|
-
let bitmap = null;
|
|
59
|
-
try {
|
|
60
|
-
bitmap = await createImageBitmap(image);
|
|
61
|
-
return Texture.from(bitmap);
|
|
62
|
-
}
|
|
63
|
-
catch {
|
|
64
|
-
try {
|
|
65
|
-
bitmap?.close();
|
|
66
|
-
}
|
|
67
|
-
catch {
|
|
68
|
-
// ImageBitmap.close throws once the bitmap is already detached.
|
|
69
|
-
}
|
|
70
|
-
return Texture.from(image);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
async function loadPixiKtx2(url) {
|
|
74
|
-
try {
|
|
75
|
-
const supported = await getSupportedTextureFormats();
|
|
76
|
-
const compressed = COMPRESSED_TARGETS.filter((format) => supported.includes(format));
|
|
77
|
-
if (!compressed.length)
|
|
78
|
-
return null;
|
|
79
|
-
const options = await loadKTX2onWorker(url, compressed);
|
|
80
|
-
if (!options?.resource || !COMPRESSED_TARGETS.includes(options.format)) {
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
return new Texture({ source: new CompressedSource(options) });
|
|
84
|
-
}
|
|
85
|
-
catch {
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
async function loadCompressed(res, imageData) {
|
|
90
|
-
const slot = res.src?.image;
|
|
91
|
-
const url = compressedTextureUrl(slot);
|
|
92
|
-
if (!url)
|
|
93
|
-
return null;
|
|
94
|
-
const buffer = asArrayBuffer(imageData);
|
|
95
|
-
if (!buffer)
|
|
96
|
-
return loadPixiKtx2(url);
|
|
97
|
-
const blobUrl = URL.createObjectURL(new Blob([buffer]));
|
|
98
|
-
try {
|
|
99
|
-
const texture = await loadPixiKtx2(blobUrl);
|
|
100
|
-
if (texture)
|
|
101
|
-
return texture;
|
|
102
|
-
}
|
|
103
|
-
finally {
|
|
104
|
-
URL.revokeObjectURL(blobUrl);
|
|
105
|
-
}
|
|
106
|
-
return loadPixiKtx2(url);
|
|
107
|
-
}
|
|
108
|
-
/** PNG/JPG/WebP stay on Texture.from. A KTX2 companion is used only when the GPU can take it. */
|
|
109
|
-
async function createImageTexture(res) {
|
|
110
|
-
const imageData = res.data?.image;
|
|
111
|
-
const compressed = await loadCompressed(res, imageData);
|
|
112
|
-
if (compressed)
|
|
113
|
-
return compressed;
|
|
114
|
-
if (typeof HTMLImageElement !== 'undefined' && imageData instanceof HTMLImageElement) {
|
|
115
|
-
return textureFromElement(imageData);
|
|
116
|
-
}
|
|
117
|
-
const fallback = imageFallbackUrl(res.src?.image);
|
|
118
|
-
if (!fallback)
|
|
119
|
-
return undefined;
|
|
120
|
-
try {
|
|
121
|
-
return textureFromElement(await loadHtmlImage(fallback));
|
|
122
|
-
}
|
|
123
|
-
catch {
|
|
124
|
-
return undefined;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
function releaseImageTexture(texture) {
|
|
128
|
-
const source = texture.source?.resource;
|
|
129
|
-
texture.destroy(true);
|
|
130
|
-
if (typeof ImageBitmap !== 'undefined' && source instanceof ImageBitmap) {
|
|
131
|
-
try {
|
|
132
|
-
source.close();
|
|
133
|
-
}
|
|
134
|
-
catch {
|
|
135
|
-
// Already closed by the texture source.
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
30
|
let imgSystem = null;
|
|
141
31
|
function holdsTexture(sprite, texture) {
|
|
142
32
|
return sprite.image === texture || sprite.sprite.texture === texture;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin-renderer-img.esm.js","sources":["../lib/component.ts","../lib/imageTexture.ts","../lib/system.ts"],"sourcesContent":["import { Field } from '@combos-fun/inspector-decorator';\nimport { Component } from '@combos-fun/engine';\n\nexport interface ImgParams {\n resource: string;\n}\n\nexport default class Img extends Component<ImgParams> {\n static componentName: string = 'Img';\n\n @Field({\n type: 'string',\n group: 'Img',\n label: 'resource',\n description: 'Image resource id from the asset registry.',\n editor: 'text',\n })\n resource: string = '';\n\n init(obj?: ImgParams) {\n if (obj && obj.resource) {\n this.resource = obj.resource;\n }\n }\n}\n","import { compressedTextureUrl, imageFallbackUrl } from '@combos-fun/engine';\nimport type { ResourceStruct } from '@combos-fun/engine';\nimport { CompressedSource, Texture, getSupportedTextureFormats, loadKTX2onWorker } from 'pixi.js';\nimport type { TEXTURE_FORMATS } from 'pixi.js';\n\n/** Compressed targets only. RGBA is omitted so Basis is never expanded to uncompressed pixels. */\nconst COMPRESSED_TARGETS = [\n 'astc-4x4-unorm',\n 'bc7-rgba-unorm',\n 'bc3-rgba-unorm',\n 'etc2-rgba8unorm',\n] as const satisfies readonly TEXTURE_FORMATS[];\n\nfunction asArrayBuffer(data: unknown): ArrayBuffer | null {\n if (data instanceof ArrayBuffer) return data;\n if (ArrayBuffer.isView(data)) {\n const view = data as ArrayBufferView;\n return view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength) as ArrayBuffer;\n }\n return null;\n}\n\nfunction loadHtmlImage(url: string): Promise<HTMLImageElement> {\n return new Promise((resolve, reject) => {\n const image = new Image();\n image.crossOrigin = 'anonymous';\n image.onload = () => resolve(image);\n image.onerror = () => reject(new Error(`Failed to load image ${url}`));\n image.src = url;\n });\n}\n\nasync function textureFromElement(image: HTMLImageElement): Promise<Texture> {\n if (typeof createImageBitmap !== 'function') return Texture.from(image);\n let bitmap: ImageBitmap | null = null;\n try {\n bitmap = await createImageBitmap(image);\n return Texture.from(bitmap);\n } catch {\n try {\n bitmap?.close();\n } catch {\n // ImageBitmap.close throws once the bitmap is already detached.\n }\n return Texture.from(image);\n }\n}\n\nasync function loadPixiKtx2(url: string): Promise<Texture | null> {\n try {\n const supported = await getSupportedTextureFormats();\n const compressed = COMPRESSED_TARGETS.filter((format) => supported.includes(format));\n if (!compressed.length) return null;\n const options = await loadKTX2onWorker(url, compressed);\n if (!options?.resource || !COMPRESSED_TARGETS.includes(options.format as typeof COMPRESSED_TARGETS[number])) {\n return null;\n }\n return new Texture({ source: new CompressedSource(options) });\n } catch {\n return null;\n }\n}\n\nasync function loadCompressed(res: ResourceStruct, imageData: unknown): Promise<Texture | null> {\n const slot = res.src?.image;\n const url = compressedTextureUrl(slot);\n if (!url) return null;\n const buffer = asArrayBuffer(imageData);\n if (!buffer) return loadPixiKtx2(url);\n const blobUrl = URL.createObjectURL(new Blob([buffer]));\n try {\n const texture = await loadPixiKtx2(blobUrl);\n if (texture) return texture;\n } finally {\n URL.revokeObjectURL(blobUrl);\n }\n return loadPixiKtx2(url);\n}\n\n/** PNG/JPG/WebP stay on Texture.from. A KTX2 companion is used only when the GPU can take it. */\nexport async function createImageTexture(res: ResourceStruct): Promise<Texture | undefined> {\n const imageData = res.data?.image;\n const compressed = await loadCompressed(res, imageData);\n if (compressed) return compressed;\n if (typeof HTMLImageElement !== 'undefined' && imageData instanceof HTMLImageElement) {\n return textureFromElement(imageData);\n }\n const fallback = imageFallbackUrl(res.src?.image);\n if (!fallback) return undefined;\n try {\n return textureFromElement(await loadHtmlImage(fallback));\n } catch {\n return undefined;\n }\n}\n\nexport function releaseImageTexture(texture: Texture) {\n const source = texture.source?.resource;\n texture.destroy(true);\n if (typeof ImageBitmap !== 'undefined' && source instanceof ImageBitmap) {\n try {\n source.close();\n } catch {\n // Already closed by the texture source.\n }\n }\n}\n","import { GameObject, decorators, resource, ComponentChanged, RESOURCE_TYPE, OBSERVER_TYPE } from '@combos-fun/engine';\nimport { RendererManager, ContainerManager, RendererSystem, Renderer, Transform2D } from '@combos-fun/plugin-renderer';\nimport { Sprite } from '@combos-fun/renderer-adapter';\nimport { Texture } from 'pixi.js';\nimport ImgComponent from './component';\nimport { createImageTexture, releaseImageTexture } from './imageTexture';\n\nlet imgSystem: Img | null = null;\n\nfunction holdsTexture(sprite: Sprite, texture: Texture) {\n return sprite.image === texture || sprite.sprite.texture === texture;\n}\n\nfunction detachTexture(texture: Texture) {\n if (!imgSystem || texture === Texture.EMPTY) return;\n for (const key in imgSystem.imgs) {\n const sprite = imgSystem.imgs[key];\n if (!sprite?.sprite || sprite.sprite.destroyed) continue;\n if (!holdsTexture(sprite, texture)) continue;\n // Assign directly. The adapter setter's HTMLImageElement branch calls destroy(false).\n sprite.sprite.texture = Texture.EMPTY;\n sprite._image = Texture.EMPTY;\n }\n}\n\nfunction attachTexture(name: string, texture: Texture) {\n if (!imgSystem || !name || texture === Texture.EMPTY) return;\n const gameObjects = imgSystem.game?.gameObjects;\n if (!gameObjects) return;\n for (const key in imgSystem.imgs) {\n const sprite = imgSystem.imgs[key];\n if (!sprite?.sprite || sprite.sprite.destroyed) continue;\n const gameObject = gameObjects.find((item) => item.id === parseInt(key));\n if (gameObject?.getComponent(ImgComponent)?.resource !== name) continue;\n sprite.sprite.texture = texture;\n sprite._image = texture;\n }\n}\n\nresource.registerInstance(RESOURCE_TYPE.IMAGE, async (res) => {\n const texture = await createImageTexture(res);\n if (texture) attachTexture(res.name, texture);\n return texture;\n});\nresource.registerDestroy(RESOURCE_TYPE.IMAGE, ({ instance }) => {\n if (!instance || instance === Texture.EMPTY) return;\n const texture = instance as Texture;\n detachTexture(texture);\n releaseImageTexture(texture);\n});\n\n@decorators.componentObserver({\n Img: [{ prop: ['resource'], deep: false }],\n})\nexport default class Img extends Renderer {\n static systemName = 'Img';\n name: string = 'Img';\n imgs: { [propName: number]: Sprite } = {};\n renderSystem: RendererSystem;\n rendererManager: RendererManager;\n containerManager: ContainerManager;\n init() {\n imgSystem = this;\n this.renderSystem = this.game.getSystem(RendererSystem) as RendererSystem;\n this.renderSystem.rendererManager.register(this);\n }\n rendererUpdate(gameObject: GameObject) {\n const { width, height } = gameObject.getComponent(Transform2D)?.size ?? { width: 0, height: 0 };\n if (this.imgs[gameObject.id]) {\n this.imgs[gameObject.id].sprite.width = width;\n this.imgs[gameObject.id].sprite.height = height;\n }\n }\n async componentChanged(changed: ComponentChanged) {\n if (changed.componentName === 'Img') {\n const component: ImgComponent = changed.component as ImgComponent;\n if (changed.type === OBSERVER_TYPE.ADD) {\n const sprite = new Sprite(null);\n this.imgs[changed.gameObject.id] = sprite;\n this.containerManager.getContainer(changed.gameObject.id).addChildAt(sprite.sprite, 0);\n const asyncId = this.increaseAsyncId(changed.gameObject.id);\n const { instance } = await resource.getResource(component.resource);\n if (!this.validateAsyncId(changed.gameObject.id, asyncId)) return;\n if (!instance) {\n console.error(`GameObject:${changed.gameObject.name}'s Img resource load error`);\n }\n this.imgs[changed.gameObject.id].image = instance;\n } else if (changed.type === OBSERVER_TYPE.CHANGE) {\n const asyncId = this.increaseAsyncId(changed.gameObject.id);\n const { instance } = await resource.getResource(component.resource);\n if (!this.validateAsyncId(changed.gameObject.id, asyncId)) return;\n if (!instance) {\n console.error(`GameObject:${changed.gameObject.name}'s Img resource load error`);\n }\n this.imgs[changed.gameObject.id].image = instance;\n } else if (changed.type === OBSERVER_TYPE.REMOVE) {\n this.increaseAsyncId(changed.gameObject.id);\n const sprite = this.imgs[changed.gameObject.id];\n if (!sprite) return;\n this.containerManager?.getContainer(changed.gameObject.id)?.removeChild(sprite.sprite);\n sprite.sprite.destroy({ children: true });\n delete this.imgs[changed.gameObject.id];\n }\n }\n }\n destroy(): void {\n for (const key in this.imgs) {\n const sprite = this.imgs[key]\n this.containerManager?.getContainer(parseInt(key))?.removeChild(sprite.sprite);\n sprite.sprite.destroy({ children: true });\n delete this.imgs[key];\n }\n if (imgSystem === this) {\n imgSystem = null;\n }\n }\n}\n"],"names":["Img","ImgComponent"],"mappings":";;;;;;;YAOc,MAAO,GAAI,SAAQ,SAAoB,CAAA;AAArD,IAAA,WAAA,GAAA;;QAUE,IAAA,CAAA,QAAQ,GAAW,EAAE;IAOvB;aAhBS,IAAA,CAAA,aAAa,GAAW,KAAX,CAAiB;AAWrC,IAAA,IAAI,CAAC,GAAe,EAAA;AAClB,QAAA,IAAI,GAAG,IAAI,GAAG,CAAC,QAAQ,EAAE;AACvB,YAAA,IAAI,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ;QAC9B;IACF;;AANA,UAAA,CAAA;AAPC,IAAA,KAAK,CAAC;AACL,QAAA,IAAI,EAAE,QAAQ;AACd,QAAA,KAAK,EAAE,KAAK;AACZ,QAAA,KAAK,EAAE,UAAU;AACjB,QAAA,WAAW,EAAE,4CAA4C;AACzD,QAAA,MAAM,EAAE,MAAM;KACf;AACqB,CAAA,EAAAA,KAAA,CAAA,SAAA,EAAA,UAAA,EAAA,MAAA,CAAA;;ACZxB;AACA,MAAM,kBAAkB,GAAG;IACzB,gBAAgB;IAChB,gBAAgB;IAChB,gBAAgB;IAChB,iBAAiB;CAC4B;AA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,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;AAC7B,YAAA,IAAI,CAAC,gBAAgB,EAAE,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC;YAC9E,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;AACzC,YAAA,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;QACvB;AACA,QAAA,IAAI,SAAS,KAAK,IAAI,EAAE;YACtB,SAAS,GAAG,IAAI;QAClB;IACF;;AA7DmB,GAAG,GAAA,UAAA,CAAA;IAHvB,UAAU,CAAC,iBAAiB,CAAC;AAC5B,QAAA,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;KAC3C;AACoB,CAAA,EAAA,GAAG,CA8DvB;kBA9DoB,GAAG;;;;"}
|
|
1
|
+
{"version":3,"file":"plugin-renderer-img.esm.js","sources":["../lib/component.ts","../lib/system.ts"],"sourcesContent":["import { Field } from '@combos-fun/inspector-decorator';\nimport { Component } from '@combos-fun/engine';\n\nexport interface ImgParams {\n resource: string;\n}\n\nexport default class Img extends Component<ImgParams> {\n static componentName: string = 'Img';\n\n @Field({\n type: 'string',\n group: 'Img',\n label: 'resource',\n description: 'Image resource id from the asset registry.',\n editor: 'text',\n })\n resource: string = '';\n\n init(obj?: ImgParams) {\n if (obj && obj.resource) {\n this.resource = obj.resource;\n }\n }\n}\n","import { GameObject, decorators, resource, ComponentChanged, RESOURCE_TYPE, OBSERVER_TYPE } from '@combos-fun/engine';\nimport { RendererManager, ContainerManager, RendererSystem, Renderer, Transform2D, createImageTexture, releaseImageTexture } from '@combos-fun/plugin-renderer';\nimport { Sprite } from '@combos-fun/renderer-adapter';\nimport { Texture } from 'pixi.js';\nimport ImgComponent from './component';\n\nlet imgSystem: Img | null = null;\n\nfunction holdsTexture(sprite: Sprite, texture: Texture) {\n return sprite.image === texture || sprite.sprite.texture === texture;\n}\n\nfunction detachTexture(texture: Texture) {\n if (!imgSystem || texture === Texture.EMPTY) return;\n for (const key in imgSystem.imgs) {\n const sprite = imgSystem.imgs[key];\n if (!sprite?.sprite || sprite.sprite.destroyed) continue;\n if (!holdsTexture(sprite, texture)) continue;\n // Assign directly. The adapter setter's HTMLImageElement branch calls destroy(false).\n sprite.sprite.texture = Texture.EMPTY;\n sprite._image = Texture.EMPTY;\n }\n}\n\nfunction attachTexture(name: string, texture: Texture) {\n if (!imgSystem || !name || texture === Texture.EMPTY) return;\n const gameObjects = imgSystem.game?.gameObjects;\n if (!gameObjects) return;\n for (const key in imgSystem.imgs) {\n const sprite = imgSystem.imgs[key];\n if (!sprite?.sprite || sprite.sprite.destroyed) continue;\n const gameObject = gameObjects.find((item) => item.id === parseInt(key));\n if (gameObject?.getComponent(ImgComponent)?.resource !== name) continue;\n sprite.sprite.texture = texture;\n sprite._image = texture;\n }\n}\n\nresource.registerInstance(RESOURCE_TYPE.IMAGE, async (res) => {\n const texture = await createImageTexture(res);\n if (texture) attachTexture(res.name, texture);\n return texture;\n});\nresource.registerDestroy(RESOURCE_TYPE.IMAGE, ({ instance }) => {\n if (!instance || instance === Texture.EMPTY) return;\n const texture = instance as Texture;\n detachTexture(texture);\n releaseImageTexture(texture);\n});\n\n@decorators.componentObserver({\n Img: [{ prop: ['resource'], deep: false }],\n})\nexport default class Img extends Renderer {\n static systemName = 'Img';\n name: string = 'Img';\n imgs: { [propName: number]: Sprite } = {};\n renderSystem: RendererSystem;\n rendererManager: RendererManager;\n containerManager: ContainerManager;\n init() {\n imgSystem = this;\n this.renderSystem = this.game.getSystem(RendererSystem) as RendererSystem;\n this.renderSystem.rendererManager.register(this);\n }\n rendererUpdate(gameObject: GameObject) {\n const { width, height } = gameObject.getComponent(Transform2D)?.size ?? { width: 0, height: 0 };\n if (this.imgs[gameObject.id]) {\n this.imgs[gameObject.id].sprite.width = width;\n this.imgs[gameObject.id].sprite.height = height;\n }\n }\n async componentChanged(changed: ComponentChanged) {\n if (changed.componentName === 'Img') {\n const component: ImgComponent = changed.component as ImgComponent;\n if (changed.type === OBSERVER_TYPE.ADD) {\n const sprite = new Sprite(null);\n this.imgs[changed.gameObject.id] = sprite;\n this.containerManager.getContainer(changed.gameObject.id).addChildAt(sprite.sprite, 0);\n const asyncId = this.increaseAsyncId(changed.gameObject.id);\n const { instance } = await resource.getResource(component.resource);\n if (!this.validateAsyncId(changed.gameObject.id, asyncId)) return;\n if (!instance) {\n console.error(`GameObject:${changed.gameObject.name}'s Img resource load error`);\n }\n this.imgs[changed.gameObject.id].image = instance;\n } else if (changed.type === OBSERVER_TYPE.CHANGE) {\n const asyncId = this.increaseAsyncId(changed.gameObject.id);\n const { instance } = await resource.getResource(component.resource);\n if (!this.validateAsyncId(changed.gameObject.id, asyncId)) return;\n if (!instance) {\n console.error(`GameObject:${changed.gameObject.name}'s Img resource load error`);\n }\n this.imgs[changed.gameObject.id].image = instance;\n } else if (changed.type === OBSERVER_TYPE.REMOVE) {\n this.increaseAsyncId(changed.gameObject.id);\n const sprite = this.imgs[changed.gameObject.id];\n if (!sprite) return;\n this.containerManager?.getContainer(changed.gameObject.id)?.removeChild(sprite.sprite);\n sprite.sprite.destroy({ children: true });\n delete this.imgs[changed.gameObject.id];\n }\n }\n }\n destroy(): void {\n for (const key in this.imgs) {\n const sprite = this.imgs[key]\n this.containerManager?.getContainer(parseInt(key))?.removeChild(sprite.sprite);\n sprite.sprite.destroy({ children: true });\n delete this.imgs[key];\n }\n if (imgSystem === this) {\n imgSystem = null;\n }\n }\n}\n"],"names":["Img","ImgComponent"],"mappings":";;;;;;;YAOc,MAAO,GAAI,SAAQ,SAAoB,CAAA;AAArD,IAAA,WAAA,GAAA;;QAUE,IAAA,CAAA,QAAQ,GAAW,EAAE;IAOvB;aAhBS,IAAA,CAAA,aAAa,GAAW,KAAX,CAAiB;AAW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B,GAAG;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@combos-fun/plugin-renderer-img",
|
|
3
|
-
"version": "0.1.14-beta.
|
|
3
|
+
"version": "0.1.14-beta.1",
|
|
4
4
|
"description": "Static image rendering",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "dist/plugin-renderer-img.esm.js",
|
|
@@ -35,10 +35,10 @@
|
|
|
35
35
|
"author": "sun668 <q947692259@gmail.com>",
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"pixi.js": "^8.18.1",
|
|
38
|
-
"@combos-fun/plugin-renderer": "0.1.14-beta.
|
|
39
|
-
"@combos-fun/renderer-adapter": "0.1.14-beta.
|
|
40
|
-
"@combos-fun/
|
|
41
|
-
"@combos-fun/
|
|
38
|
+
"@combos-fun/plugin-renderer": "0.1.14-beta.1",
|
|
39
|
+
"@combos-fun/renderer-adapter": "0.1.14-beta.1",
|
|
40
|
+
"@combos-fun/inspector-decorator": "0.1.14-beta.1",
|
|
41
|
+
"@combos-fun/engine": "0.1.14-beta.1"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "node ../../scripts/build-package.mjs"
|