1. Products
  2.   3D
  3.   Ruby
  4.   Ray
 
  

Ruby Library to Create & Render 3D Graphics & Game Development

Open Source Ruby 3D Library for Creating and Rendering 3D Graphics & Scenes in Game Development, Simulations, and Roboticsusing via Free Ruby API.

What is Ray Library ?

If you're a Ruby developer with a love for elegant DSLs and a taste for terminal-based graphics, the Ray library is an open source gem worth checking out. Created by Mon-Ouie, Ray offers a domain-specific language (DSL) for building beautiful ASCII and ANSI graphics right in your terminal. Unlike heavier graphics libraries, Ray emphasizes simplicity and expressiveness, allowing you to define visuals with minimal code. The library supports both 2D and 3D ray operations, making it versatile for different types of applications. Let's delve into some of the specific features offered by the library. It supports simplified Game/Window setup (DSL), create and manage scenes, drawing common 2D shapes and text, create basic geometric shapes, vector Math creation, custom transformation matrices support, off-Screen rendering support and so on.

Ray is a Ruby DSL for drawing terminal graphics, allowing you to define shapes, colors, gradients, and layouts using a declarative syntax. The core idea is to treat the terminal as a canvas and build structured graphical elements that can be composed like UI widgets. Think of it like HTML and CSS for your terminal. It takes advantage of ANSI escape codes and Unicode characters to render complex visuals. Ray can render graphics using Unicode characters and box-drawing symbols, which are essential for creating sharp and clean layouts in terminal environments. Ray is a delightful little Ruby library that captures the spirit of minimalism and expressiveness. While it may not replace full-blown TUI (Terminal User Interface) frameworks like curses or tty, it excels in quick layout prototyping, beautiful output formatting, and providing a fun, Ruby-idiomatic way to build terminal visuals.

Previous Next

Getting Started with Ray

The easiest way to install Ray stable release is using RubyGem. Please use the following command for a smooth installation.

Install Ray via RubyGem

gem 'ray', github: 'Mon-Ouie/ray' 

You can download the compiled shared library from Github repository.

Basic Raycasting Visualization via Ruby Library

The Ray Ruby library brings powerful 3D mathematics and raycasting capabilities to Ruby developers in an elegant package. Raycasting is essential for detecting intersections between rays and objects in a 2D or 3D space. The Ray library provides a straightforward way to cast rays and retrieve collision data. Here is a very useful example that demonstrates how to perform basic raycasting visualization using Ruby library.

How to Perform Basic Raycasting Visualization using Ruby?

require 'ray'
require 'ruby2d'

# Set up 2D window
set title: "Raycasting Demo", width: 800, height: 600

# Create a ray from center pointing at mouse
ray = Ray::Ray.new(origin: [400, 300], direction: [1, 0])

# Target circle
target = Circle.new(x: 500, y: 300, radius: 30, color: 'red')

update do
  # Update ray direction toward mouse
  ray.direction = [Window.mouse_x - 400, Window.mouse_y - 300].normalize

  # Check intersection
  if ray.intersect_circle(target.position, target.radius)
    target.color = 'lime'
  else
    target.color = 'red'
  end
end

show

Reflection and Refraction Calculations

The open source Ray library has provided complete support for reflection and refraction calculations inside Ruby applications. The library includes functions to compute ray reflections and refractions, which are useful for lighting effects, mirrors, and optical simulations. Here is a simple example that demonstrates to software developers can calculating ray reflection using Ruby 3D library.

How to Calculate Ray Reflection via Ruby Library??

# Incident ray (light direction)
incident = Ray::Vector[1, -1, 0].normalize

# Surface normal
normal = [0, 1, 0]

# Calculate reflection
reflected = incident.reflect(normal)
puts "Reflected direction: #{reflected}"

Alignment and Positioning

The open source 3D Ray library has provided complete support for 2D and 3D elements alignments inside Ruby applications. It offers simple methods to align and position elements: align, padding, and margin methods give developers control over layout, similar to CSS box models. The following simple example shows how to get multiple lines, aligned to both ends of the canvas using Ruby commands.

How to Get Multiple Lines, Aligned to Both Ends of the Canvas via Ruby API?

Ray.draw(width: 40, height: 10) do
  text "Left aligned" do
    align :left
  end

  text "Right aligned" do
    align :right
  end
end

Box Drawing with Unicode Borders

The Ray 3D library makes it easy for software developers to draw Boxes with Unicode drawing inside Ruby applications. Boxes make your terminal output more structured. Ray uses Unicode box-drawing characters to create elegant borders. The box method wraps your content in a neat border using characters like ┌ ─ ┐ and └ ┘ as shown in the following code example.

How to Wraps Content in a Neat Box with Border using Ruby Library?

Ray.draw(width: 50, height: 8) do
  box do
    text "Welcome to Ray UI!" do
      align :center
    end
  end
end