3D in the browser – with Ruby
The best known 3D library of the web is called three.js and is written in JavaScript. The gem three-rb gives you the same building blocks in Ruby: you build scene, camera, shapes and materials in pure Ruby – the actual drawing on the graphics card is done by three.js.
This page is made for exactly that: Ruby runs here as WebAssembly, three.js sits right next to it, and the two talk over the JavaScript bridge.
One little trap up front: the gem is called three-rb, but you load it as three. That happens quite often – the gem name and the require name are two different things.
install_gem "three-rb"
require "three"
scene = Three::Scene.new
camera = Three::PerspectiveCamera.new(70, aspect: 460.0 / 320, near: 0.1, far: 100)
camera.position.z = 3
cube = Three::Mesh.new(
Three::BoxGeometry.new(1, 1, 1),
Three::MeshBasicMaterial.new(color: 0xe8722a)
)
scene.add(cube)
show_three scene, cameraEvery 3D picture needs three things:
- a scene (
Three::Scene) – the stage everything stands on; - a camera (
Three::PerspectiveCamera) – the point of view.70is the field of view in degrees,aspectthe aspect ratio,nearandfarlimit the visible depth; - a mesh (
Three::Mesh) – a thing to look at, always made of two parts: a geometry (the shape) and a material (the surface).
Up to that point it is all ordinary Ruby: objects hanging in a tree. Nothing is drawn. Only show_three scene, camera builds a stage below and lets three.js make a picture of it.
Every object has position, rotation and scale – each with x, y and z. Turn the cube a little; the cells share one memory, so cube is still around:
cube.rotation.x = 0.5
cube.rotation.y = 0.8
cube.scale.set(1.4, 1.4, 1.4)
show_three scene, cameraAt least you can see edges now – before, the cube was an orange square. The material is to blame: MeshBasicMaterial is a flat color that does not care about light at all.
It gets three-dimensional with MeshStandardMaterial. That one needs light – without it, it stays black. So here is a second scene, this time lit: an AmbientLight (equally bright everywhere, so nothing is pitch dark) and a DirectionalLight (like the sun: coming from one direction).
scene2 = Three::Scene.new
scene2.add(Three::AmbientLight.new(0xffffff, 0.35))
sun = Three::DirectionalLight.new(0xffffff, 2.5)
sun.position.set(2, 3, 4)
scene2.add(sun)
ball = Three::Mesh.new(
Three::SphereGeometry.new(1, width_segments: 48, height_segments: 24),
Three::MeshStandardMaterial.new(color: 0xc14a2e, roughness: 0.35, metalness: 0.1)
)
scene2.add(ball)
show_three scene2, cameraA still picture is nice, movement is nicer. Hand show_three a block: it runs before every frame – about 60 times per second – and receives the frame number. With orbit: true you can also turn the scene with the mouse and zoom with the wheel.
show_three scene2, camera, orbit: true do |frame|
ball.position.y = Math.sin(frame * 0.05) * 0.5
ball.rotation.y += 0.01
endOn your own machine the setup is the same, except that you attach the renderer to a <canvas> yourself:
renderer = Three::Renderers::ThreeJSRenderer.new(canvas: "#scene")
renderer.set_size(640, 480)
renderer.render(scene, camera)That is exactly what show_three does for you here.
tower, give it light, and stack at least three cubes on top of each other – at different heights via position.y. Show it with show_three tower, camera and let it turn slowly in the block.# tower = Three::Scene.new
# tower.add(Three::AmbientLight.new(0xffffff, 0.4))
# ...
# show_three tower, camera do
# tower.rotation.y += 0.01
# end