Update to the latest version of OSGL here!
About
What is OSGL?
OSGL (Open-Source Graphical Library) is a fast and lightweight graphical library that makes it easy to render pixels onto EditableImage
objects. With support for drawing shapes, loading textures, and manipulating pixel data, OSGL was built from the ground-up for performance and simplicity.
Originally inspired by CanvasDraw, OSGL was created specifically for Roblox developers who need a reliable tool for pixel-based rendering. It focuses on doing one thing well: giving developers an efficient way to create graphics using the in-built EditableImage
.
Currently in beta, OSGL is already the fastest public EditableImage
library available. Whether you’re building pixel art, animations, or unique visual effects, OSGL will allow you to get what you need, done. If you’re among the users who can use OSGL in live games, we’d love to see what you create!
So, what can OSGL do?
In OSGL, a “Window” is just another name for an EditableImage; a canvas where you can draw anything. You can create shapes, color individual pixels, or load textures directly from your PC in multiple formats to use in-game.
OSGL also includes built-in error handling to make your life easier, catching common EditableImage issues, like the dreaded “buffer out of bounds” error! Scary!
Examples
OSGL v1.2b, v1.3b v1.4b currently have no examples.
OSGL v1.1b Examples
144P Shrek, sampled at 15FPS:
Minecraft Wireframe 3D Renderer (60FPS):
Live shading and rotating render:
Quick tutorial
This guide explains how to draw a circle using OSGL.
Note: This tutorial assumes a basic understanding of
Luau
. If you are new to the language, refer to available learning resources.
For additional information, visit the OSGL Documentation.
Setting Up
-
Download OSGL
Obtain the latest version of OSGL from either:Insert the downloaded module into your project, such as
ReplicatedStorage/Packages
. -
Create an ImageLabel
Add anImageLabel
toStarterGui
. Set theBackgroundTransparency
property to0
. ThisImageLabel
will serve as your canvas for rendering.Tip: If you encounter blurred images at low resolutions, or if you want a pixelated look, set the
ResampleMode
property of theImageLabel
toPixelated
. -
Add a LocalScript
Create aLocalScript
in an appropriate location (e.g.,StarterPlayer/StarterPlayerScripts
). This script will manage the OSGL window and rendering.
Creating a Window
An OSGL window is essentially a wrapper for an EditableImage
, providing a simplified API for rendering. To create a window:
- Import OSGL Modules
Copy code
local OSGL = require(ReplicatedStorage.Packages.OSGL)
local Window = OSGL.Window
- Choose a Window creation method
OSGL provides several functions to create a window:
-
Window.from
: Creates an OSGL window from an existing EditableImage. -
Window.new
: Creates an OSGL window by initializing a new EditableImage instance at the specified location. -
Window.fromAssetId
: Given an assetId, creates a Window. -
Window.fromBuffer
: Given a buffer, creates a Window.
Since we are starting fresh, we’ll use Window.new
:
Copy code
local OSGL = require(ReplicatedStorage.Packages.OSGL)
local Window = OSGL.Window
local windowUi = -- *reference to windowUi, our `ImageLabel`*
-- Create our window, 500x500
local myWindow = Window.new(windowUi, { sizeX = 500, sizeY = 500 })
At this point, the window is ready for rendering.
Rendering to the Window
The following example sets the target frame rate, clears the screen to black, and renders the updated buffer:
Copy code
local OSGL = require(ReplicatedStorage.Packages.OSGL)
local Window = OSGL.Window
local color = OSGL.color
local windowUi -- reference to windowUi, our `ImageLabel`
-- Create our window, 500x500
local myWindow = Window.new(windowUi, { sizeX = 500, sizeY = 500 })
myWindow.targetFPS = 270
myWindow
:Clear(color.BLACK)
:Render()
-
Clear(color)
: Clears the window with the specified color. -
Render()
: Displays the updated content.
It’s important to note that the method calls do not need to be chained. You can achieve the same effect with separate statements, as shown below:
Copy code
myWindow:Clear(color.BLACK)
myWindow:Render()
The draw
sub-module can be utilized to render directly onto the buffer. All drawing functions require a Window
or Texture
(known as a DrawableObject
) to specify where to draw. As an example, a pixel could be drawn by using the appropriate draw function provided by OSGL:
Copy code
myWindow:Clear(color.BLACK)
-- It isn't necessary to clear the screen. If you want to keep the contents
-- of the previous frame, you can!
-- Draw a red pixel on `myWindow`, at 0, 0
draw.pixel(myWindow, 0, 0, color.RED)
myWindow:Render()
In this example, each operation within the loop is presented as separate statements. However, these statements can be combined into a single statement using method chaining, as shown below:
Copy code
myWindow
:Clear(color.BLACK)
:Draw() -- Open a `DrawingContext`
:Pixel(0, 0, color.RED) -- `myWindow` is automatically passed as the first argument
:StopDrawing()
:Render()
-
Draw()
: Opens a drawing context, allowing you to use drawing functions. -
Pixel(x, y, color)
: Draws a pixel at a location with the given color. -
StopDrawing()
: Ends the drawing context and returns theWindow
object.
The draw.circle
function, or the Circle
method can be used to draw a circle onto the Window
:
Copy code
myWindow
:Clear(color.BLACK)
:Draw()
:Circle(250, 250, 50, color.RED)
:StopDrawing()
:Render()
This code will draw a red circle at (250, 250), with a radius of 50:
Congratulations! You’ve rendered your first circle using OSGL. For more details on other features and functions, refer to the OSGL Documentation.
Full code
local OSGL = require(ReplicatedStorage.Packages.OSGL)
local Window = OSGL.Window
local color = OSGL.color
local windowUi -- reference to windowUi, our `ImageLabel`
-- Create our window, 500x500
local myWindow = Window.new(windowUi, { sizeX = 500, sizeY = 500 })
myWindow.targetFPS = 270
myWindow
:Clear(color.BLACK)
:Draw()
:Circle(250, 250, 50, color.RED)
:StopDrawing()
:Render()
Contribution
OSGL is an open-source project, and we welcome your contributions! You’re encouraged to edit the source code and share your ideas. Feel free to participate via GitHub, Discord, or even on the DevForum. Your involvement is greatly appreciated!
Credits
While you do not need to credit me for using OSGL, acknowledging the original creator is always appreciated but entirely optional. You are free to modify the source code as you wish, but please refrain from reuploading or claiming the asset as your own work.