3rb 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/3rb.gemspec +29 -0
- data/CHANGELOG.md +12 -0
- data/LICENSE +21 -0
- data/README.md +321 -0
- data/Rakefile +13 -0
- data/examples/01_hello_cube.rb +29 -0
- data/examples/02_basic_geometries.rb +56 -0
- data/examples/03_materials.rb +61 -0
- data/examples/04_lighting.rb +63 -0
- data/examples/05_animation.rb +79 -0
- data/examples/06_custom_shader.rb +92 -0
- data/examples/07_scene_graph.rb +74 -0
- data/examples/08_orbit_controls.rb +50 -0
- data/examples/09_3d_chart.rb +71 -0
- data/examples/10_procedural_terrain.rb +140 -0
- data/examples/11_particle_system.rb +68 -0
- data/examples/12_model_loader.rb +73 -0
- data/examples/13_game_prototype.rb +145 -0
- data/examples/14_utah_teapot.rb +291 -0
- data/examples/15_stanford_bunny.rb +200 -0
- data/examples/16_cornell_box.rb +373 -0
- data/examples/17_weird_fractal4.rb +130 -0
- data/examples/18_platonic_solids.rb +268 -0
- data/lib/3rb/animation/animation_clip.rb +287 -0
- data/lib/3rb/animation/animation_mixer.rb +366 -0
- data/lib/3rb/cameras/camera.rb +50 -0
- data/lib/3rb/cameras/orthographic_camera.rb +92 -0
- data/lib/3rb/cameras/perspective_camera.rb +103 -0
- data/lib/3rb/controls/orbit_controls.rb +341 -0
- data/lib/3rb/core/buffer_attribute.rb +172 -0
- data/lib/3rb/core/group.rb +9 -0
- data/lib/3rb/core/object3d.rb +298 -0
- data/lib/3rb/core/scene.rb +78 -0
- data/lib/3rb/dsl/helpers.rb +57 -0
- data/lib/3rb/dsl/scene_builder.rb +288 -0
- data/lib/3rb/ffi/glfw.rb +61 -0
- data/lib/3rb/ffi/opengl.rb +137 -0
- data/lib/3rb/ffi/platform.rb +65 -0
- data/lib/3rb/geometries/box_geometry.rb +101 -0
- data/lib/3rb/geometries/buffer_geometry.rb +345 -0
- data/lib/3rb/geometries/cone_geometry.rb +29 -0
- data/lib/3rb/geometries/cylinder_geometry.rb +149 -0
- data/lib/3rb/geometries/plane_geometry.rb +75 -0
- data/lib/3rb/geometries/sphere_geometry.rb +93 -0
- data/lib/3rb/geometries/torus_geometry.rb +77 -0
- data/lib/3rb/lights/ambient_light.rb +9 -0
- data/lib/3rb/lights/directional_light.rb +57 -0
- data/lib/3rb/lights/hemisphere_light.rb +26 -0
- data/lib/3rb/lights/light.rb +27 -0
- data/lib/3rb/lights/point_light.rb +68 -0
- data/lib/3rb/lights/rect_area_light.rb +35 -0
- data/lib/3rb/lights/spot_light.rb +88 -0
- data/lib/3rb/loaders/gltf_loader.rb +304 -0
- data/lib/3rb/loaders/loader.rb +94 -0
- data/lib/3rb/loaders/obj_loader.rb +186 -0
- data/lib/3rb/loaders/texture_loader.rb +55 -0
- data/lib/3rb/materials/basic_material.rb +70 -0
- data/lib/3rb/materials/lambert_material.rb +102 -0
- data/lib/3rb/materials/material.rb +114 -0
- data/lib/3rb/materials/phong_material.rb +106 -0
- data/lib/3rb/materials/shader_material.rb +104 -0
- data/lib/3rb/materials/standard_material.rb +106 -0
- data/lib/3rb/math/color.rb +246 -0
- data/lib/3rb/math/euler.rb +156 -0
- data/lib/3rb/math/math_utils.rb +132 -0
- data/lib/3rb/math/matrix3.rb +269 -0
- data/lib/3rb/math/matrix4.rb +501 -0
- data/lib/3rb/math/quaternion.rb +337 -0
- data/lib/3rb/math/vector2.rb +216 -0
- data/lib/3rb/math/vector3.rb +366 -0
- data/lib/3rb/math/vector4.rb +233 -0
- data/lib/3rb/native/gl.rb +382 -0
- data/lib/3rb/native/native.rb +55 -0
- data/lib/3rb/native/window.rb +111 -0
- data/lib/3rb/native.rb +9 -0
- data/lib/3rb/objects/line.rb +116 -0
- data/lib/3rb/objects/mesh.rb +40 -0
- data/lib/3rb/objects/points.rb +71 -0
- data/lib/3rb/renderers/opengl_renderer.rb +567 -0
- data/lib/3rb/renderers/renderer.rb +60 -0
- data/lib/3rb/renderers/shader_lib.rb +100 -0
- data/lib/3rb/textures/cube_texture.rb +26 -0
- data/lib/3rb/textures/data_texture.rb +35 -0
- data/lib/3rb/textures/render_target.rb +125 -0
- data/lib/3rb/textures/texture.rb +190 -0
- data/lib/3rb/version.rb +5 -0
- data/lib/3rb.rb +86 -0
- data/shaders/basic.frag +19 -0
- data/shaders/basic.vert +15 -0
- data/shaders/common/lights.glsl +53 -0
- data/shaders/common/uniforms.glsl +9 -0
- data/shaders/lambert.frag +37 -0
- data/shaders/lambert.vert +22 -0
- data/shaders/phong.frag +51 -0
- data/shaders/phong.vert +28 -0
- data/shaders/standard.frag +92 -0
- data/shaders/standard.vert +28 -0
- metadata +155 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c2ee6abb7f305cbf4e04e10e4a4f9400534ff7254af3c88cdc109563927bff45
|
|
4
|
+
data.tar.gz: 4c15b24c1cbb52719563fe14e34a0a1a9e7ec94eecffda8d47df006dd1df9296
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e3c71db17e64723328d37c895b47a21f4a77c57c4ca969a4cac01b36d77381ef73227023f15ec7b3b66e9ba52f538f72ade36f73fc80d38fd1aaaf07c59a189f
|
|
7
|
+
data.tar.gz: ad86cd39eafafb133488859d44655657880815a832132fec4ebb18544747f55f6d6064aae1026e8a20807a1ce7c2115ca55acb9e5521070cb098119c494d786b
|
data/.rspec
ADDED
data/3rb.gemspec
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/3rb/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "3rb"
|
|
7
|
+
spec.version = Sunrb::VERSION
|
|
8
|
+
spec.authors = ["Yudai Takada"]
|
|
9
|
+
spec.email = ["t.yudai92@gmail.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "3D graphics library for Ruby inspired by Three.js"
|
|
12
|
+
spec.description = "A powerful, intuitive 3D graphics library for Ruby with a Three.js-like API and Ruby DSL"
|
|
13
|
+
spec.homepage = "https://github.com/ydah/3rb"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
spec.required_ruby_version = ">= 3.0.0"
|
|
16
|
+
|
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
18
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
|
19
|
+
|
|
20
|
+
spec.files = Dir.chdir(__dir__) do
|
|
21
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
22
|
+
(File.expand_path(f) == __FILE__) ||
|
|
23
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
spec.require_paths = ["lib"]
|
|
27
|
+
|
|
28
|
+
spec.add_dependency "ffi", "~> 1.15"
|
|
29
|
+
end
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## Unreleased
|
|
9
|
+
|
|
10
|
+
## 0.1.0 - 2025-12-31
|
|
11
|
+
|
|
12
|
+
- Initial release
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yudai Takada
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
# 3rb
|
|
2
|
+
|
|
3
|
+
A 3D graphics library for Ruby inspired by Three.js.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Provides a Three.js-like API
|
|
8
|
+
- Includes a DSL for building scenes with Ruby blocks
|
|
9
|
+
- Uses OpenGL/GLFW via FFI (no compilation required)
|
|
10
|
+
- Supports PBR rendering
|
|
11
|
+
- Contains math classes for Vector, Matrix, Quaternion, Euler, Color
|
|
12
|
+
- Has basic geometries like Box, Sphere, Cylinder, Torus
|
|
13
|
+
- Supports Basic, Lambert, Phong, Standard, and Shader materials
|
|
14
|
+
- Includes Ambient, Directional, Point, and Spot lights
|
|
15
|
+
- Supports keyframe animation
|
|
16
|
+
- Has OrbitControls for camera manipulation
|
|
17
|
+
- Can load OBJ and GLTF files
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
Add this line to your Gemfile:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
gem "3rb"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Then execute:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
bundle install
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### System Dependencies
|
|
34
|
+
|
|
35
|
+
**macOS:**
|
|
36
|
+
```bash
|
|
37
|
+
brew install glfw
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Ubuntu/Debian:**
|
|
41
|
+
```bash
|
|
42
|
+
sudo apt-get install libglfw3-dev libgl1-mesa-dev
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Quick Start
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
require "3rb"
|
|
49
|
+
|
|
50
|
+
scene = Sunrb::Scene.new
|
|
51
|
+
scene.background = Sunrb::Color.new(0x1a1a2e)
|
|
52
|
+
|
|
53
|
+
camera = Sunrb::PerspectiveCamera.new(fov: 75)
|
|
54
|
+
camera.position.set(0, 0, 5)
|
|
55
|
+
|
|
56
|
+
geometry = Sunrb::BoxGeometry.new(width: 1, height: 1, depth: 1)
|
|
57
|
+
material = Sunrb::MeshStandardMaterial.new(color: 0x00ff00)
|
|
58
|
+
cube = Sunrb::Mesh.new(geometry, material)
|
|
59
|
+
scene.add(cube)
|
|
60
|
+
|
|
61
|
+
light = Sunrb::DirectionalLight.new(color: 0xffffff, intensity: 1.0)
|
|
62
|
+
light.position.set(5, 5, 5)
|
|
63
|
+
scene.add(light)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Using the DSL
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
require "3rb"
|
|
70
|
+
|
|
71
|
+
Sunrb.scene do
|
|
72
|
+
background 0x000033
|
|
73
|
+
|
|
74
|
+
camera :perspective, fov: 60, position: [0, 5, 10]
|
|
75
|
+
|
|
76
|
+
ambient_light color: 0x404040
|
|
77
|
+
directional_light color: 0xffffff, position: [5, 10, 5]
|
|
78
|
+
|
|
79
|
+
mesh :box, size: 2, color: 0xff0000, position: [0, 1, 0]
|
|
80
|
+
mesh :sphere, radius: 1, color: 0x00ff00, position: [3, 1, 0]
|
|
81
|
+
end
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Core Classes
|
|
85
|
+
|
|
86
|
+
### Math
|
|
87
|
+
|
|
88
|
+
| Class | Description |
|
|
89
|
+
|-------|-------------|
|
|
90
|
+
| `Vector2` | 2D vector |
|
|
91
|
+
| `Vector3` | 3D vector |
|
|
92
|
+
| `Vector4` | 4D vector |
|
|
93
|
+
| `Matrix3` | 3x3 matrix |
|
|
94
|
+
| `Matrix4` | 4x4 transformation matrix |
|
|
95
|
+
| `Quaternion` | Rotation quaternion |
|
|
96
|
+
| `Euler` | Euler angles |
|
|
97
|
+
| `Color` | RGB color |
|
|
98
|
+
|
|
99
|
+
### Geometries
|
|
100
|
+
|
|
101
|
+
| Class | Description |
|
|
102
|
+
|-------|-------------|
|
|
103
|
+
| `BoxGeometry` | Rectangular cuboid |
|
|
104
|
+
| `SphereGeometry` | UV sphere |
|
|
105
|
+
| `PlaneGeometry` | Flat plane |
|
|
106
|
+
| `CylinderGeometry` | Cylinder |
|
|
107
|
+
| `ConeGeometry` | Cone |
|
|
108
|
+
| `TorusGeometry` | Torus (donut) |
|
|
109
|
+
| `CircleGeometry` | Flat circle |
|
|
110
|
+
| `RingGeometry` | Flat ring |
|
|
111
|
+
| `TorusKnotGeometry` | Torus knot |
|
|
112
|
+
| `IcosahedronGeometry` | Icosahedron |
|
|
113
|
+
| `OctahedronGeometry` | Octahedron |
|
|
114
|
+
| `TetrahedronGeometry` | Tetrahedron |
|
|
115
|
+
| `DodecahedronGeometry` | Dodecahedron |
|
|
116
|
+
|
|
117
|
+
### Materials
|
|
118
|
+
|
|
119
|
+
| Class | Description |
|
|
120
|
+
|-------|-------------|
|
|
121
|
+
| `MeshBasicMaterial` | Unlit, flat shading |
|
|
122
|
+
| `MeshLambertMaterial` | Diffuse (matte) |
|
|
123
|
+
| `MeshPhongMaterial` | Specular highlights |
|
|
124
|
+
| `MeshStandardMaterial` | PBR material |
|
|
125
|
+
| `ShaderMaterial` | Custom GLSL shaders |
|
|
126
|
+
| `LineBasicMaterial` | For line rendering |
|
|
127
|
+
| `PointsMaterial` | For point clouds |
|
|
128
|
+
|
|
129
|
+
### Lights
|
|
130
|
+
|
|
131
|
+
| Class | Description |
|
|
132
|
+
|-------|-------------|
|
|
133
|
+
| `AmbientLight` | Uniform lighting |
|
|
134
|
+
| `DirectionalLight` | Sun-like parallel rays |
|
|
135
|
+
| `PointLight` | Omni-directional |
|
|
136
|
+
| `SpotLight` | Cone-shaped |
|
|
137
|
+
|
|
138
|
+
### Objects
|
|
139
|
+
|
|
140
|
+
| Class | Description |
|
|
141
|
+
|-------|-------------|
|
|
142
|
+
| `Mesh` | Geometry + Material |
|
|
143
|
+
| `Line` | Line rendering |
|
|
144
|
+
| `Points` | Point cloud |
|
|
145
|
+
| `Group` | Container for objects |
|
|
146
|
+
| `Scene` | Root container |
|
|
147
|
+
|
|
148
|
+
## Examples
|
|
149
|
+
|
|
150
|
+
The `examples/` directory contains 18 example files:
|
|
151
|
+
|
|
152
|
+
### Basic
|
|
153
|
+
- `01_hello_cube.rb` - Simple rotating cube
|
|
154
|
+
- `02_basic_geometries.rb` - All geometry types
|
|
155
|
+
- `03_materials.rb` - Material showcase
|
|
156
|
+
- `04_lighting.rb` - Light types demo
|
|
157
|
+
|
|
158
|
+
### Advanced
|
|
159
|
+
- `05_animation.rb` - Keyframe animation
|
|
160
|
+
- `06_custom_shader.rb` - GLSL shaders
|
|
161
|
+
- `07_scene_graph.rb` - Hierarchical transforms
|
|
162
|
+
- `08_orbit_controls.rb` - Camera controls
|
|
163
|
+
|
|
164
|
+
### Practical
|
|
165
|
+
- `09_3d_chart.rb` - Data visualization
|
|
166
|
+
- `10_procedural_terrain.rb` - Terrain generation
|
|
167
|
+
- `11_particle_system.rb` - Particle effects
|
|
168
|
+
- `12_model_loader.rb` - OBJ/GLTF loading
|
|
169
|
+
- `13_game_prototype.rb` - Simple game
|
|
170
|
+
|
|
171
|
+
### Classic 3D Models
|
|
172
|
+
- `14_utah_teapot.rb` - The famous Utah Teapot
|
|
173
|
+
- `15_stanford_bunny.rb` - Stanford Bunny
|
|
174
|
+
- `16_cornell_box.rb` - Cornell Box
|
|
175
|
+
- `17_suzanne.rb` - Blender's Suzanne
|
|
176
|
+
- `18_platonic_solids.rb` - Five Platonic solids
|
|
177
|
+
|
|
178
|
+
Run an example:
|
|
179
|
+
```bash
|
|
180
|
+
ruby examples/01_hello_cube.rb
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## API Reference
|
|
184
|
+
|
|
185
|
+
### Vector3
|
|
186
|
+
|
|
187
|
+
```ruby
|
|
188
|
+
v = Sunrb::Vector3.new(1, 2, 3)
|
|
189
|
+
|
|
190
|
+
v.length # => 3.7416...
|
|
191
|
+
v.normalize! # Normalize in place
|
|
192
|
+
v.normalized # Return normalized copy
|
|
193
|
+
|
|
194
|
+
v.dot(other) # Dot product
|
|
195
|
+
v.cross(other) # Cross product
|
|
196
|
+
|
|
197
|
+
v + other # Addition
|
|
198
|
+
v - other # Subtraction
|
|
199
|
+
v * scalar # Scalar multiplication
|
|
200
|
+
|
|
201
|
+
v.set(x, y, z) # Set components
|
|
202
|
+
v.copy(other) # Copy from another vector
|
|
203
|
+
v.clone # Create a copy
|
|
204
|
+
|
|
205
|
+
v.to_a # => [1.0, 2.0, 3.0]
|
|
206
|
+
v.to_h # => {x: 1.0, y: 2.0, z: 3.0}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Matrix4
|
|
210
|
+
|
|
211
|
+
```ruby
|
|
212
|
+
m = Sunrb::Matrix4.new
|
|
213
|
+
|
|
214
|
+
m.identity!
|
|
215
|
+
m.make_translation(x, y, z)
|
|
216
|
+
m.make_rotation_x(angle)
|
|
217
|
+
m.make_rotation_y(angle)
|
|
218
|
+
m.make_rotation_z(angle)
|
|
219
|
+
m.make_scale(x, y, z)
|
|
220
|
+
|
|
221
|
+
m.multiply(other)
|
|
222
|
+
m.invert!
|
|
223
|
+
m.transpose!
|
|
224
|
+
|
|
225
|
+
m.decompose(position, quaternion, scale)
|
|
226
|
+
m.compose(position, quaternion, scale)
|
|
227
|
+
|
|
228
|
+
m.make_perspective(fov, aspect, near, far)
|
|
229
|
+
m.make_orthographic(left, right, top, bottom, near, far)
|
|
230
|
+
m.look_at(eye, target, up)
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Object3D
|
|
234
|
+
|
|
235
|
+
```ruby
|
|
236
|
+
obj = Sunrb::Object3D.new
|
|
237
|
+
|
|
238
|
+
obj.position # Vector3
|
|
239
|
+
obj.rotation # Euler
|
|
240
|
+
obj.quaternion # Quaternion
|
|
241
|
+
obj.scale # Vector3
|
|
242
|
+
|
|
243
|
+
obj.add(child)
|
|
244
|
+
obj.remove(child)
|
|
245
|
+
obj.children # Array of children
|
|
246
|
+
obj.parent # Parent object
|
|
247
|
+
|
|
248
|
+
obj.look_at(target)
|
|
249
|
+
obj.update_matrix
|
|
250
|
+
obj.update_world_matrix(update_parents, update_children)
|
|
251
|
+
|
|
252
|
+
obj.traverse { |child| ... }
|
|
253
|
+
obj.get_object_by_name(name)
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Mesh
|
|
257
|
+
|
|
258
|
+
```ruby
|
|
259
|
+
geometry = Sunrb::BoxGeometry.new(width: 1, height: 1, depth: 1)
|
|
260
|
+
material = Sunrb::MeshStandardMaterial.new(
|
|
261
|
+
color: 0xff0000,
|
|
262
|
+
roughness: 0.5,
|
|
263
|
+
metalness: 0.5
|
|
264
|
+
)
|
|
265
|
+
mesh = Sunrb::Mesh.new(geometry, material)
|
|
266
|
+
|
|
267
|
+
mesh.geometry # BufferGeometry
|
|
268
|
+
mesh.material # Material
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Development
|
|
272
|
+
|
|
273
|
+
```bash
|
|
274
|
+
git clone https://github.com/ydah/3rb.git
|
|
275
|
+
cd 3rb
|
|
276
|
+
bundle install
|
|
277
|
+
|
|
278
|
+
# Run tests
|
|
279
|
+
bundle exec rspec
|
|
280
|
+
|
|
281
|
+
# Run specific test
|
|
282
|
+
bundle exec rspec spec/math/vector3_spec.rb
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
## Architecture
|
|
286
|
+
|
|
287
|
+
```
|
|
288
|
+
lib/3rb/
|
|
289
|
+
├── animation/ # Animation system
|
|
290
|
+
├── cameras/ # Camera types
|
|
291
|
+
├── controls/ # OrbitControls
|
|
292
|
+
├── core/ # Object3D, Scene, Group
|
|
293
|
+
├── dsl/ # Ruby DSL
|
|
294
|
+
├── ffi/ # FFI bindings
|
|
295
|
+
│ ├── platform.rb # Platform detection
|
|
296
|
+
│ ├── glfw.rb # GLFW bindings
|
|
297
|
+
│ └── opengl.rb # OpenGL bindings
|
|
298
|
+
├── geometries/ # Geometry primitives
|
|
299
|
+
├── lights/ # Light types
|
|
300
|
+
├── loaders/ # OBJ, GLTF loaders
|
|
301
|
+
├── materials/ # Material types
|
|
302
|
+
├── math/ # Vector, Matrix, etc.
|
|
303
|
+
├── native/ # Native module wrappers
|
|
304
|
+
│ ├── native.rb # Sunrb::Native module
|
|
305
|
+
│ ├── window.rb # Window class
|
|
306
|
+
│ └── gl.rb # GL module
|
|
307
|
+
├── objects/ # Mesh, Line, Points
|
|
308
|
+
├── renderers/ # OpenGL renderer
|
|
309
|
+
└── textures/ # Texture handling
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
## License
|
|
313
|
+
|
|
314
|
+
MIT License. See [LICENSE](LICENSE) for details.
|
|
315
|
+
|
|
316
|
+
## Acknowledgments
|
|
317
|
+
|
|
318
|
+
- [Three.js](https://threejs.org/) - The inspiration for this library
|
|
319
|
+
- [OpenGL](https://www.opengl.org/) - Graphics API
|
|
320
|
+
- [GLFW](https://www.glfw.org/) - Window and input handling
|
|
321
|
+
- [FFI](https://github.com/ffi/ffi) - Foreign Function Interface for Ruby
|
data/Rakefile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rspec/core/rake_task"
|
|
5
|
+
require "rake/extensiontask"
|
|
6
|
+
|
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
8
|
+
|
|
9
|
+
Rake::ExtensionTask.new("three_rb") do |ext|
|
|
10
|
+
ext.lib_dir = "lib/three_rb"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
task default: :spec
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# Simple Cube - The most basic way to create a 3D scene
|
|
5
|
+
|
|
6
|
+
require_relative "../lib/3rb"
|
|
7
|
+
|
|
8
|
+
Sunrb.app do
|
|
9
|
+
background 0x1a1a2e
|
|
10
|
+
|
|
11
|
+
perspective_camera(fov: 75, near: 0.1, far: 1000) do
|
|
12
|
+
position 0, 0, 5
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
ambient_light color: 0x404040
|
|
16
|
+
directional_light(color: 0xffffff, intensity: 1.0) do
|
|
17
|
+
position 5, 5, 5
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
mesh :cube do
|
|
21
|
+
geometry :box, width: 1, height: 1, depth: 1
|
|
22
|
+
material :standard, color: 0x00ff00
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
on_frame do |delta|
|
|
26
|
+
cube.rotation.x += 0.01
|
|
27
|
+
cube.rotation.y += 0.01
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# Basic Geometries - All available primitive shapes
|
|
5
|
+
|
|
6
|
+
require_relative "../lib/3rb"
|
|
7
|
+
|
|
8
|
+
Sunrb.app do
|
|
9
|
+
background 0x2d3436
|
|
10
|
+
|
|
11
|
+
perspective_camera(fov: 60) do
|
|
12
|
+
position 0, 3, 12
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
ambient_light color: 0x404040
|
|
16
|
+
directional_light(intensity: 0.8) do
|
|
17
|
+
position 10, 10, 10
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
mesh :box do
|
|
21
|
+
geometry :box, width: 1.5, height: 1.5, depth: 1.5
|
|
22
|
+
material :standard, color: 0xe74c3c
|
|
23
|
+
position -4, 0, 0
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
mesh :sphere do
|
|
27
|
+
geometry :sphere, radius: 1, width_segments: 32, height_segments: 32
|
|
28
|
+
material :standard, color: 0x3498db
|
|
29
|
+
position -1.5, 0, 0
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
mesh :cylinder do
|
|
33
|
+
geometry :cylinder, radius_top: 0.5, radius_bottom: 0.8, height: 2
|
|
34
|
+
material :standard, color: 0x2ecc71
|
|
35
|
+
position 1.5, 0, 0
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
mesh :torus do
|
|
39
|
+
geometry :torus, radius: 0.8, tube: 0.3
|
|
40
|
+
material :standard, color: 0x9b59b6
|
|
41
|
+
position 4, 0, 0
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
mesh :plane do
|
|
45
|
+
geometry :plane, width: 15, height: 15
|
|
46
|
+
material :standard, color: 0x7f8c8d
|
|
47
|
+
position 0, -1.5, 0
|
|
48
|
+
rotation -Math::PI / 2, 0, 0
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
on_frame do |delta|
|
|
52
|
+
[box, sphere, cylinder, torus].each do |obj|
|
|
53
|
+
obj.rotation.y += 0.01
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# Material Types - Basic, Lambert, Phong, and Standard materials
|
|
5
|
+
|
|
6
|
+
require_relative "../lib/3rb"
|
|
7
|
+
|
|
8
|
+
Sunrb.app do
|
|
9
|
+
background 0x1a1a2e
|
|
10
|
+
|
|
11
|
+
perspective_camera(fov: 50) do
|
|
12
|
+
position 0, 2, 10
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
ambient_light color: 0x404040
|
|
16
|
+
|
|
17
|
+
point_light(color: 0xffffff, intensity: 1.0, distance: 50) do
|
|
18
|
+
position 5, 5, 5
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
point_light(color: 0x4488ff, intensity: 0.5, distance: 30) do
|
|
22
|
+
position -5, 3, -5
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
mesh :basic_sphere do
|
|
26
|
+
geometry :sphere, radius: 1
|
|
27
|
+
material :basic, color: 0xff6b6b
|
|
28
|
+
position -4.5, 0, 0
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
mesh :lambert_sphere do
|
|
32
|
+
geometry :sphere, radius: 1
|
|
33
|
+
material :lambert, color: 0x4ecdc4
|
|
34
|
+
position -1.5, 0, 0
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
mesh :phong_sphere do
|
|
38
|
+
geometry :sphere, radius: 1
|
|
39
|
+
material :phong, color: 0x45b7d1, shininess: 100
|
|
40
|
+
position 1.5, 0, 0
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
mesh :standard_sphere do
|
|
44
|
+
geometry :sphere, radius: 1
|
|
45
|
+
material :standard, color: 0xf9ca24, metalness: 0.3, roughness: 0.4
|
|
46
|
+
position 4.5, 0, 0
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
mesh :floor do
|
|
50
|
+
geometry :plane, width: 20, height: 20
|
|
51
|
+
material :standard, color: 0x2c3e50, roughness: 0.8
|
|
52
|
+
position 0, -1.5, 0
|
|
53
|
+
rotation -Math::PI / 2, 0, 0
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
on_frame do |delta|
|
|
57
|
+
[basic_sphere, lambert_sphere, phong_sphere, standard_sphere].each do |s|
|
|
58
|
+
s.rotation.y += 0.005
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# Light Types - Ambient, Directional, Point, and Spot lights
|
|
5
|
+
|
|
6
|
+
require_relative "../lib/3rb"
|
|
7
|
+
|
|
8
|
+
Sunrb.app do
|
|
9
|
+
background 0x0a0a0a
|
|
10
|
+
|
|
11
|
+
perspective_camera(fov: 60) do
|
|
12
|
+
position 0, 8, 15
|
|
13
|
+
look_at 0, 0, 0
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
ambient_light color: 0x111111
|
|
17
|
+
|
|
18
|
+
directional_light(color: 0xffffcc, intensity: 0.5) do
|
|
19
|
+
position 10, 20, 10
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
point_light(color: 0xff0000, intensity: 1.0, distance: 15, decay: 2) do
|
|
23
|
+
position -5, 3, 0
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
point_light(color: 0x00ff00, intensity: 1.0, distance: 15, decay: 2) do
|
|
27
|
+
position 5, 3, 0
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
spot_light(
|
|
31
|
+
color: 0x0088ff,
|
|
32
|
+
intensity: 2.0,
|
|
33
|
+
distance: 30,
|
|
34
|
+
angle: Math::PI / 6,
|
|
35
|
+
penumbra: 0.3
|
|
36
|
+
) do
|
|
37
|
+
position 0, 10, 0
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
mesh :floor do
|
|
41
|
+
geometry :plane, width: 30, height: 30
|
|
42
|
+
material :standard, color: 0x333333, roughness: 0.9
|
|
43
|
+
position 0, 0, 0
|
|
44
|
+
rotation -Math::PI / 2, 0, 0
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
5.times do |i|
|
|
48
|
+
5.times do |j|
|
|
49
|
+
mesh :"sphere_#{i}_#{j}" do
|
|
50
|
+
geometry :sphere, radius: 0.5
|
|
51
|
+
material :standard, color: 0xffffff, metalness: 0.1, roughness: 0.3
|
|
52
|
+
position (i - 2) * 3, 0.5, (j - 2) * 3
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
time = 0
|
|
58
|
+
on_frame do |delta|
|
|
59
|
+
time += delta
|
|
60
|
+
objects[:point_light_0]&.position&.x = Math.sin(time) * 5
|
|
61
|
+
objects[:point_light_1]&.position&.x = Math.cos(time) * 5
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# Keyframe Animation using AnimationClip and AnimationMixer
|
|
5
|
+
|
|
6
|
+
require_relative "../lib/3rb"
|
|
7
|
+
|
|
8
|
+
scene = Sunrb::Scene.new
|
|
9
|
+
scene.background = Sunrb::Color.new(0x1a1a2e)
|
|
10
|
+
|
|
11
|
+
camera = Sunrb::PerspectiveCamera.new(fov: 60, aspect: 800.0 / 600.0, near: 0.1, far: 1000)
|
|
12
|
+
camera.position.set(0, 3, 8)
|
|
13
|
+
camera.look_at(Sunrb::Vector3.new(0, 0, 0))
|
|
14
|
+
|
|
15
|
+
geometry = Sunrb::BoxGeometry.new(width: 1, height: 1, depth: 1)
|
|
16
|
+
material = Sunrb::MeshStandardMaterial.new(color: 0xff6b6b)
|
|
17
|
+
cube = Sunrb::Mesh.new(geometry, material)
|
|
18
|
+
scene.add(cube)
|
|
19
|
+
|
|
20
|
+
ambient = Sunrb::AmbientLight.new(color: 0x404040)
|
|
21
|
+
scene.add(ambient)
|
|
22
|
+
|
|
23
|
+
directional = Sunrb::DirectionalLight.new(color: 0xffffff, intensity: 1.0)
|
|
24
|
+
directional.position.set(5, 5, 5)
|
|
25
|
+
scene.add(directional)
|
|
26
|
+
|
|
27
|
+
position_track = Sunrb::VectorKeyframeTrack.new(
|
|
28
|
+
name: "position",
|
|
29
|
+
times: [0, 1, 2, 3, 4],
|
|
30
|
+
values: [
|
|
31
|
+
0, 0, 0,
|
|
32
|
+
2, 1, 0,
|
|
33
|
+
0, 2, 0,
|
|
34
|
+
-2, 1, 0,
|
|
35
|
+
0, 0, 0
|
|
36
|
+
]
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
rotation_track = Sunrb::VectorKeyframeTrack.new(
|
|
40
|
+
name: "rotation",
|
|
41
|
+
times: [0, 2, 4],
|
|
42
|
+
values: [
|
|
43
|
+
0, 0, 0,
|
|
44
|
+
0, Math::PI, 0,
|
|
45
|
+
0, Math::PI * 2, 0
|
|
46
|
+
]
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
scale_track = Sunrb::VectorKeyframeTrack.new(
|
|
50
|
+
name: "scale",
|
|
51
|
+
times: [0, 1, 2, 3, 4],
|
|
52
|
+
values: [
|
|
53
|
+
1, 1, 1,
|
|
54
|
+
1.5, 0.5, 1.5,
|
|
55
|
+
1, 1, 1,
|
|
56
|
+
0.5, 1.5, 0.5,
|
|
57
|
+
1, 1, 1
|
|
58
|
+
]
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
clip = Sunrb::AnimationClip.new(
|
|
62
|
+
name: "cube_animation",
|
|
63
|
+
tracks: [position_track, rotation_track, scale_track]
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
mixer = Sunrb::AnimationMixer.new(cube)
|
|
67
|
+
action = mixer.clip_action(clip)
|
|
68
|
+
action.play
|
|
69
|
+
|
|
70
|
+
puts "Animation clip duration: #{clip.duration}s"
|
|
71
|
+
puts "Tracks: #{clip.tracks.map(&:name).join(', ')}"
|
|
72
|
+
puts "Press ESC to exit"
|
|
73
|
+
|
|
74
|
+
renderer = Sunrb::OpenGLRenderer.new(width: 800, height: 600, title: "Animation Example")
|
|
75
|
+
|
|
76
|
+
renderer.run do |delta|
|
|
77
|
+
mixer.update(delta)
|
|
78
|
+
renderer.render(scene, camera)
|
|
79
|
+
end
|