Distance Field Font Rendering

Posted on

Platformer options view with distance field fonts
Distance field fonts example

A new option at TCastleFont is available that activates distance field font rendering. Using it is trivial: just set up TCastleLabel with custom font in TCastleFont, following the manual about text and fonts. Then toggle TCastleFont.DistanceField checkbox to true.

This is an alternative technique for rendering fonts which results in font having better quality if you “upscale” it, that is: render large font size (e.g. because TCastleLabel.FontSize is large, like 100.0) but the underlying font texture is small (that is: TCastleFont.OptimalSize is smaller, like 30.0).

The font rendered using distance field fonts always remains “crisp” on the edges.

The technique, at least in our current implementation, has some drawbacks — see TCastleFont.DistanceField API documentation. Never the less, it definitely results in a more crisp text look in many practical cases.

An extreme examples of it are available in (maybe even a bit too extreme, it shows actually some issues) is the new example: examples/fonts/distance_field_fonts.

This feature has been implemented largely by the late Eugene Loza. We miss you.

Comments on the forum ➤

Catch me in October at conferences in Köln (Germany) and Mszczonów (Poland)

Posted on

If you want to listen to Michalis in person rambling about Castle Game Engine, now you can!

  1. I will be at FPC & Lazarus meeting in Köln, Germany on October 10-11, 2024. My talk plan is here.

  2. Just a few days later, I will be at Zlot Programistów Delphi (“Delphi Programmer’s Meeting” in Polish) in Mszczonów (very close to Poland’s capital city Warszawa) on October 15-16, 2024.

  3. Let me add a honorable mention: I will not be present at Web3D 2024 conference this September (starts in a few days), but I know that Don Brutzman (thank you!) will mention our engine and tools. So watch the event carefully if you can 🙂

    You are welcome to read my document Castle Game Engine and related tools for content authoring using X3D (and other 3D formats). Don has an impossible task of summarizing this document in about 10 minutes 🙂 (12 pages, and this is a summary of just one thing we do — X3D authoring, not even the actual core engine features).

See you there!

Comments on the forum ➤

Terrain improvements

Posted on

Terrain
Terrain patches seamlessly connect
New TCastleTerrain subdivisions meaning
Blender subdivisions meaning

We did a few updates to our terrains, set up by the TCastleTerrain component. You can play with them e.g. by exploring the examples/terrain or separate demo test-high-precision-terrains.

  1. The code is simpler and more efficient by internally using the TIndexedTriangleSetNode node for rendering.

    This means always using 1 draw call to render entire terrain (unlike previous TIndexedTriangleStripSetNode rendering mode when Triangulate=true where each strip took 1 draw call; we considered using an OpenGL trick to connect multiple strips into one draw call by using degenerate triangles at strip start/end, but the TIndexedTriangleSetNode seemed more straightforward solution).

    The TIndexedTriangleSetNode also maps directly to GPU (unlike TIndexedFaceSetNode that needs triangulation, unlike TElevationGridNode that needs a proxy).

    The code is also simpler: we have just one rendering path. Previously, we had 2 approaches, and deprecated Triangulate was choosing which one we use.

  2. Normals are calculated fast and are good for connected terrain patches.

    This was broken previously (in both Triangulate=true and Triangulate=false modes, although was supposed to work OK in Triangulate=true).

    Now it’s correct (and it is correct regardless of the deprecated Triangulate / new TCastleTerrain.TriangulationIgnoreHeights). Terrain patches will match (regardless of TCastleTerrain.TriangulationIgnoreHeights, terrains will maych even if their TCastleTerrain.TriangulationIgnoreHeights are different).

    Explore additional_noise_offset_test.castle-user-interface in examples/terrain to test this. The screenshot on the side shows 3 TCastleTerrain instances, but you see no weird lighting at the point where they connect, their normals match perfectly.

  3. New TCastleTerrain.TriangulationIgnoreHeights property. This determines how we split quads into triangles.

  4. Triangulate is now a deprecated alias to TCastleTerrain.TriangulationIgnoreHeights. If you used this to request consistent ordering — then new TCastleTerrain.TriangulationIgnoreHeights is just as good. If you used Triangulate for anything else — then it is probably not needed anymore, new rendering takes the best from previous 2 algorithms.

  5. TCastleTerrain.Subdivisions meaning changed.

    They now determine the number of rows in columns in the mesh. E.g. subdivision (4,4) means 16 (4 * 4) quads and 25 (5 * 5) vertexes. This matches the Blender’s “subdivisions” parameter for grids.

Note: This post details changed done last month, even before 7.0-alpha.3 release. Yeah, we have a backlog of announcements 🙂

Next steps in terrains: synchronize, review and merge Base terrain editing tools for Castle Engine Editor pull request from Andrzej Kilijański to introduce a big new feature – terrain editing! Stay tuned and please support us on Patreon!

Comments on the forum ➤

Export 3D or 2D world designed in Castle Game Engine editor to X3D or STL

Posted on

export_to_x3d_stl

This is a cool new feature that allows you to use our editor in a bit non-standard way. Use the new menu item “Data -> Export Viewport to X3D, STL” to export the contents of the TCastleViewport to X3D or STL file.

To be clear, the main purpose of our editor remains to save the design to a Castle Game Engine-specific format (*.castle-user-interface, which is a JSON serialization of engine components, that can be later read back using CastleComponentSerialize routines). But this new feature allows to use our editor for other purposes. You can design a 3D or 2D world, export it to X3D or STL, and use it with a plethora of other 3D software. For example, you can:

  • Open the X3D file in other X3D viewers, including viewers that can render X3D in a browser. Here’s a non-exhaustive list of software that can “consume” X3D files:
  • Import the resulting X3D or STL files into other 3D software, like Blender.

Try it out! Just download the engine, create a new project (or open one of the examples) and use “Data -> Export Viewport to X3D, STL” to see what happens. Try out the new project template like “3D FPS Game”. Add there more engine components (see our documentation and in particular overview of components useful in the viewport) and have fun 🙂

In case of X3D, remember that:

  1. The resulting file will refer to the resources (like models loaded in TCastleScene or textures) using relative paths. So you should keep the directory structure of the exported files intact, or adjust the paths in the X3D file manually.

  2. The exported X3D files may use some X3D extensions, specific to our engine. You can just remove them as necessary to make the models compatible with other X3D browsers.

  3. While you can load the resulting X3D back inside the engine editor, this will be just a single component TCastleScene, not all the components you exported. Solution: If you want to use CGE editor for authoring X3D or other model formats, just keep the original CGE editor files (.castle-user-interface) around, as the “source” version of your 3D / 2D world and export it to X3D as many times as you wish.

All the features supported by the export are listed below:

An additional use-case of this feature is to debug what some engine routines are doing.

  • While in some cases we create X3D nodes during the export (e.g. for viewpoints and navigation info, in which case we have somewhat similar but far from identical functionality offered by our components like TCastleCamera and TCastleWalkNavigation)…

  • …but in other cases the export just “reveals” the actual nodes used to to render given things (this is the case with e.g. lights, primitives, fog and background).

Have fun with Castle Game Engine and X3D!

Comments on the forum ➤

Important bugfixes – scaling mesh collider, STL and CW faces, water shader, macOS Retina and clipboard, Delphi integration

Posted on

Mesh collider scaled demo
macOS clipboard
Water surrounded by Cthulhu statuettes, from Lynch gamejam game

Announcing a few more important fixes from the last ~month. As usual, remember that all the bugfixes are available immediately in the latest download. And if you like what we do, we appreciate your support on Patreon!

Bugfixes worth mentioning:

  1. Fixed scaling TCastleMeshCollider.

    Scaling the TCastleScene by just setting the TCastleTransform.Scale, like MyScene.Scale := Vector3(100, 100,100) will now affect the physics as it should. The associated TCastleMeshCollider will behave properly, following the scale change. You can test this by changing scale at design-time (in editor) or run-time (from Pascal code) of course.

    I added an interactive demo of this in examples/physics/physics_test_scaled_mesh_collider.

  2. Saving of STL files from our tools (like Castle Model Viewer, Castle Model Converter) now accounts correctly for X3D shapes with CW (clock-wise) ordering of vertexes. This is important, as STL files must have CCW (counter-clock-wise) ordering of vertexes. Thanks to Jan Adamec!

  3. Water shader important fix (for some GPUs). Affects water in our terrain example, lynch gamejam project, water in demo-models.

  4. (Before 7.0-alpha.3 release): Fixed Delphi crash at exit in -dRELEASE.

  5. (Before 7.0-alpha.3 release): Fixed editor occasional crash when closing a view with camera preview open (testcase: press Ctrl+W right after selecting the camera in viewport).

  6. (Before 7.0-alpha.3 release): Fixed window resolution on Retina screens. We now behave properly on macOS Retina screens, and we render with full resolution (result is “crisp”, not artificially upscaled by macOS), and dragging window between monitors behaves OK too. Big thanks to Jan Adamec!

  7. (Before 7.0-alpha.3 release): Fixed clipboard on macOS (with Cocoa backend of TCastleWindow). Cut/copy/paste work as they should. Again thanks to Jan Adamec. They also react to Command + C / X / V, not Control + C / X / V.

  8. (This was a hotfix at 7.0-alpha.3 release, initial upload had a bug but was quickly replaced with good one): Fixed “Configure Delphi to use engine” menu item (from our Delphi integration) to handle gracefully paths with spaces.

Comments on the forum ➤

Important rendering bugfix: Light components (TCastlePointLight and TCastleSpotLight) defaults are correctly applied now, look realistic out-of-the-box (but also, look a bit different – read on for details)

Posted on

New lights attenuation in the shadows example in editor
New lights attenuation in the shadows example
light_cge
Comparison with Blender point light

Sorry for the long news title and even longer news post 🙂 This is a very important bugfix and at the same time it couldn’t (reasonably) have been done in 100% backward-compatible way. We tried to explain everything below. If in doubt how to upgrade your projects (in case things will look different), don’t hesitate to contact us by forum, Discord or any other means, we’ll help! The core of the change happened in this commit on August 1st (2024), well before 7.0-alpha.3 release so it affects everyone who upgrades to engine after that date with a project created before that date.

What was fixed? A few properties of the TCastlePointLight, TCastleSpotLight and TCastleDirectionalLight had their default values applied incorrectly. That is, the default values were correct, but their effect was wrong. In particular, TCastlePointLight.Attenuation and TCastleSpotLight.Attenuation were not applied correctly.

The most visible consequence of the bug (now fixed): The default attenuation of positional lights (TCastlePointLight and TCastleSpotLight components), equal to 0 0 1 (realistic intensity falloff, following the inverse square law) was not applied. In effect, default lights didn’t have any “attenuation”, which means they were bright even on objects far away from the light position (very unrealistic). The behavior was equivalent to attenuation 0 0 0, although the property value was 0 0 1.

In short, what you may observe after the fix if you open a project created in older engine: Some of lights you have set up in your viewport (using TCastlePointLight and TCastleSpotLight) may look different, in effect your world may look darker.

Why the change is good? New lights behave in much more realistic way. It was obvious to me after doing the fix how many of our previous examples have been unrealistically bright, different than how other 3D software (like Blender or other game engines) treats positional light sources. Previous light look was incorrect, not following the attenuation value (0 0 1 behaved like 0 0 0) and not realistic (no light falloff is not realistic). The new behavior is correct, and much prettier by default.

Default intensity also changes: To account for the fact that new positional lights (TCastlePointLight and TCastleSpotLight) are much quicker fading to darkness, we also changed their default Intensity (TCastlePointLight.Intensity and TCastleSpotLight.Intensity) from 1 to 100. Previous default, 1, was too small to be useful with the new attenuation.

We also adjusted the default light setup when adding a new “Viewport (3D)” component in the editor. To make it work reasonably out of the box, useful for demos etc.

We are now more consistent with other applications. Specifically:

  • Now, after the fixes, our default point light is quite like Blender’s default light. Comparing screenshots, it is obvious now they both have ~inverse square falloff, out of the box. Testing on a plane with boxes setup at 10 units around center — lighting works similar, see the screenshot in this post. This is great.

  • Unity by default adds a directional light to the a new 3D scene, which naturally behaves differently than the point light (attenuation doesn’t matter, and much smaller intensity is reasonable). But if you add point light in Unity, with intensity 100, at height = 3, it behaves similarly to us — again, this is good consistency.

  • We are also consistent with glTF lights, with inverse square falloff (which was why I chosen attenuation = 0 0 1 as a default long time ago). See glTF/extensions/2.0/Khronos/KHR_lights_punctual/README.md, “Within the range of the light, attenuation should follow the inverse square law as closely as possible”.

What to do to in your old projects to adjust to the new lighting?

This fix is necessarily breaking compatibility in some cases. If you relied on the fact that your positional lights by default did not have attenuation (because effectively attenuation 0 0 1 was treated like 0 0 0) then once you upgrade to the new engine version, you will notice that everything is darker.

  1. The quickest, painless solution is to just set the Attenuation of the positional (point, spot) lights from 0 0 1 to 0 0 0. This disables attenuation. You can also readjust the Intensity from 100 back to 1 (without any attenuation, much smaller intensity values are reasonable).

    This is the simplest way to get back exactly the same behavior as before.

  2. The alternative approach, that will take more time, but is also more recommended, is to keep the realistic attenuation (0 0 1) and adjust your light setup. We did this in all CGE examples.

    You can use more lights. You can add an additional directional light to make everything at least slightly brighter. It may even make sense to add 2 directional lights, with different directions in X and Z (but similar in Y) to simulate the daylight best. There’s no straightforward advice here, sorry — you have to experiment and decide what you want to be bright / dark, and how to achieve it using realistic lighting.

    You can also bump intensity of existing point/spot lights. It was automatically upgraded from 1 to 100, but other values (like 2) have not been adjusted. Consider increasing the intensity yourself.

Note: We looked into adding some hack to preserve the look of older setups, to keep compatibility. But it was causing more pain and complications. The problem of the old approach was not “wrong default property value” but “default property value was not properly applied”. We tried two “hacks for compatibility”, to make old lights look like they did, but they seemed both dangerous (making code really ugly to maintain) and confusing to users (since they had to essentially “activate a buggy behavior” under certain conditions). So, we decided to avoid hacks, and break compatibility, to just render stuff 100% correctly from now on.

What does not change, to be clear:

  • If you have customized the attenuation of your lights previously, even very slightly (e.g. to 0 0 1.1) then it was applied OK. The bug only affected you if you left attenuation untouched, exact 0 0 1.

  • Nothing changes in the way how attenuation equation matters (see X3D spec about how attenuation should be applied).

  • The change doesn’t concern X3D nodes for lights (like TPointLightNode) and so nothing changes for lights you set up e.g. in Blender and exported to glTF or X3D. These look the same as before, and everything was and is correct in their case.

  • The default TCastleDirectionalLight.Intensity didn’t change, it is still 1.

Things are better 🙂I (Michalis) feared this change, admittedly — as I realize this is a significant compatibility break. But after testing, and adjusting all our examples to follow it (honor attenuation = 0 0 1, physical)… everything looks so much better.

I hope you enjoy new correct and realistic lighting out-of-the-box!

Comments on the forum ➤

Coding games using Castle Game Engine and Delphi – our presentation from Dev Days of Summer 2024

Posted on

Pirates - at Dev Days Summer 2024!

Our news have been quiet in the last 2 weeks, and in effect we have a backlog of many neat new improvements to announce. So stay tuned, and be prepared for more noise than usual from our blog in the upcoming days 🙂

First of all, our presentation from Embarcadero Dev Days of Summer 2024 is available to watch on YouTube:

If you missed it (it was originally played on August 21st), we recommend you check it out! This is our latest and right now the most up-to-date presentation on how to make a new game using Castle Game Engine.

There’s an associated demo project: Pirate shooting balls at skeletons, using physics. Yeah, as the name implies, it is the most impressive and creative game ever made in 2024! 🙂

You can also browse the slides.

It makes sense for both FPC and Delphi users, naturally — as our engine supports both FPC and Delphi, and everything we show really applies to both. For Pascal editing you can use any IDE, including Delphi, Lazarus and VS Code.

Comments on the forum ➤

Castle Model Viewer release 5.2.0: updated underlying engine (better shadow maps, float-based images for shaders), cycle viewpoints, fixed multi-viewport

Posted on

Castle of Consuegra in Castle Model Viewer - model from https://sketchfab.com/3d-models/castle-of-consuegra-toledo-spain-dd6b50b630904b569bace37e2bfaa559

We’re proud to release new version 5.2.0 of Castle Model Viewer and Castle Model Converter. They are distributed together, just head on to Castle Model Viewer downloads to get them both.

Highlights of this release (since 5.0.0):

  • Engine upgraded to the finally released Castle Game Engine 7.0-alpha.3 version. The previous version was based on an engine effectively somewhere between 7.0-alpha.2 and 7.0-alpha.3.

  • Reworked the shadow maps logic – much simpler now, using the shadows field at light node doesn’t process the scene graph “under the hood” anymore. Some difficult / buggy situations are thus solved and we are ready for future shadow maps improvements.

    Out of necessity, we also had to remove (already deprecated) support for using GeneratedShadowMap node in the Appearance.texture list. This was too convoluted to support (because shadow map does not affect the shape look like other textures on Appearance.texture, the shadow map effect is applied at the specific light source query). You can now only use GeneratedShadowMap inside the defaultShadwMap field of the light node.

  • Float-based images support. In case of Castle Model Viewer, this means that loading 16-bit PNG images of float-based KTX images is useful for shader effects, as shaders will get full float values from the image. This extra precision sometimes matters a lot, e.g. if you use a texture for a heightmap in the shader.

  • Cycle through viewpoints when you use Page Up, Page Down or the corresponding menu items.

  • Fixed display when usign multiple viewports (Display -> 2 Horizontal Viewports, Display -> 4 Viewports).

Have fun with 3D! And consider supporting us on Patreon!

Comments on the forum ➤

7.0-alpha.3 release: physics, blending, batching, shadows, Delphi (packages, Linux), UI masks, lots of editor UX, Android, iOS work, float textures, VS Code, Sketchfab, more…

Posted on

First-person shooter game example
Bad way to play chess
Color picker in "Eye of Beholder" example
Cat with jetpack from Sketchfab - https://sketchfab.com/3d-models/jetpack-cat-6838f7599617408485cd055cd0ce5182
Castle Model Viewer Mobile - Dungeon in Examine Mode
Artstation Challenge - Untamed - Cat Duelist by Marc_A_D\, from Sketchfab - https://sketchfab.com/3d-models/artstation-challenge-untamed-cat-duelist-5bec11c3160048f7a8ce482523ac2deb
(Wireframe View) Artstation Challenge - Untamed - Cat Duelist by Marc_A_D\, from Sketchfab - https://sketchfab.com/3d-models/artstation-challenge-untamed-cat-duelist-5bec11c3160048f7a8ce482523ac2deb
Flying Cthulhu from Sketchfab https://sketchfab.com/3d-models/flying-cthulhu-4737a3b84e00415b9d8bb42ae44285b2 by TooManyDemons
TCastleControl on Delphi FMX form
Inspector debug view
"Lynch" game editor screenshot
"Lynch" statue in Castle Model Viewer
Cathedral minecraft model\, by Patrix\, from https://sketchfab.com/3d-models/cathedral-faed84a829114e378be255414a7826ca
Great lakes - terrain from precise 16-bit PNG ( https://www.motionforgepictures.com/height-maps/ )
Tiled snow map (with animations)

We are proud to present a big new release of Castle Game Engine, with lots of new features, improvements and fixes. The new engine version is available for download now.

The highlights of the release are below. For more details, see the full release notes. The number of this release, “7.0-alpha.3”, is a signal that we’re on our road to the big 7.0 release (see the roadmap and plans and timeline).

This release is dedicated to the late Eugene Loza. I miss you, dear friend.

The most important highlights of this release are:

  • Physics. New components for colliders and rigid bodies, ability to run physics simulation in the editor, joints, layers, forces.

  • Many rendering improvements: Correct and automatic blending, both batching and occlusion culling easier to use and more powerful, fully modern rendering on new GPUs, better support for ancient systems with old GPUs.

  • Also OpenGLES (Android, iOS, Nintendo Switch) rendering supports now almost all desktop rendering features: shadow volumes, shadow maps, 3D textures, occlusion query, anisotropic filtering, meshes with 32-bit indexes.

  • Shadows using shadow volumes are trivial to activate. Work is under way to expose shadow maps through a trivial property as well.

  • Visual Studio Code integration. Our own VS Code “Castle Game Engine” extension with code completion and syntax highlighting for all Pascal code and easy building / running / debugging of Castle Game Engine projects.

  • Delphi integration improvements: TCastleControl available for VCL and FMX applications, Delphi packages with IDE support, Delphi Linux support and more.

  • Support for Tiled maps with a new component that is comfortable to use and efficient to render.

  • User Interface improvements: better default font, UI batching, image regions and 9-slices borders visually configurable, mask component.

  • Many editor UX improvements: modern color picker, unified tools to edit UI and transformations, wireframe view, “Change Class” menu action, Sketchfab Importer, finding components by name, multi-selection.

  • Improvements to views (formerly states): better API, automatic initialization of the published fields, opening the project greets you with UX to manage views.

  • Mobile improvements: lots of Android services improvements, Castle Model Viewer Mobile and more Android applications released on Google Play, lots of general Android and iOS fixes and improvements to make porting your games to mobile trivial, safe borders.

  • Loading and saving: cache, MD3 improvements, X3D 4.0 improvements, custom model formats registration, STL saving.

  • Inspector (press F8 / 3 fingers in a debug build of any project) improvements: tweaking boolean properties at run-time, hot-reloading at run-time to iterate on your graphics.

  • Float-based images (e.g. for precise terrains) full support at loading and rendering (formats: 16-bit PNG, float-based KTX, 16/32-bit TIFFs).

  • A lot of new documentation and examples. We in particular recommend 2 new tutorial articles introducing most important engine concepts: Bad way to play chess, aka 3D physics fun.

Support us

We appreciate your support on Patreon. There are also other ways to support us and if you’re a company, we’re open to cooperation (write to [email protected]). By 2024, Michalis, the lead engine developer, spent most of his life on the engine and doesn’t plan to stop. Moreover, Michalis now works on the engine full-time counting only on your support. So anything you can give is really appreciated!

Spread the word!

We don’t do much marketing. Because we suck at marketing. Help us — spread the word about this new release! Post about it anywhere you can. Talk about it in your communities — online or in the Real World. Tell your family about it (they really want to know). Reuse this news post and screenshots freely.

Have fun everyone and thank you for using the engine.

Comments on the forum ➤

New example space_shooter: space ship shooting rockets, destroying rocks, flying on a tiling background

Posted on

Space shooter example

Enjoy a new example in our engine: examples/space_shooter. Features:

  • Space ship (click anywhere on the screen, or use arrows, to move it).

  • Background (drawn efficiently as one quad, with texture repeating to infinity).

  • Rockets (fired by the player’s cannons). Each rocker has a collider and can detect when it hit an enemy. Rocket can be “armed” (destroys enemies) or “not” (just a ball that bounces off enemies).

  • Enemies, played by the Mighty Rotating Rocks! They just move slowly towards the player. You can shoot them to destroy.

Technically:

  • The player is just a TCastleScene inside a TCastleViewport, with input events handled in TViewMain.

  • Using TCastleComponentFactory to create rockets and rocks. Each of them is defined by a reusable design.

  • Using behaviors to define various behaviors of rocks and rockets.

  • Using physics to handle collisions between rockets and rocks.

  • Special component TTilingBackground to draw a background that (seems to) repeat to infinity. This is a reworked version of an old example background_tiling.

Comments on the forum ➤