Practical Charts and Graphics with Bevy
About this Book
Unlock the power of modern GPU-accelerated visualization with Practical Charts and Graphics with Bevy.
This hands-on guide shows you how to transform the Bevy game engine into a powerful platform for building sophisticated charts, scientific visualizations, and high-performance 3D graphics. Written for developers and graphics enthusiasts, this book bridges the gap between traditional data visualization and real-time rendering—without the complexity of low-level GPU programming.
Instead of working directly with wgpu, you’ll leverage Bevy’s modern architecture to create visually rich applications faster and more efficiently. Through step-by-step examples and real-world projects, you’ll learn how to harness Bevy’s Entity-Component-System (ECS) design to build scalable, modular, and high-performance graphics systems.
Inside, you’ll discover how to create everything from 2D charts—such as line graphs, bar charts, and pie charts—to advanced 3D visualizations including surfaces, wireframes, and interactive charting environments. You’ll also explore GPU-powered techniques like shader programming, procedural terrain generation, marching cubes, and particle systems, enabling you to render complex data in real time.
Packed with ready-to-run examples and reusable components, this book goes beyond theory. You’ll gain practical tools such as colormaps, domain coloring techniques, and shader-based rendering workflows that can be directly integrated into your own projects.
Whether you’re a graphics programmer, game developer, or a Rust enthusiast looking to expand into data visualization, Practical Charts and Graphics with Bevy equips you with the skills to build cutting-edge visual applications. If you already have a foundation in graphics concepts and want to push into modern, GPU-driven design, this book will take you there—step by step.
Table of Contents
Contents v Introduction 1 Overview 1 What this Book Includes 2 Is this Book for You? 3 What Do You Need to Use this Book? 3 How this Book Is Organized 4 Use Code Examples 5 Customer Support 6 1 Get Started 7 1.1 Creating a Bevy Application 7 1.1.1 Setup 7 1.1.2 Bevy Apps 9 1.2 Bevy ECS 9 1.2.1 Core Concepts 10 1.2.2 How Bevy ECS Works 10 1.2.3 Compare with Traditional OOP 14 1.2.4 Resources 15 1.3 Plugins 17 1.3.1 Built-In Plugins 17 1.3.2 Third-Part Plugins 18 1.3.3 Create Bevy Plugins 19 1.4 Color in Bevy 20 1.4.1 Color Conversion 20 1.4.2 Color Palettes 22 1.4.3 Extract Color Names 23 1.4.4 Dynamic Background Color 26 2 2D Primitives 31 2.1 2D Sprite 31 2.2 2D Primitives 33 2.2.1 Create a 2D Triangle 34 2.2.2 Create Basic 2D Shapes 36 2.3 2D Shapes with Stroke 37 2.3.1 Create 2D Shapes with Stroke 38 2.3.2 Create Star Shape 41 2.3.3 Create a US Flag 46 3 2D Line Charts 53 3.1 Coordinate Systems 53 3.1.1 Default Coordinates 53 3.1.2 2D Endless Gridlines 54 3.1.3 Draw Lines 60 3.2 Custom Coordinates 62 3.2.1 Plot Area 63 3.2.2 2D Mask 65 3.2.3 Test 2D Mask 69 3.2.4 Resize 2D Mask 71 3.3 A Simple Line Chart 75 3.3.1 Helper Functions 75 3.3.2 Create a Line Chart 78 3.4 Line Charts with Styles 82 3.4.1 Chart Style 82 3.4.2 Data Series 85 3.4.3 Data Collection 86 3.4.4 Create a Line Chart with Style 89 3.4.5 Dashed Lines 93 3.5 Gridlines and Labels 96 3.5.1 Gridlines 96 3.5.2 Ticks and Labels 103 3.6 Legends 109 3.6.1 Implementation 109 3.6.2 Prelude-Like Imports 113 3.6.3 Chart with Legend 115 4 Specialized 2D Charts 121 4.1 Bar Charts 121 4.1.1 Bar Chart Style 122 4.1.2 Bar Chart Implementation 127 4.1.3 Vertical Bar Charts 132 4.1.4 Horizontal Bar Charts 136 4.1.5 Group Bar Charts 137 4.1.6 Overlay Bar Charts 140 4.1.7 Stacked Bar Charts 141 4.2 Area Charts 143 4.2.1 Implementation 143 4.2.2 Test Area Charts 147 4.3 Pie Charts 151 4.3.1 Implementation 151 4.3.2 Legend for Pie Charts 155 4.3.3 Test Pie Charts 158 4.4 Polar Charts 162 4.4.1 Implementation 162 4.4.2 Legend for Polar Charts 168 4.4.3 Test Polar Charts 170 5 Matrices and Transformations 175 5.1 Matrices and Transformations 176 5.1.1 3D Vector and Matrix Operations 176 5.1.2 Scaling 177 5.1.3 Translation 179 5.1.4 Rotation 180 5.1.5 Combining Transformations 181 5.2 Projections and Viewing 182 5.2.1 Viewing Transform 183 5.2.2 Perspective Projection 185 5.2.3 Orthographic Projection 192 5.3 Model Transformations in Bevy 196 5.3.1 2D Transform Example 197 5.3.2 3D Transform Example 201 5.4 Cameras and Projections in Bevy 205 5.4.1 Cameras 205 5.4.2 2D Camera Setting 206 5.4.3 3D Camera Controls 208 5.4.4 Perspective Projection in Bevy 211 5.4.5 Orthographic Projection in Bevy 213 6 3D Primitives and Wireframes 217 6.1 3D Shapes 217 6.1.1 Cube 220 6.1.2 Tetrahedron 220 6.1.3 Sphere 222 6.1.4 Torus 223 6.1.5 Cylinder 224 6.1.6 Cone 224 6.1.7 Capsule 226 6.2 3D Wireframes 226 6.2.1 Wireframe Plugin 227 6.2.2 Wireframe Example 227 6.3 Vertex Colors 230 7 Colormap and Custom 3D Shapes 235 7.1 Colormap 235 7.1.1 Implementation 236 7.1.2 Color Interpolation 242 7.1.3 Colormap for Mesh 243 7.1.4 Cylinder with Colormap 246 7.2 Custom Shapes 249 7.2.1 Steps for Creating Custom Shapes 249 7.2.2 Creating a Custom Shape 251 7.2.3 Creating a Custom Line 255 7.2.4 Line3d Module 258 7.2.5 Creating 3D lines using Line3d Module 261 7.3 Simple 3D Surfaces 263 7.3.1 Implementation 263 7.3.2 Math Functions 267 7.3.3 Creating Simple 3D Surfaces 270 7.4 Parametric Surfaces 274 7.4.1 Implementation 275 7.4.2 Math Functions 277 7.4.3 Creating Parametric 3D Surfaces 286 8 3D Charts 291 8.1 3D Chart Basics 291 8.1.1 3D Chart Style 291 8.1.2 Coordinate Axes 294 8.1.3 Tick Marks 297 8.1.4 Gridlines 299 8.1.5 Title and Labels 302 8.1.6 Put It All Together 307 8.2 Plugin for 3D Charts 307 8.2.1 Implementation 308 8.2.2 Test 3D Chart Plugin 310 8.3 3D Line Charts 312 8.3.1 Implementation 312 8.3.2 Creating Line Charts 314 8.4 Simple 3D Surface Charts 316 8.4.1 Implementation 316 8.4.2 Creating Simple 3D Surfaces 320 8.4.3 Surface Charts with Built-in Wireframe 323 8.5 Parametric 3D Surface Charts 325 8.5.1 Implementation 325 8.5.2 Creating Parametric 3D Surfaces 330 9 Textures 333 9.1 Texture Coordinates 333 9.1.1 Representation 333 9.1.2 UV Coordinate System in Bevy 334 9.1.3 Storage in Mesh 335 9.1.4 Important Considerations 335 9.2 Texture Mapping in Bevy 335 9.2.1 Cube with Texture 335 9.2.2 Texture Transformations 337 9.2.3 Switching Textures 340 9.2.4 Sphere with Texture 344 9.3 Texture Mapping for Custom Shapes 345 9.3.1 Simple Surfaces with Texture 345 9.3.2 Parametric Surfaces with Texture 349 9.3.3 Fake Cubemap 350 9.4 Normal Mapping 354 9.4.1 Cube with Normal Mapping 355 9.4.2 Sphere with Normal Mapping 358 9.5 Parallax Mapping 359 9.5.1 Parallax Mapping in Bevy 359 9.5.2 Cube with Parallax Mapping 360 9.6 Skybox and Environment Mapping 361 9.6.1 Texture Preparation 361 9.6.2 A Custom Shape in a Skybox 364 9.6.3 Removing the Skybox 367 10 Shader Programming 369 10.1 Manipulate Colors 370 10.1.1 Animate Colors 370 10.1.2 Using Uniform Parameter 375 10.1.3 2D Textures 377 10.2 Manipulate Geometries 382 10.2.1 Triangle Example 382 10.2.2 Simple 3D Surfaces 386 10.3 Customizing Render Phase 397 10.3.1 Shader Code 397 10.3.2 Rust Code 399 10.4 Post Processing 405 10.4.1 Shader Code 405 10.4.2 Rust Code 407 10.5 Compute Shaders 413 10.5.1 Compute Space and Workgroups 413 10.5.2 bevy_app_compute Crate 414 10.5.3 Matrix-Multiplication using Compute Shader 415 10.5.4 Firework Simulation 419 11 Procedural Terrains 431 11.1 Noise Model 431 11.1.1 Perlin Noise 432 11.1.2 Noise Map 432 11.2 Creating Terrains on the CPU 435 11.2.1 Terrain Data 435 11.2.2 Creating Terrains 438 11.3 Water Level 441 11.3.1 Colormap for Terrain 443 11.3.2 Terrain Data with Water Level 444 11.3.3 Rust Code 445 11.4 Terrain Chunks 448 11.4.1 Level of Detail Algorithm 449 11.4.2 Terrain Chunk Data 450 11.4.3 Rust Code 452 11.5 Terrain Instances 455 11.5.1 Instanced Drawing 455 11.5.2 Rust Code 456 11.6 Multiple Terrain Chunks 458 11.7 Terrain Animation 461 11.8 Creating Terrain on GPU 464 11.8.1 WGSL Code for Noise Model 464 11.8.2 Shader for Terrain data 466 11.8.3 Rust Code 469 11.9 Minecraft Terrains 473 11.9.1 Shader Code 473 11.9.2 Rust Code 476 12 Marching Cubes 481 12.1 Marching Cubes Algorithm 481 12.2 Implicit 3D Surfaces 483 12.2.1 Shader Code for Implicit Functions 483 12.2.2 Compute Shader for Values 487 12.2.3 Compute Shader for Implicit Surfaces 488 12.2.4 Rust Code 493 12.3 Metaballs 502 12.3.1 Compute Shader for Values 503 12.3.2 Compute Shader for Metaballs 504 12.3.3 Rust Code 505 12.4 Voxel Terrains 514 12.4.1 Shader Code for 3D Noise 515 12.4.2 Compute Shader for Values 517 12.4.3 Compute Shader for Voxel Terrain 519 12.4.4 Rust Code 523 13 Visualizing Complex Functions 533 13.1 Basics of Complex Numbers 533 13.1.1 Complex Numbers in Rust 534 13.1.2 Complex Numbers in WGSL 535 13.2 3D Surface Plots 538 13.2.1 Complex Functions in Shader 538 13.2.2 Shader for Complex Surface Data 541 13.2.3 Rust Code 545 13.3 Domain Coloring 549 13.3.1 Color Functions in Shader 549 13.3.2 Compute Shader for Domain Coloring 551 13.3.3 Rust Code 552 13.4 Domain Coloring for Iterated Functions 560 13.4.1 Compute Shader for iterated functions 560 13.4.2 Rust Code 562 Index 571
Introduction
        Overview
        What This Book Includes
        Is This Book for You
        How This Book is Organized
        Using Code Examples
Overview
Welcome to Practical Charts and Graphics with Bevy! This book offers an immersive and hands-on approach to creating charts and graphics using Bevy.
Bevy is a next-generation game engine written in Rust that leverages wgpu, a graphics API providing a safe and modern interface for GPU rendering. It features an Entity-Component-System (ECS) architecture and includes modules for rendering, physics, and input handling. By abstracting many low-level details, Bevy simplifies the process of building games and interactive applications. Since Bevy uses wgpu as its graphics backend, developers can take advantage of GPU rendering without dealing directly with the complexities of low-level APIs.
Although Bevy is primarily designed as a game engine, creating charts and graphics in Bevy is significantly easier than working directly with wgpu. Through step-by-step, real-world examples, this book will guide you in building advanced charts, visualizations, and GPU-powered computations using the Bevy engine.
Whether you are a graphics designer, computer graphics programmer, game developer, or a student interested in modern Rust-based graphics development, this book is designed to equip you with the skills and knowledge you need to succeed.
Bevy is an open-source, data-driven engine built in Rust, designed for performance, simplicity, and modularity. Launched in 2020, it emphasizes the ECS architecture while leveraging Rust’s safety and concurrency features. Bevy is licensed under MIT/Apache 2.0, fostering an open and collaborative development community.
At the core of Bevy is its ECS architecture, which efficiently organizes and processes data, enabling scalable and highly parallel game development. ECS structures game logic around three key elements:
- Entities (E): Unique identifiers representing objects. An entity itself does not store data – it is simply an ID to which components can be attached.
- Components (C) Simple data structures that store attributes or properties of an entity. They contain only data, not logic.
- Systems (S) Functions that operate on entities with specific components, defining application logic by processing the associated data.
In simple terms, entities act as containers for components, while systems query and manipulate these components in parallel using Rust’s type-safe and thread-safe features. Bevy’s scheduler automatically manages system execution order and concurrency. This design achieves performance through cache efficiency, scalability by decoupling data from logic, and flexibility through composition—allowing developers to build complex, modular applications without inheritance or boilerplate.
Bevy’s tight integration with Rust ensures memory safety and enables effortless parallelism, making it ideal for both simple 2D visualizations and high-performance 3D graphics.
When creating graphical objects directly in wgpu, developers must manually manage GPU resources such as buffers, textures, and binding groups. They must also write WGSL shader code, define render pipelines, and handle boilerplate for window management, event loops, and input.
By contrast, Bevy provides prebuilt components such as Sprite, Mesh, and Camera, as well as systems like Transform, Input, and physics integration via plugins, which simplify many common tasks. Bevy automates pipeline creation, buffer management, and resource binding, while also handling windowing, input, and rendering loops out of the box.
This book aims to provide a comprehensive guide to Rust-based chart and graphics programming using Bevy, bridging the gap between data visualization and game engine technology. While existing tutorials and documentation focus primarily on game development, this book demonstrates how to repurpose Bevy’s powerful, game-centric tools to create data-driven frameworks for advanced charts, 2D/3D graphics, scientific visualizations, and computational rendering.
Practical Charts and Graphics with Bevy equips you with everything you need to design sophisticated charts and stunning 3D graphics with GPU acceleration. You will learn to create a wide range of 3D visualizations—from basic shapes like cubes, spheres, and cylinders to intricate surfaces, wireframes, procedural terrains, complex function visualizations, and particle systems using compute shaders. By breaking down Bevy’s ECS concepts and graphics systems into approachable lessons, this book makes advanced Rust graphics development accessible even to those with minimal prior experience.
This book is an ideal resource for anyone seeking to design powerful and visually rich charting and graphics applications using the Bevy game engine.
What This Book Includes
This book and its sample code listings, which are available for download at my website at https://drxudotnet.com, provide you with
- A complete and in-depth guide to practical chart and graphics programming using Bevy and Rust. After reading this book and running the example programs, you will be able to create a variety of sophisticated charts and 3D graphics with GPU acceleration in your own applications.
- Ready-to-run example projects that allow you to explore the charting and graphics techniques described in this book. These examples help you understand how charts and visualizations are created using the Bevy game engine. You can modify the code or add new features to build your own projects. Some of the provided examples are already complete, advanced chart and graphics applications that can be directly integrated into real-world projects.
- A collection of reusable functions and components that you will find useful in your chart and graphics development. These include implementations of colormaps, marching cubes, domain coloring, shader code, and various utility functions. You can extract and reuse these components in your own Bevy-based graphics applications.
Is This Book for You
The primary focus of this book is advanced GPU-based chart and graphics programming with Bevy. Therefore, it does not cover the fundamentals of programming in Bevy or Rust. To get the most out of this book, you should have some prior experience with graphics programming in OpenGL or WebGPU and an understanding of 3D rendering concepts such as model coordinates, view coordinates, perspective transformations, and other related mathematical foundations. Familiarity with Rust, wgpu, and game engine programming will also be helpful, as I do not discuss the absolute basics of coding.
It is worth noting that much of the material presented in this book about chart and graphics programming in Bevy is not available in other tutorials or reference books. In addition, most of the example programs provided here can be used directly in your own real-world projects. This book offers detailed explanations, clear instructions, and complete sample code that will enable you to create a wide range of modern charts and graphics using the Bevy game engine.
Many of the example programs in this book are designed for routine use by graphics programmers. Throughout the chapters, I emphasize the practical applications of chart and graphics programming in Bevy for real-world scenarios. By following the step-by-step guidance in this book, you will be able to develop a wide variety of GPU-accelerated graphics applications—from simple charts and geometric shapes to complex 3D surfaces featuring colormaps, wireframes, and texture mapping. You will also learn how to create procedural terrains and implicit surfaces using marching cubes and compute shaders, as well as implement advanced visualizations such as particle systems, and domain coloring for complex functions.
This book does not focus on program style or code optimization, as those topics are well-covered in other resources. For clarity and learning purposes, most of the example programs omit extensive error handling to keep the focus on key concepts and practical techniques.
How This Book Is Organized
This book is structured into thirteen chapters, with each chapter exploring a distinct topic related to chart and graphics programming in Bevy. Below is a summary of each chapter to provide an overview of the book’s contents:
Chapter 1, Get Started
This chapter introduces the fundamentals of graphics programming in Bevy, including setting up the development environment and tools, understanding Bevy’s ECS architecture, and creating plugins. It also explores Bevy’s color system, covering topics such as color conversion, color palettes, and dynamic color implementation.
Chapter 2, 2D Primitives
This chapter introduces Bevy’s built-in 2D primitives for rendering basic shapes like circles, rectangles, and polygons using mesh-based geometry. It also explores the bevy_prototype_lyon crate, which enables vector-style drawing with separate stroke and fill handling, providing a higher-level, declarative approach to creating complex 2D shapes and paths.
Chapter 3, 2D Line Charts
This chapter demonstrates how to use bevy_prototype_lyon to create 2D charts in Bevy with customizable lines, curves, and shapes. By defining paths with ShapeBuilder and ShapePath, you’ll learn how to generate dynamic line graphs with scalable, precise strokes—ideal for axes, gridlines, and plotted data.
Chapter 4, Specialized 2D Charts
This chapter explores specialized chart types—bar, area, pie/donut, and polar charts—commonly used in analytics and scientific visualization. You’ll learn to implement these charts in Bevy to highlight distributions, comparisons, and trends. By the end, you’ll be able to create professional, high-performance visualizations comparable to enterprise-grade charting tools.
Chapter 5, Matrices and Transformations
This chapter bridges the gap between 2D and 3D development in Bevy. You’ll learn about 3D coordinate systems, transformations, and camera projections, as well as how to extract matrices for custom rendering. Through practical examples, you’ll gain the skills to build, position, and manipulate 3D scenes with precision.
Chapter 6, 3D Primitives and Wireframes
This chapter demonstrates how to create 3D shapes and wireframes in Bevy using its built-in primitives and WireframePlugin. You’ll learn to generate cubes, spheres, and other shapes with minimal code and explore how Bevy automates mesh generation, rendering, and materials—making 3D graphics development simpler than low-level graphics APIs such as wgpu.
Chapter 7, Colormap and Custom 3D Shapes
This chapter introduces colormap rendering in Bevy, moving beyond simple solid color shading. It explains how to map vertex data to gradient colors for visualizing mathematical and scientific datasets. Readers learn to create custom colormaps, generate 3D surfaces, and apply color gradients to enhance realism and express data-driven visualizations.
Chapter 8, 3D Charts
This chapter focuses on constructing 3D charts in Bevy using previously learned concepts such as transformations, shapes, and colormaps. It introduces the 3D coordinate system, axes, ticks, labels, and gridlines, then demonstrates building 3D line and surface charts. The chart_style module enables customization and professional visualization of 3D chart elements.
Chapter 9, Textures
This chapter introduces texture mapping in Bevy, advancing beyond solid and colormap rendering to achieve greater realism. It explains how to apply 2D image textures to 3D surfaces using UV mapping, Bevy’s asset system, and shaders. Readers learn to enhance visual detail, depth, and realism in their 3D models.
Chapter 10, Shader Programming
This chapter introduces shader programming in Bevy using WGSL, offering low-level GPU control for custom visual effects and performance optimization. It explains vertex, fragment, and compute shaders, how to integrate them with custom materials and render pipelines, and demonstrates creating dynamic 3D surfaces and advanced visual effects.
Chapter 11, Procedural Terrains
This chapter explores procedural terrain generation in Bevy, focusing on creating realistic 3D landscapes using noise models. It explains the advantages of procedural over manual terrain creation, covers performance optimization with chunks and level of detail (LOD), and demonstrates how vertex shaders enhance terrain realism and rendering efficiency.
Chapter 12, Marching Cubes
This chapter explores the Marching Cubes algorithm, which extracts polygonal meshes from 3D scalar fields by evaluating cube corners and connecting edge intersections. Applications include implicit surfaces, metaballs, and voxel terrains. GPU-based compute shaders are used to accelerate calculations, optimize memory access, and efficiently render complex 3D meshes in parallel.
Chapter 13, Visualizing Complex Functions
This chapter covers complex numbers, functions, and analysis, emphasizing real and imaginary decomposition. Visualization techniques include 3D surface plots and domain coloring, where colors represent function arguments. GPU-based vertex and fragment shaders are used for efficient computation, enabling real-time rendering of complex functions and large grids in Bevy for scientific and engineering applications.
Using Code Examples
You may use the code in this book in your own applications and documentation without needing to contact the author for permission, unless you are reproducing a significant portion of the code. For instance, writing a program that utilizes several code segments from this book does not necessitate permission. However, selling or distributing the example code listings requires permission. If you plan to integrate a substantial amount of example code from this book into your applications and documentation, you will also need permission. Integrating the example code from this book into commercial products is prohibited without written permission from the author.
