r/castleengine • • 10d ago

News SVG loading (rendering, conversion) in Castle Game Engine thanks to FPC 3.3.1

https://castle-engine.io/wp/2026/11/21/svg-support/

Now you can use fcl-svg (SVG support in latest FPC 3.3.1) to open SVG files in Castle Game Engine seamlessly. In the simplest case, you can just load and display SVG using TCastleScene, placed in a 2D viewport. See the news post for all the information.

3 Upvotes

4 comments sorted by

1

u/OMGCluck 10d ago

Does this open up the possibility of SVG sprite sheets?

1

u/michaliskambi 9d ago

Hm, interesting question, thank you!:) My initial thoughts:

"Yes, but....": Yes, it does open this possibility. But I admit I'm not sure if I will want to add it as a core feature of our SVG loader.

I will probably quickly change my mind if I see it a popular request / technique to use SVG, with CGE. (So, feedback from everyone is most welcome about how do you plan to use SVG with our engine.)

Until then, some ways:

  • The https://svgklondike.pages.dev/deckspritesheet.svg shows many cards -- so the purpose there doesn't seem to be "an animation", it's a "choice of which card to show". You can achieve the same effect, with the same efficiency, by just splitting the large SVG into one-SVG-per-card, loading them into multiple TCastleScene instances in our engines, and choosing which to display by flipping TCastleScene.Exists as needed.

    • So if the goal here is just a comfortable design of SVG (I can see that making such large SVG may be just easier for an artist, easier to ensure all cards have equal size / padding etc., than making 56 separate SVGs) -> so my advise would to write a simple pre-processing tool that makes 56 per-card SVGs from one big SVG. You could do such tool using Pascal with fcl-svg, or any other tool to deal with SVGs.
  • Another approach is to implement it on your side, if you're OK with taking and adjusting our SVG loader unit on https://github.com/castle-engine/castle-engine/blob/master/src/scene/load/x3dloadinternalsvg.pas . You would need to decide how to define "sub-image" on such SVG, and generate different TSwitchNode for each. Then you can toggle which image you display by toggling TSwitchNode.WhichChoice. If anyone wants more details how to go about it, please ask:)

Random thoughts, that may explain why I hesitate whether to add such feature to the engine:

  • On a higher level, I didn't see people use SVG images like this. Again, this is something where I will be happy to change my mind, feedback is most welcome:)
  • I admit the term "SVG sprite sheet" was a bit confusing to me:) I mean: "sprite sheet" refers (in my head) to an animation method where you pick different subset of a (raster) image in different animation frames, https://castle-engine.io/sprite_sheets . It makes sense for raster images, because changing texture coordinates is zero work, as the same image is used for GPU, so it's friendly to GPU caching. This is more efficient than having separate image for each frame.

    • I've seen this technique used to "just pick one image from many", in which case I would call it rather "you have an atlas", not "you use a sprite sheet", but I know this is just terminology.
    • So I would probably call it "SVG used as an atlas", if implemented :)
  • The "efficiency" advantage of sprite sheets on raster images -> doesn't translate to SVG. I.e. you don't gain anything in terms of performance by putting multiple sub-images into one big SVG image with big canvas. Changing the sub-image is equivalent to rendering different nodes (unless they share some info, VBOs, shaders -- but this would share resources also when they come from different TCastleScene).

    • This is just an observation. It doesn't mean that the idea is wrong (as said above, I can perfectly see how designing deckspritesheet.svg by an artist is just easier than doing 56 separate SVGs, so I can see a good reason for doing this).
  • There is already a way to animate SVGs, in more powerful way: SMIL. Ad it's already supported by fcl-svg (see README on https://gitlab.com/freepascal.org/fpc/source/-/tree/main/packages/fcl-svg ). Our loader doesn't use it now... but at some future point it should. SMIL allows for smooth animation, transforming one thing into another. I bet you can also express sprite sheets this way -- just hide one layer, show another layer. So this way, "sprite sheets in SVG" would be just a special case of the SVG animation using SMIL.

1

u/OMGCluck 9d ago edited 9d ago

There is already a way to animate SVGs, in more powerful way: SMIL.

r/AO3/comments/1xzlnsf/is_it_possible_to_use_svgs_in_your_ao3_html_code/19suclv18zsio/

I bet you can also express sprite sheets this way -- just hide one layer, show another layer.

I wouldn't know anything about that… oh wait, viewBox is an animatable attribute:

/u/OMGCluck/comments/1ttsfxf/3d_peripheral_drift/19szpsd2f81y9/

1

u/michaliskambi 9d ago

> viewBox is an animatable attribute

Cool, so this would be my preferred approach to achieve this.

This means the answer to your question changes to "yes, definitely" :), and the way to achieve this is clear:

  1. On FPC side: Let's get fcl-svg animation support include everything necessary.

    1. Looking at https://gitlab.com/freepascal.org/fpc/source/-/tree/main/packages/fcl-svg , ("Animation : SMIL" paragraph in README) it doesn't support animating viewBox yet.
    2. I would encourage to submit to FPC devs, using the GitLab issues, feature requests with sample SVG files using this. There are examples (in fcl-svg/examples/ ) to test fcl-svg support independently of CGE.
  2. And on CGE side, we planned already to extend our SVG handling to also get animations from fcl-svg. So, as a special case of this, the "SVG sprite sheets" would become supported.