I dont get it, everything seems to be right,
examining entity and its element, font and fontAsset appears as it would from editor based entity
var font = new pc.Font(pc.app.assets.find('fontpng').resource,pc.app.assets.find('fontjson').resource)
console.log(font);
var fontAsset = new pc.Asset('myFont', 'font', null, {
textures: [pc.app.assets.find('fontpng').resource], // Array of texture asset IDs
data: pc.app.assets.find('fontjson').resource // Font JSON asset ID
});
app.assets.add(fontAsset);
app.assets.load(fontAsset);
console.log(fontAsset);
var text = new pc.Entity();
text.setPosition(0, 0.5, 0);
text.addComponent("element", {
type: pc.ELEMENTTYPE_TEXT,
anchor: new pc.Vec4(0.5,0.5,0.5,0.5),
pivot: new pc.Vec2(0.5,0.5),
alignment: new pc.Vec2(0.5,0.5),
lineHeight: 32,
autoWidth: true,
autoHeight: true,
fontAsset: pc.app.assets.find('myFont'),
fontSize: 5,
color: new pc.Color().fromString("#FFFFFF"),
});
text.element.text = 'Hello, world!';
app.root.addChild(text);
console.log(text);
Okay I’m running out of ideas…
Made function on top of FontHandler
function loadFontFromFiles(jsonUrl, pngUrl, callback) {
var fontHandler = new pc.FontHandler(app);
if (jsonUrl.endsWith('.json')) {
fetch(jsonUrl)
.then(response => response.json())
.then(data => {
fontHandler._loadTextures(pngUrl, data, function (err, textures) {
if (err) return callback(err);
var asset = {
data: data,
textures: textures,
resource: null // Will be assigned after creating the font object
};
var font = fontHandler.open(jsonUrl, asset);
asset.resource = font; // Assign the created font object
// Create the final response similar to original FontHandler
var fontResponse = {
data: asset.data,
textures: asset.textures
};
callback(null, fontResponse);
});
})
.catch(err => {
callback("Error loading font resource: " + jsonUrl + " [" + err + "]");
});
}
}
// Usage example
var fontJsonUrl = 'font.json';
var fontPngUrl = 'font.png';
loadFontFromFiles(fontJsonUrl, fontPngUrl, function (err, fontResponse) {
if (err) {
console.error(err);
return;
}
// Now you can use the 'fontResponse' object which mimics the original FontHandler response
console.log("Loaded Font:", fontResponse);
var fontAsset = new pc.Asset('myFont', 'font', null, {
textures: [fontResponse.textures[0]], // Assuming textures is an array
data: fontResponse.data
});
pc.app.assets.add(fontAsset);
pc.app.assets.load(fontAsset);
});
Same story, everything looks right, really can’t figure this out…
Hi @Newbie_Coder I’m not sure about patching an existing handler. Check this post, it’s quite old but it may be useful to get an idea on how to add your own custom resource handler: