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.
Files changed (100) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/3rb.gemspec +29 -0
  4. data/CHANGELOG.md +12 -0
  5. data/LICENSE +21 -0
  6. data/README.md +321 -0
  7. data/Rakefile +13 -0
  8. data/examples/01_hello_cube.rb +29 -0
  9. data/examples/02_basic_geometries.rb +56 -0
  10. data/examples/03_materials.rb +61 -0
  11. data/examples/04_lighting.rb +63 -0
  12. data/examples/05_animation.rb +79 -0
  13. data/examples/06_custom_shader.rb +92 -0
  14. data/examples/07_scene_graph.rb +74 -0
  15. data/examples/08_orbit_controls.rb +50 -0
  16. data/examples/09_3d_chart.rb +71 -0
  17. data/examples/10_procedural_terrain.rb +140 -0
  18. data/examples/11_particle_system.rb +68 -0
  19. data/examples/12_model_loader.rb +73 -0
  20. data/examples/13_game_prototype.rb +145 -0
  21. data/examples/14_utah_teapot.rb +291 -0
  22. data/examples/15_stanford_bunny.rb +200 -0
  23. data/examples/16_cornell_box.rb +373 -0
  24. data/examples/17_weird_fractal4.rb +130 -0
  25. data/examples/18_platonic_solids.rb +268 -0
  26. data/lib/3rb/animation/animation_clip.rb +287 -0
  27. data/lib/3rb/animation/animation_mixer.rb +366 -0
  28. data/lib/3rb/cameras/camera.rb +50 -0
  29. data/lib/3rb/cameras/orthographic_camera.rb +92 -0
  30. data/lib/3rb/cameras/perspective_camera.rb +103 -0
  31. data/lib/3rb/controls/orbit_controls.rb +341 -0
  32. data/lib/3rb/core/buffer_attribute.rb +172 -0
  33. data/lib/3rb/core/group.rb +9 -0
  34. data/lib/3rb/core/object3d.rb +298 -0
  35. data/lib/3rb/core/scene.rb +78 -0
  36. data/lib/3rb/dsl/helpers.rb +57 -0
  37. data/lib/3rb/dsl/scene_builder.rb +288 -0
  38. data/lib/3rb/ffi/glfw.rb +61 -0
  39. data/lib/3rb/ffi/opengl.rb +137 -0
  40. data/lib/3rb/ffi/platform.rb +65 -0
  41. data/lib/3rb/geometries/box_geometry.rb +101 -0
  42. data/lib/3rb/geometries/buffer_geometry.rb +345 -0
  43. data/lib/3rb/geometries/cone_geometry.rb +29 -0
  44. data/lib/3rb/geometries/cylinder_geometry.rb +149 -0
  45. data/lib/3rb/geometries/plane_geometry.rb +75 -0
  46. data/lib/3rb/geometries/sphere_geometry.rb +93 -0
  47. data/lib/3rb/geometries/torus_geometry.rb +77 -0
  48. data/lib/3rb/lights/ambient_light.rb +9 -0
  49. data/lib/3rb/lights/directional_light.rb +57 -0
  50. data/lib/3rb/lights/hemisphere_light.rb +26 -0
  51. data/lib/3rb/lights/light.rb +27 -0
  52. data/lib/3rb/lights/point_light.rb +68 -0
  53. data/lib/3rb/lights/rect_area_light.rb +35 -0
  54. data/lib/3rb/lights/spot_light.rb +88 -0
  55. data/lib/3rb/loaders/gltf_loader.rb +304 -0
  56. data/lib/3rb/loaders/loader.rb +94 -0
  57. data/lib/3rb/loaders/obj_loader.rb +186 -0
  58. data/lib/3rb/loaders/texture_loader.rb +55 -0
  59. data/lib/3rb/materials/basic_material.rb +70 -0
  60. data/lib/3rb/materials/lambert_material.rb +102 -0
  61. data/lib/3rb/materials/material.rb +114 -0
  62. data/lib/3rb/materials/phong_material.rb +106 -0
  63. data/lib/3rb/materials/shader_material.rb +104 -0
  64. data/lib/3rb/materials/standard_material.rb +106 -0
  65. data/lib/3rb/math/color.rb +246 -0
  66. data/lib/3rb/math/euler.rb +156 -0
  67. data/lib/3rb/math/math_utils.rb +132 -0
  68. data/lib/3rb/math/matrix3.rb +269 -0
  69. data/lib/3rb/math/matrix4.rb +501 -0
  70. data/lib/3rb/math/quaternion.rb +337 -0
  71. data/lib/3rb/math/vector2.rb +216 -0
  72. data/lib/3rb/math/vector3.rb +366 -0
  73. data/lib/3rb/math/vector4.rb +233 -0
  74. data/lib/3rb/native/gl.rb +382 -0
  75. data/lib/3rb/native/native.rb +55 -0
  76. data/lib/3rb/native/window.rb +111 -0
  77. data/lib/3rb/native.rb +9 -0
  78. data/lib/3rb/objects/line.rb +116 -0
  79. data/lib/3rb/objects/mesh.rb +40 -0
  80. data/lib/3rb/objects/points.rb +71 -0
  81. data/lib/3rb/renderers/opengl_renderer.rb +567 -0
  82. data/lib/3rb/renderers/renderer.rb +60 -0
  83. data/lib/3rb/renderers/shader_lib.rb +100 -0
  84. data/lib/3rb/textures/cube_texture.rb +26 -0
  85. data/lib/3rb/textures/data_texture.rb +35 -0
  86. data/lib/3rb/textures/render_target.rb +125 -0
  87. data/lib/3rb/textures/texture.rb +190 -0
  88. data/lib/3rb/version.rb +5 -0
  89. data/lib/3rb.rb +86 -0
  90. data/shaders/basic.frag +19 -0
  91. data/shaders/basic.vert +15 -0
  92. data/shaders/common/lights.glsl +53 -0
  93. data/shaders/common/uniforms.glsl +9 -0
  94. data/shaders/lambert.frag +37 -0
  95. data/shaders/lambert.vert +22 -0
  96. data/shaders/phong.frag +51 -0
  97. data/shaders/phong.vert +28 -0
  98. data/shaders/standard.frag +92 -0
  99. data/shaders/standard.vert +28 -0
  100. metadata +155 -0
@@ -0,0 +1,22 @@
1
+ #version 330 core
2
+
3
+ layout (location = 0) in vec3 position;
4
+ layout (location = 1) in vec3 normal;
5
+ layout (location = 2) in vec2 uv;
6
+
7
+ uniform mat4 modelMatrix;
8
+ uniform mat4 modelViewMatrix;
9
+ uniform mat4 projectionMatrix;
10
+ uniform mat3 normalMatrix;
11
+
12
+ out vec3 vNormal;
13
+ out vec3 vWorldPosition;
14
+ out vec2 vUv;
15
+
16
+ void main() {
17
+ vUv = uv;
18
+ vNormal = normalize(normalMatrix * normal);
19
+ vec4 worldPosition = modelMatrix * vec4(position, 1.0);
20
+ vWorldPosition = worldPosition.xyz;
21
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
22
+ }
@@ -0,0 +1,51 @@
1
+ #version 330 core
2
+
3
+ in vec3 vNormal;
4
+ in vec3 vWorldPosition;
5
+ in vec3 vViewPosition;
6
+ in vec2 vUv;
7
+
8
+ out vec4 FragColor;
9
+
10
+ uniform vec3 diffuse;
11
+ uniform vec3 emissive;
12
+ uniform vec3 specular;
13
+ uniform float shininess;
14
+ uniform float opacity;
15
+ uniform bool useMap;
16
+ uniform sampler2D map;
17
+
18
+ uniform vec3 cameraPosition;
19
+ uniform vec3 ambientLightColor;
20
+ uniform vec3 directionalLightDirection;
21
+ uniform vec3 directionalLightColor;
22
+
23
+ void main() {
24
+ vec3 normal = normalize(vNormal);
25
+ vec3 viewDir = normalize(cameraPosition - vWorldPosition);
26
+
27
+ vec4 diffuseColor = vec4(diffuse, opacity);
28
+ if (useMap) {
29
+ diffuseColor *= texture(map, vUv);
30
+ }
31
+
32
+ // Ambient
33
+ vec3 lighting = ambientLightColor * diffuseColor.rgb;
34
+
35
+ // Directional light
36
+ vec3 lightDir = normalize(directionalLightDirection);
37
+ float dotNL = max(dot(normal, lightDir), 0.0);
38
+
39
+ // Diffuse
40
+ lighting += directionalLightColor * diffuseColor.rgb * dotNL;
41
+
42
+ // Specular (Blinn-Phong)
43
+ vec3 halfDir = normalize(lightDir + viewDir);
44
+ float dotNH = max(dot(normal, halfDir), 0.0);
45
+ float specularStrength = pow(dotNH, shininess);
46
+ lighting += directionalLightColor * specular * specularStrength;
47
+
48
+ vec3 outgoingLight = lighting + emissive;
49
+
50
+ FragColor = vec4(outgoingLight, diffuseColor.a);
51
+ }
@@ -0,0 +1,28 @@
1
+ #version 330 core
2
+
3
+ layout (location = 0) in vec3 position;
4
+ layout (location = 1) in vec3 normal;
5
+ layout (location = 2) in vec2 uv;
6
+
7
+ uniform mat4 modelMatrix;
8
+ uniform mat4 modelViewMatrix;
9
+ uniform mat4 projectionMatrix;
10
+ uniform mat3 normalMatrix;
11
+
12
+ out vec3 vNormal;
13
+ out vec3 vWorldPosition;
14
+ out vec3 vViewPosition;
15
+ out vec2 vUv;
16
+
17
+ void main() {
18
+ vUv = uv;
19
+ vNormal = normalize(normalMatrix * normal);
20
+
21
+ vec4 worldPosition = modelMatrix * vec4(position, 1.0);
22
+ vWorldPosition = worldPosition.xyz;
23
+
24
+ vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
25
+ vViewPosition = -mvPosition.xyz;
26
+
27
+ gl_Position = projectionMatrix * mvPosition;
28
+ }
@@ -0,0 +1,92 @@
1
+ #version 330 core
2
+
3
+ in vec3 vNormal;
4
+ in vec3 vWorldPosition;
5
+ in vec3 vViewPosition;
6
+ in vec2 vUv;
7
+
8
+ out vec4 FragColor;
9
+
10
+ uniform vec3 diffuse;
11
+ uniform vec3 emissive;
12
+ uniform float roughness;
13
+ uniform float metalness;
14
+ uniform float opacity;
15
+ uniform bool useMap;
16
+ uniform sampler2D map;
17
+
18
+ uniform vec3 cameraPosition;
19
+ uniform vec3 ambientLightColor;
20
+ uniform vec3 directionalLightDirection;
21
+ uniform vec3 directionalLightColor;
22
+
23
+ const float PI = 3.14159265359;
24
+
25
+ // GGX/Trowbridge-Reitz normal distribution function
26
+ float D_GGX(float NoH, float roughness) {
27
+ float a = roughness * roughness;
28
+ float a2 = a * a;
29
+ float NoH2 = NoH * NoH;
30
+ float denom = NoH2 * (a2 - 1.0) + 1.0;
31
+ return a2 / (PI * denom * denom);
32
+ }
33
+
34
+ // Schlick-GGX geometry function
35
+ float G_SchlickGGX(float NoV, float roughness) {
36
+ float r = roughness + 1.0;
37
+ float k = (r * r) / 8.0;
38
+ return NoV / (NoV * (1.0 - k) + k);
39
+ }
40
+
41
+ float G_Smith(float NoV, float NoL, float roughness) {
42
+ return G_SchlickGGX(NoV, roughness) * G_SchlickGGX(NoL, roughness);
43
+ }
44
+
45
+ // Fresnel-Schlick approximation
46
+ vec3 F_Schlick(float cosTheta, vec3 F0) {
47
+ return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
48
+ }
49
+
50
+ void main() {
51
+ vec3 normal = normalize(vNormal);
52
+ vec3 viewDir = normalize(cameraPosition - vWorldPosition);
53
+
54
+ vec4 diffuseColor = vec4(diffuse, opacity);
55
+ if (useMap) {
56
+ diffuseColor *= texture(map, vUv);
57
+ }
58
+
59
+ vec3 albedo = diffuseColor.rgb;
60
+
61
+ // Calculate F0 (reflectance at normal incidence)
62
+ vec3 F0 = mix(vec3(0.04), albedo, metalness);
63
+
64
+ // Ambient
65
+ vec3 ambient = ambientLightColor * albedo * (1.0 - metalness);
66
+
67
+ // Directional light
68
+ vec3 lightDir = normalize(directionalLightDirection);
69
+ vec3 halfDir = normalize(lightDir + viewDir);
70
+
71
+ float NoV = max(dot(normal, viewDir), 0.001);
72
+ float NoL = max(dot(normal, lightDir), 0.0);
73
+ float NoH = max(dot(normal, halfDir), 0.0);
74
+ float VoH = max(dot(viewDir, halfDir), 0.0);
75
+
76
+ // Cook-Torrance BRDF
77
+ float D = D_GGX(NoH, roughness);
78
+ float G = G_Smith(NoV, NoL, roughness);
79
+ vec3 F = F_Schlick(VoH, F0);
80
+
81
+ vec3 specularBRDF = (D * G * F) / (4.0 * NoV * NoL + 0.001);
82
+
83
+ // Diffuse BRDF (Lambertian)
84
+ vec3 kD = (1.0 - F) * (1.0 - metalness);
85
+ vec3 diffuseBRDF = kD * albedo / PI;
86
+
87
+ vec3 lighting = ambient + (diffuseBRDF + specularBRDF) * directionalLightColor * NoL;
88
+
89
+ vec3 outgoingLight = lighting + emissive;
90
+
91
+ FragColor = vec4(outgoingLight, diffuseColor.a);
92
+ }
@@ -0,0 +1,28 @@
1
+ #version 330 core
2
+
3
+ layout (location = 0) in vec3 position;
4
+ layout (location = 1) in vec3 normal;
5
+ layout (location = 2) in vec2 uv;
6
+
7
+ uniform mat4 modelMatrix;
8
+ uniform mat4 modelViewMatrix;
9
+ uniform mat4 projectionMatrix;
10
+ uniform mat3 normalMatrix;
11
+
12
+ out vec3 vNormal;
13
+ out vec3 vWorldPosition;
14
+ out vec3 vViewPosition;
15
+ out vec2 vUv;
16
+
17
+ void main() {
18
+ vUv = uv;
19
+ vNormal = normalize(normalMatrix * normal);
20
+
21
+ vec4 worldPosition = modelMatrix * vec4(position, 1.0);
22
+ vWorldPosition = worldPosition.xyz;
23
+
24
+ vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
25
+ vViewPosition = -mvPosition.xyz;
26
+
27
+ gl_Position = projectionMatrix * mvPosition;
28
+ }
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: 3rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yudai Takada
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: ffi
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.15'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.15'
26
+ description: A powerful, intuitive 3D graphics library for Ruby with a Three.js-like
27
+ API and Ruby DSL
28
+ email:
29
+ - t.yudai92@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - 3rb.gemspec
36
+ - CHANGELOG.md
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - examples/01_hello_cube.rb
41
+ - examples/02_basic_geometries.rb
42
+ - examples/03_materials.rb
43
+ - examples/04_lighting.rb
44
+ - examples/05_animation.rb
45
+ - examples/06_custom_shader.rb
46
+ - examples/07_scene_graph.rb
47
+ - examples/08_orbit_controls.rb
48
+ - examples/09_3d_chart.rb
49
+ - examples/10_procedural_terrain.rb
50
+ - examples/11_particle_system.rb
51
+ - examples/12_model_loader.rb
52
+ - examples/13_game_prototype.rb
53
+ - examples/14_utah_teapot.rb
54
+ - examples/15_stanford_bunny.rb
55
+ - examples/16_cornell_box.rb
56
+ - examples/17_weird_fractal4.rb
57
+ - examples/18_platonic_solids.rb
58
+ - lib/3rb.rb
59
+ - lib/3rb/animation/animation_clip.rb
60
+ - lib/3rb/animation/animation_mixer.rb
61
+ - lib/3rb/cameras/camera.rb
62
+ - lib/3rb/cameras/orthographic_camera.rb
63
+ - lib/3rb/cameras/perspective_camera.rb
64
+ - lib/3rb/controls/orbit_controls.rb
65
+ - lib/3rb/core/buffer_attribute.rb
66
+ - lib/3rb/core/group.rb
67
+ - lib/3rb/core/object3d.rb
68
+ - lib/3rb/core/scene.rb
69
+ - lib/3rb/dsl/helpers.rb
70
+ - lib/3rb/dsl/scene_builder.rb
71
+ - lib/3rb/ffi/glfw.rb
72
+ - lib/3rb/ffi/opengl.rb
73
+ - lib/3rb/ffi/platform.rb
74
+ - lib/3rb/geometries/box_geometry.rb
75
+ - lib/3rb/geometries/buffer_geometry.rb
76
+ - lib/3rb/geometries/cone_geometry.rb
77
+ - lib/3rb/geometries/cylinder_geometry.rb
78
+ - lib/3rb/geometries/plane_geometry.rb
79
+ - lib/3rb/geometries/sphere_geometry.rb
80
+ - lib/3rb/geometries/torus_geometry.rb
81
+ - lib/3rb/lights/ambient_light.rb
82
+ - lib/3rb/lights/directional_light.rb
83
+ - lib/3rb/lights/hemisphere_light.rb
84
+ - lib/3rb/lights/light.rb
85
+ - lib/3rb/lights/point_light.rb
86
+ - lib/3rb/lights/rect_area_light.rb
87
+ - lib/3rb/lights/spot_light.rb
88
+ - lib/3rb/loaders/gltf_loader.rb
89
+ - lib/3rb/loaders/loader.rb
90
+ - lib/3rb/loaders/obj_loader.rb
91
+ - lib/3rb/loaders/texture_loader.rb
92
+ - lib/3rb/materials/basic_material.rb
93
+ - lib/3rb/materials/lambert_material.rb
94
+ - lib/3rb/materials/material.rb
95
+ - lib/3rb/materials/phong_material.rb
96
+ - lib/3rb/materials/shader_material.rb
97
+ - lib/3rb/materials/standard_material.rb
98
+ - lib/3rb/math/color.rb
99
+ - lib/3rb/math/euler.rb
100
+ - lib/3rb/math/math_utils.rb
101
+ - lib/3rb/math/matrix3.rb
102
+ - lib/3rb/math/matrix4.rb
103
+ - lib/3rb/math/quaternion.rb
104
+ - lib/3rb/math/vector2.rb
105
+ - lib/3rb/math/vector3.rb
106
+ - lib/3rb/math/vector4.rb
107
+ - lib/3rb/native.rb
108
+ - lib/3rb/native/gl.rb
109
+ - lib/3rb/native/native.rb
110
+ - lib/3rb/native/window.rb
111
+ - lib/3rb/objects/line.rb
112
+ - lib/3rb/objects/mesh.rb
113
+ - lib/3rb/objects/points.rb
114
+ - lib/3rb/renderers/opengl_renderer.rb
115
+ - lib/3rb/renderers/renderer.rb
116
+ - lib/3rb/renderers/shader_lib.rb
117
+ - lib/3rb/textures/cube_texture.rb
118
+ - lib/3rb/textures/data_texture.rb
119
+ - lib/3rb/textures/render_target.rb
120
+ - lib/3rb/textures/texture.rb
121
+ - lib/3rb/version.rb
122
+ - shaders/basic.frag
123
+ - shaders/basic.vert
124
+ - shaders/common/lights.glsl
125
+ - shaders/common/uniforms.glsl
126
+ - shaders/lambert.frag
127
+ - shaders/lambert.vert
128
+ - shaders/phong.frag
129
+ - shaders/phong.vert
130
+ - shaders/standard.frag
131
+ - shaders/standard.vert
132
+ homepage: https://github.com/ydah/3rb
133
+ licenses:
134
+ - MIT
135
+ metadata:
136
+ source_code_uri: https://github.com/ydah/3rb
137
+ changelog_uri: https://github.com/ydah/3rb/blob/main/CHANGELOG.md
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: 3.0.0
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubygems_version: 4.0.3
153
+ specification_version: 4
154
+ summary: 3D graphics library for Ruby inspired by Three.js
155
+ test_files: []