Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor PixiResourcesLoader #6560

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 117 additions & 16 deletions Extensions/JsExtensionTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,107 @@ type GDNamespace = typeof import('../GDevelop.js/types');
// in this file and merge it with the other namespace declarations.
declare namespace PIXI {}

/**
* Expose functions to load PIXI textures or fonts, given the names of
* resources and a gd.Project.
*/
declare class PixiResourcesLoader {
burstCache();

async reloadResource(project: gd.Project, resourceName: string);

/**
* Reload the given resources.
*/
async reloadResources(
project: gd.Project,
resourceNames: Array<string>
): Promise<void>;

/**
* Return the PIXI texture represented by the given resource.
* If not loaded, it will load it.
*/
getPIXITexture(project: gd.Project, resourceName: string): PIXI.Texture;

/**
* Return the three.js texture associated to the specified resource name.
* Returns a placeholder texture if not found.
* @param project The project
* @param resourceName The name of the resource
* @returns The requested texture, or a placeholder if not found.
*/
getThreeTexture(project: gd.Project, resourceName: string): THREE.Texture;

/**
* Return the three.js material associated to the specified resource name.
* @param project The project
* @param resourceName The name of the resource
* @param options Set if the material should be transparent or not.
* @returns The requested material.
*/
getThreeMaterial(
project: gd.Project,
resourceName: string,
{ useTransparentTexture }: { useTransparentTexture: boolean }
): THREE.Material;

/**
* Return the three.js material associated to the specified resource name.
* @param project The project
* @param resourceName The name of the resource
* @param options
* @returns The requested material.
*/
get3DModel(
project: gd.Project,
resourceName: string
): Promise<THREE.THREE_ADDONS.GLTF>;

/**
* Return the Pixi spine data for the specified resource name.
* @param project The project
* @param spineName The name of the spine json resource
* @returns The requested spine skeleton.
*/
async getSpineData(
project: gd.Project,
spineName: string
): Promise<SpineDataOrLoadingError>;

/**
* Return the PIXI video texture represented by the given resource.
* If not loaded, it will load it.
* @returns The PIXI.Texture to be used. It can be loading, so you
* should listen to PIXI.Texture `update` event, and refresh your object
* if this event is triggered.
*/
getPIXIVideoTexture(project: gd.Project, resourceName: string): PIXI.Texture;

/**
* Load the given font from its url/filename.
* @returns a Promise that resolves with the font-family to be used
* to render a text with the font.
*/
loadFontFamily(project: gd.Project, resourceName: string): Promise<string>;

/**
* Get the font family name for the given font resource.
* The font won't be loaded.
* @returns The font-family to be used to render a text with the font.
*/
getFontFamily(project: gd.Project, resourceName: string): string;

/**
* Get the data from a bitmap font file (fnt/xml) resource in the IDE.
*/
getBitmapFontData(project: gd.Project, resourceName: string): Promise<any>;

getInvalidPIXITexture();

getResourceJsonData(project: gd.Project, resourceName: string);
}

/**
* RenderedInstance is the base class used for creating 2D renderers of instances,
* which display on the scene editor, using Pixi.js, the instance of an object (see InstancesEditor).
Expand All @@ -14,17 +115,17 @@ class RenderedInstance {
_instance: gd.InitialInstance;
_associatedObjectConfiguration: gd.ObjectConfiguration;
_pixiContainer: PIXI.Container;
_pixiResourcesLoader: Class<PixiResourcesLoader>;
_pixiResourcesLoader: PixiResourcesLoader;
_pixiObject: PIXI.DisplayObject;
wasUsed: boolean;

constructor(
project: gdProject,
layout: gdLayout,
instance: gdInitialInstance,
associatedObjectConfiguration: gdObjectConfiguration,
project: gd.Project,
layout: gd.Layout,
instance: gd.InitialInstance,
associatedObjectConfiguration: gd.ObjectConfiguration,
pixiContainer: PIXI.Container,
pixiResourcesLoader: Class<PixiResourcesLoader>
pixiResourcesLoader: PixiResourcesLoader
);

/**
Expand Down Expand Up @@ -85,25 +186,25 @@ class RenderedInstance {
* It can also display 2D artifacts on Pixi 2D plane (3D object shadow projected on the plane for instance).
*/
class Rendered3DInstance {
_project: gdProject;
_layout: gdLayout;
_instance: gdInitialInstance;
_associatedObjectConfiguration: gdObjectConfiguration;
_project: gd.Project;
_layout: gd.Layout;
_instance: gd.InitialInstance;
_associatedObjectConfiguration: gd.ObjectConfiguration;
_pixiContainer: PIXI.Container;
_threeGroup: THREE.Group;
_pixiResourcesLoader: Class<PixiResourcesLoader>;
_pixiResourcesLoader: PixiResourcesLoader;
_pixiObject: PIXI.DisplayObject;
_threeObject: THREE.Object3D | null;
wasUsed: boolean;

constructor(
project: gdProject,
layout: gdLayout,
instance: gdInitialInstance,
associatedObjectConfiguration: gdObjectConfiguration,
project: gd.Project,
layout: gd.Layout,
instance: gd.InitialInstance,
associatedObjectConfiguration: gd.ObjectConfiguration,
pixiContainer: PIXI.Container,
threeGroup: THREE.Group,
pixiResourcesLoader: Class<PixiResourcesLoader>
pixiResourcesLoader: PixiResourcesLoader
);

/**
Expand Down
1 change: 1 addition & 0 deletions Extensions/TileMap/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,7 @@ module.exports = {
manager.getOrLoadTextureCache(
this._loadTileMapWithCallback.bind(this),
(textureName) =>
// @ts-ignore
this._pixiResourcesLoader.getPIXITexture(
this._project,
mapping[textureName] || textureName
Expand Down
2 changes: 1 addition & 1 deletion Extensions/TileMap/TileMapRuntimeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace gdjs {
* @param callback A function called when the tiles textures are split.
*/
getOrLoadTextureCache(
getTexture: (textureName: string) => PIXI.BaseTexture<PIXI.Resource>,
getTexture: (textureName: string) => PIXI.Texture,
atlasImageResourceName: string,
tileMapJsonResourceName: string,
tileSetJsonResourceName: string,
Expand Down
2 changes: 1 addition & 1 deletion Extensions/TileMap/helper/TileMapHelper.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Extensions/TileMap/helper/TileMapHelper.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Extensions/TileMap/helper/dts/render/TileMapManager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export declare class TileMapManager {
tileSetJsonResourceName: string,
callback: (tileMapFileContent: TileMapFileContent | null) => void
) => void,
getTexture: (textureName: string) => PIXI.BaseTexture<PIXI.Resource>,
getTexture: (textureName: string) => PIXI.Texture,
atlasImageResourceName: string,
tileMapJsonResourceName: string,
tileSetJsonResourceName: string,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Extensions/TileMap/helper/dts/render/TileMapPixiHelper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export declare namespace PixiTileMapHelper {
function parseAtlas(
tileMap: TileMapFileContent,
levelIndex: number,
atlasTexture: PIXI.BaseTexture<PIXI.Resource> | null,
getTexture: (textureName: string) => PIXI.BaseTexture<PIXI.Resource>
atlasTexture: PIXI.Texture | null,
getTexture: (textureName: string) => PIXI.Texture
): TileTextureCache | null;
/**
* Re-renders the tile map whenever its rendering settings have been changed
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { TileTextureCache } from '../TileTextureCache';
import { LDtkTileMap } from '../../load/ldtk/LDtkFormat';
type Texture = PIXI.BaseTexture<PIXI.Resource>;
type TextureLoader = (textureName: string) => PIXI.BaseTexture<PIXI.Resource>;
export declare namespace LDtkPixiHelper {
/**
* Split an atlas image into Pixi textures.
Expand All @@ -15,9 +13,8 @@ export declare namespace LDtkPixiHelper {
function parseAtlas(
tileMap: LDtkTileMap,
levelIndex: number,
atlasTexture: Texture | null,
getTexture: TextureLoader
atlasTexture: PIXI.Texture | null,
getTexture: (textureName: string) => PIXI.Texture
): TileTextureCache | null;
}
export {};
//# sourceMappingURL=LDtkPixiHelper.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export declare namespace TiledPixiHelper {
function parseAtlas(
tileMap: TiledTileMap,
levelIndex: number,
atlasTexture: PIXI.BaseTexture<PIXI.Resource> | null,
getTexture: (textureName: string) => PIXI.BaseTexture<PIXI.Resource>
atlasTexture: PIXI.Texture | null,
getTexture: (textureName: string) => PIXI.Texture
): TileTextureCache | null;
}
//# sourceMappingURL=TiledPixiHelper.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions Extensions/TileMap/tilemapruntimeobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,7 @@ namespace gdjs {
this._tilemapJsonFile,
textureName
);
return (game
.getImageManager()
.getPIXITexture(mappedName) as unknown) as PIXI.BaseTexture<
PIXI.Resource
>;
return game.getImageManager().getPIXITexture(mappedName);
},
this._tilemapAtlasImage,
this._tilemapJsonFile,
Expand Down
2 changes: 1 addition & 1 deletion SharedLibs/TileMapHelper/src/render/TileMapManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class TileMapManager {
tileSetJsonResourceName: string,
callback: (tileMapFileContent: TileMapFileContent | null) => void
) => void,
getTexture: (textureName: string) => PIXI.BaseTexture<PIXI.Resource>,
getTexture: (textureName: string) => PIXI.Texture,
atlasImageResourceName: string,
tileMapJsonResourceName: string,
tileSetJsonResourceName: string,
Expand Down
4 changes: 2 additions & 2 deletions SharedLibs/TileMapHelper/src/render/TileMapPixiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export namespace PixiTileMapHelper {
export function parseAtlas(
tileMap: TileMapFileContent,
levelIndex: number,
atlasTexture: PIXI.BaseTexture<PIXI.Resource> | null,
getTexture: (textureName: string) => PIXI.BaseTexture<PIXI.Resource>
atlasTexture: PIXI.Texture | null,
getTexture: (textureName: string) => PIXI.Texture
): TileTextureCache | null {
if (tileMap.kind === "ldtk") {
return LDtkPixiHelper.parseAtlas(
Expand Down
21 changes: 9 additions & 12 deletions SharedLibs/TileMapHelper/src/render/ldtk/LDtkPixiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ import { TileTextureCache } from "../TileTextureCache";
import { LDtkTileMap, LDtkTilesetDef } from "../../load/ldtk/LDtkFormat";
import { getLDtkTileId } from "../../load/ldtk/LDtkTileMapLoaderHelper";

type Texture = PIXI.BaseTexture<PIXI.Resource>;
type TextureLoader = (textureName: string) => PIXI.BaseTexture<PIXI.Resource>;

function getAtlasTexture(
atlasTextures: Record<number, Texture | null>,
atlasTextures: Record<number, PIXI.Texture | null>,
tilesetCache: Record<number, LDtkTilesetDef>,
getTexture: TextureLoader,
getTexture: (textureName: string) => PIXI.Texture,
tilesetId: number
): Texture | null {
): PIXI.Texture | null {
if (atlasTextures[tilesetId]) {
return atlasTextures[tilesetId];
}

let texture: Texture | null = null;
let texture: PIXI.Texture | null = null;

const tileset = tilesetCache[tilesetId];
if (tileset?.relPath) {
Expand Down Expand Up @@ -51,8 +48,8 @@ export namespace LDtkPixiHelper {
export function parseAtlas(
tileMap: LDtkTileMap,
levelIndex: number,
atlasTexture: Texture | null,
getTexture: TextureLoader
atlasTexture: PIXI.Texture | null,
getTexture: (textureName: string) => PIXI.Texture
): TileTextureCache | null {
const level = tileMap.levels[levelIndex > -1 ? levelIndex : 0];
if (!level || !level.layerInstances) {
Expand All @@ -68,7 +65,7 @@ export namespace LDtkPixiHelper {
// List the tiles that have been loaded to Pixi by all the layers of the level.
// The keys are a composition (getLDtkTileId) between the tileset's id and the tile's id.
const levelTileCache: Record<number, boolean> = {};
const atlasTextures: Record<number, Texture | null> = {};
const atlasTextures: Record<number, PIXI.Texture | null> = {};

for (let iLayer = level.layerInstances.length - 1; iLayer >= 0; --iLayer) {
const layer = level.layerInstances[iLayer];
Expand Down Expand Up @@ -113,7 +110,7 @@ export namespace LDtkPixiHelper {
const [x, y] = tile.src;
const rect = new PIXI.Rectangle(x, y, gridSize, gridSize);

const texture = new PIXI.Texture(atlasTexture, rect);
const texture = new PIXI.Texture(atlasTexture.baseTexture, rect);

textureCache.setTexture(tileId, texture);
} catch (error) {
Expand All @@ -131,7 +128,7 @@ export namespace LDtkPixiHelper {
if (level.bgRelPath) {
const atlasTexture = getTexture(level.bgRelPath);
const rect = new PIXI.Rectangle(0, 0, level.pxWid, level.pxHei);
const texture = new PIXI.Texture(atlasTexture!, rect);
const texture = new PIXI.Texture(atlasTexture!.baseTexture, rect);

textureCache.setLevelBackgroundTexture(level.bgRelPath, texture);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export namespace TiledPixiHelper {
export function parseAtlas(
tileMap: TiledTileMap,
levelIndex: number,
atlasTexture: PIXI.BaseTexture<PIXI.Resource> | null,
getTexture: (textureName: string) => PIXI.BaseTexture<PIXI.Resource>
atlasTexture: PIXI.Texture | null,
getTexture: (textureName: string) => PIXI.Texture
): TileTextureCache | null {
if (!tileMap.tiledversion) {
console.warn(
Expand Down Expand Up @@ -99,7 +99,7 @@ export namespace TiledPixiHelper {

try {
const rect = new PIXI.Rectangle(x, y, tilewidth, tileheight);
const texture = new PIXI.Texture(atlasTexture!, rect);
const texture = new PIXI.Texture(atlasTexture!.baseTexture, rect);

textureCache.setTexture(tileId, texture);
} catch (error) {
Expand Down
Loading
Loading