ruby_rpg 0.0.4 → 0.0.5
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 +4 -4
- data/README.md +21 -21
- data/bin/build.bash +13 -13
- data/bin/import +44 -44
- data/glfw-3.3.9.bin.MACOS/LICENSE.md +23 -23
- data/glfw-3.3.9.bin.MACOS/README.md +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/build_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/build_guide.html +199 -199
- data/glfw-3.3.9.bin.MACOS/docs/html/compat_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/compat_guide.html +153 -153
- data/glfw-3.3.9.bin.MACOS/docs/html/compile_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/compile_guide.html +223 -223
- data/glfw-3.3.9.bin.MACOS/docs/html/context_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/context_guide.html +259 -259
- data/glfw-3.3.9.bin.MACOS/docs/html/deprecated.html +88 -88
- data/glfw-3.3.9.bin.MACOS/docs/html/dir_08423ce7729c4554356a7de7171ba263.html +85 -85
- data/glfw-3.3.9.bin.MACOS/docs/html/dir_2dcebcdf14b42e92a6ed85c61bda6b9d.html +93 -93
- data/glfw-3.3.9.bin.MACOS/docs/html/dir_62ce056722ee97c199075aa1d6eca309.html +95 -95
- data/glfw-3.3.9.bin.MACOS/docs/html/dir_dddf4cf62655095a666cf715de3a31a2.html +91 -91
- data/glfw-3.3.9.bin.MACOS/docs/html/doc.svg +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/docd.svg +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/doxygen.css +1685 -1685
- data/glfw-3.3.9.bin.MACOS/docs/html/doxygen.svg +28 -28
- data/glfw-3.3.9.bin.MACOS/docs/html/dynsections.js +192 -192
- data/glfw-3.3.9.bin.MACOS/docs/html/extra.css +1 -1
- data/glfw-3.3.9.bin.MACOS/docs/html/files.html +91 -91
- data/glfw-3.3.9.bin.MACOS/docs/html/folderclosed.svg +11 -11
- data/glfw-3.3.9.bin.MACOS/docs/html/folderclosedd.svg +11 -11
- data/glfw-3.3.9.bin.MACOS/docs/html/folderopen.svg +17 -17
- data/glfw-3.3.9.bin.MACOS/docs/html/folderopend.svg +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/glfw3_8h.html +1661 -1661
- data/glfw-3.3.9.bin.MACOS/docs/html/glfw3_8h_source.html +1175 -1175
- data/glfw-3.3.9.bin.MACOS/docs/html/glfw3native_8h.html +167 -167
- data/glfw-3.3.9.bin.MACOS/docs/html/glfw3native_8h_source.html +303 -303
- data/glfw-3.3.9.bin.MACOS/docs/html/group__buttons.html +282 -282
- data/glfw-3.3.9.bin.MACOS/docs/html/group__context.html +304 -304
- data/glfw-3.3.9.bin.MACOS/docs/html/group__errors.html +304 -304
- data/glfw-3.3.9.bin.MACOS/docs/html/group__gamepad__axes.html +202 -202
- data/glfw-3.3.9.bin.MACOS/docs/html/group__gamepad__buttons.html +410 -410
- data/glfw-3.3.9.bin.MACOS/docs/html/group__hat__state.html +234 -234
- data/glfw-3.3.9.bin.MACOS/docs/html/group__init.html +570 -570
- data/glfw-3.3.9.bin.MACOS/docs/html/group__input.html +2255 -2255
- data/glfw-3.3.9.bin.MACOS/docs/html/group__joysticks.html +362 -362
- data/glfw-3.3.9.bin.MACOS/docs/html/group__keys.html +2034 -2034
- data/glfw-3.3.9.bin.MACOS/docs/html/group__mods.html +198 -198
- data/glfw-3.3.9.bin.MACOS/docs/html/group__monitor.html +848 -848
- data/glfw-3.3.9.bin.MACOS/docs/html/group__native.html +812 -812
- data/glfw-3.3.9.bin.MACOS/docs/html/group__shapes.html +198 -198
- data/glfw-3.3.9.bin.MACOS/docs/html/group__vulkan.html +359 -359
- data/glfw-3.3.9.bin.MACOS/docs/html/group__window.html +3434 -3434
- data/glfw-3.3.9.bin.MACOS/docs/html/index.html +100 -100
- data/glfw-3.3.9.bin.MACOS/docs/html/input_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/input_guide.html +570 -570
- data/glfw-3.3.9.bin.MACOS/docs/html/internal_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/internals_guide.html +132 -132
- data/glfw-3.3.9.bin.MACOS/docs/html/intro_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/intro_guide.html +351 -351
- data/glfw-3.3.9.bin.MACOS/docs/html/jquery.js +33 -33
- data/glfw-3.3.9.bin.MACOS/docs/html/main_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/menu.js +136 -136
- data/glfw-3.3.9.bin.MACOS/docs/html/menudata.js +29 -29
- data/glfw-3.3.9.bin.MACOS/docs/html/minus.svg +8 -8
- data/glfw-3.3.9.bin.MACOS/docs/html/minusd.svg +8 -8
- data/glfw-3.3.9.bin.MACOS/docs/html/monitor_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/monitor_guide.html +229 -229
- data/glfw-3.3.9.bin.MACOS/docs/html/moving_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/moving_guide.html +374 -374
- data/glfw-3.3.9.bin.MACOS/docs/html/news.html +634 -634
- data/glfw-3.3.9.bin.MACOS/docs/html/news_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/pages.html +99 -99
- data/glfw-3.3.9.bin.MACOS/docs/html/plus.svg +9 -9
- data/glfw-3.3.9.bin.MACOS/docs/html/plusd.svg +9 -9
- data/glfw-3.3.9.bin.MACOS/docs/html/quick_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/quick_guide.html +397 -397
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_0.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_1.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_10.js +65 -65
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_11.js +26 -26
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_12.js +44 -44
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_13.js +34 -34
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_14.js +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_15.js +52 -52
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_16.js +69 -69
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_17.js +51 -51
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_18.js +14 -14
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_19.js +36 -36
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_1a.js +98 -98
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_1b.js +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_2.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_3.js +9 -9
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_4.js +57 -57
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_5.js +22 -22
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_6.js +103 -103
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_7.js +23 -23
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_8.js +35 -35
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_9.js +63 -63
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_a.js +505 -505
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_b.js +24 -24
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_c.js +50 -50
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_d.js +17 -17
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_e.js +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_f.js +35 -35
- data/glfw-3.3.9.bin.MACOS/docs/html/search/classes_0.js +7 -7
- data/glfw-3.3.9.bin.MACOS/docs/html/search/close.svg +18 -18
- data/glfw-3.3.9.bin.MACOS/docs/html/search/defines_0.js +34 -34
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_0.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_1.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_2.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_3.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_4.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_5.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_6.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_7.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_8.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/functions_0.js +146 -146
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_0.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_1.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_10.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_2.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_3.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_4.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_5.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_6.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_7.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_8.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_9.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_a.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_b.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_c.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_d.js +7 -7
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_e.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_f.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/mag.svg +24 -24
- data/glfw-3.3.9.bin.MACOS/docs/html/search/mag_d.svg +24 -24
- data/glfw-3.3.9.bin.MACOS/docs/html/search/mag_sel.svg +31 -31
- data/glfw-3.3.9.bin.MACOS/docs/html/search/mag_seld.svg +31 -31
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_0.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_1.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_10.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_2.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_3.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_4.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_5.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_6.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_7.js +7 -7
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_8.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_9.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_a.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_b.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_c.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_d.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_e.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_f.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/search.css +291 -291
- data/glfw-3.3.9.bin.MACOS/docs/html/search/search.js +840 -840
- data/glfw-3.3.9.bin.MACOS/docs/html/search/searchdata.js +39 -39
- data/glfw-3.3.9.bin.MACOS/docs/html/search/typedefs_0.js +32 -32
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_0.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_1.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_2.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_3.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_4.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_5.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_6.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_7.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/spaces.svg +877 -877
- data/glfw-3.3.9.bin.MACOS/docs/html/struct_g_l_f_wgamepadstate.html +134 -134
- data/glfw-3.3.9.bin.MACOS/docs/html/struct_g_l_f_wgammaramp.html +170 -170
- data/glfw-3.3.9.bin.MACOS/docs/html/struct_g_l_f_wimage.html +151 -151
- data/glfw-3.3.9.bin.MACOS/docs/html/struct_g_l_f_wvidmode.html +204 -204
- data/glfw-3.3.9.bin.MACOS/docs/html/vulkan_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/vulkan_guide.html +196 -196
- data/glfw-3.3.9.bin.MACOS/docs/html/window_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/window_guide.html +763 -763
- data/glfw-3.3.9.bin.MACOS/include/GLFW/glfw3.h +5932 -5932
- data/glfw-3.3.9.bin.MACOS/include/GLFW/glfw3native.h +634 -634
- data/glfw-3.3.9.bin.MACOS/lib-arm64/libglfw.3.dylib +0 -0
- data/glfw-3.3.9.bin.MACOS/lib-universal/libglfw.3.dylib +0 -0
- data/glfw-3.3.9.bin.MACOS/lib-x86_64/libglfw.3.dylib +0 -0
- data/lib/engine/autoloader.rb +11 -11
- data/lib/engine/camera.rb +12 -12
- data/lib/engine/component.rb +63 -63
- data/lib/engine/components/audio_source.rb +41 -41
- data/lib/engine/components/direction_light.rb +22 -22
- data/lib/engine/components/font_renderer.rb +134 -134
- data/lib/engine/components/mesh_renderer.rb +31 -31
- data/lib/engine/components/orthographic_camera.rb +54 -54
- data/lib/engine/components/perspective_camera.rb +53 -53
- data/lib/engine/components/point_light.rb +23 -23
- data/lib/engine/components/sprite_renderer.rb +141 -141
- data/lib/engine/components/ui_font_renderer.rb +140 -140
- data/lib/engine/components/ui_sprite_clickbox.rb +62 -0
- data/lib/engine/components/ui_sprite_renderer.rb +101 -101
- data/lib/engine/compute_shader.rb +93 -0
- data/lib/engine/cursor.rb +43 -43
- data/lib/engine/debugging.rb +41 -41
- data/lib/engine/engine.rb +131 -131
- data/lib/engine/font.rb +74 -74
- data/lib/engine/game_object.rb +234 -234
- data/lib/engine/importers/font_importer.rb +69 -69
- data/lib/engine/importers/obj_file.rb +244 -244
- data/lib/engine/importers/obj_importer.rb +33 -33
- data/lib/engine/input.rb +105 -104
- data/lib/engine/material.rb +79 -79
- data/lib/engine/mesh.rb +43 -43
- data/lib/engine/path.rb +92 -92
- data/lib/engine/physics/collision.rb +12 -12
- data/lib/engine/physics/components/cube_collider.rb +6 -6
- data/lib/engine/physics/components/rigidbody.rb +103 -103
- data/lib/engine/physics/components/sphere_collider.rb +93 -93
- data/lib/engine/physics/physics_resolver.rb +37 -37
- data/lib/engine/polygon_mesh.rb +45 -45
- data/lib/engine/quaternion.rb +234 -234
- data/lib/engine/rendering/instance_renderer.rb +153 -153
- data/lib/engine/rendering/render_pipeline.rb +35 -35
- data/lib/engine/screenshoter.rb +34 -34
- data/lib/engine/shader.rb +119 -91
- data/lib/engine/shaders/colour_frag.glsl +8 -8
- data/lib/engine/shaders/colour_vertex.glsl +13 -13
- data/lib/engine/shaders/mesh_frag.glsl +102 -102
- data/lib/engine/shaders/mesh_vertex.glsl +25 -25
- data/lib/engine/shaders/skybox_frag.glsl +12 -12
- data/lib/engine/shaders/skybox_vert.glsl +14 -14
- data/lib/engine/shaders/sprite_frag.glsl +17 -17
- data/lib/engine/shaders/sprite_vertex.glsl +15 -15
- data/lib/engine/shaders/text_frag.glsl +15 -15
- data/lib/engine/shaders/text_vertex.glsl +31 -31
- data/lib/engine/shaders/ui_sprite_frag.glsl +15 -15
- data/lib/engine/shaders/ui_sprite_vertex.glsl +15 -15
- data/lib/engine/shaders/vertex_lit_frag.glsl +89 -89
- data/lib/engine/shaders/vertex_lit_vertex.glsl +28 -28
- data/lib/engine/tangent_calculator.rb +42 -42
- data/lib/engine/texture.rb +53 -53
- data/lib/engine/video_mode.rb +37 -37
- data/lib/engine/window.rb +126 -126
- data/lib/ruby_rpg.rb +58 -56
- metadata +5 -6
@@ -1,244 +1,244 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Engine
|
4
|
-
class ObjFile
|
5
|
-
def initialize(file_path)
|
6
|
-
@mesh_data = File.readlines(file_path + ".obj")
|
7
|
-
if File.exist?(file_path + ".mtl")
|
8
|
-
@mtl_data = File.readlines(file_path + ".mtl").map(&:chomp)
|
9
|
-
else
|
10
|
-
@mtl_data = []
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def vertices
|
15
|
-
@vertices ||=
|
16
|
-
@mesh_data.select { |line| line.start_with?("v ") }.map do |line|
|
17
|
-
_, x, y, z = line.split(" ")
|
18
|
-
Vector[x.to_f, y.to_f, z.to_f]
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def normals
|
23
|
-
@normals ||=
|
24
|
-
@mesh_data.select { |line| line.start_with?("vn ") }.map do |line|
|
25
|
-
_, x, y, z = line.split(" ")
|
26
|
-
Vector[x.to_f, y.to_f, z.to_f]
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def tangents
|
31
|
-
@tangents ||=
|
32
|
-
(0...vertex_indices.length).each_slice(3).map do |i1, i2, i3|
|
33
|
-
vertex1 = vertices[vertex_indices[i1]]
|
34
|
-
normal1 = normals[normal_indices[i1]]
|
35
|
-
texture_coord1 = texture_coords[texture_indices[i1]]
|
36
|
-
|
37
|
-
vertex2 = vertices[vertex_indices[i2]]
|
38
|
-
normal2 = normals[normal_indices[i2]]
|
39
|
-
texture_coord2 = texture_coords[texture_indices[i2]]
|
40
|
-
|
41
|
-
vertex3 = vertices[vertex_indices[i3]]
|
42
|
-
normal3 = normals[normal_indices[i3]]
|
43
|
-
texture_coord3 = texture_coords[texture_indices[i3]]
|
44
|
-
|
45
|
-
triangle_vertices = [vertex1, vertex2, vertex3]
|
46
|
-
triangle_normals = [normal1, normal2, normal3]
|
47
|
-
triangle_texture_coords = [texture_coord1, texture_coord2, texture_coord3]
|
48
|
-
|
49
|
-
Engine::TangentCalculator.new(triangle_vertices, triangle_normals, triangle_texture_coords).calculate_tangents
|
50
|
-
end.flatten(1)
|
51
|
-
end
|
52
|
-
|
53
|
-
def texture_coords
|
54
|
-
@texture_coords ||=
|
55
|
-
@mesh_data.select { |line| line.start_with?("vt ") }.map do |line|
|
56
|
-
_, x, y = line.split(" ")
|
57
|
-
Vector[x.to_f, y.to_f]
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def face_lines
|
62
|
-
@face_lines ||=
|
63
|
-
begin
|
64
|
-
split_faces(@mesh_data).select { |line| line.start_with?("f ") }
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def split_faces(lines)
|
69
|
-
lines.map.with_index do |line, line_number|
|
70
|
-
if line.start_with?("f ")
|
71
|
-
new_faces = split_face(line)
|
72
|
-
(new_faces.length * 3).times do
|
73
|
-
material_names << material_for_line(line_number)
|
74
|
-
end
|
75
|
-
new_faces
|
76
|
-
else
|
77
|
-
line
|
78
|
-
end
|
79
|
-
end.flatten
|
80
|
-
end
|
81
|
-
|
82
|
-
def split_face(face)
|
83
|
-
face_vertices = face.split(" ")[1..-1]
|
84
|
-
return [face] if face_vertices.length == 3
|
85
|
-
|
86
|
-
world_points = face_vertices.map { |v| v.split("/")[0] }
|
87
|
-
.map { |i| vertices[i.to_i - 1] }
|
88
|
-
plane = plane_matrix(world_points)
|
89
|
-
flat_points = world_points.map { |point| plane * point }
|
90
|
-
packed_points = face_vertices.map.with_index do |v, i|
|
91
|
-
{ point_string: face_vertices[i], 0 => flat_points[i][0], 1 => flat_points[i][1] }
|
92
|
-
end
|
93
|
-
|
94
|
-
path = Path.new(packed_points.reverse)
|
95
|
-
begin
|
96
|
-
decompose_path(path, true)
|
97
|
-
rescue NoEarsException
|
98
|
-
decompose_path(Path.new(packed_points), false)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
def decompose_path(path, reverse)
|
103
|
-
decomposed_vertices = []
|
104
|
-
depth = 100
|
105
|
-
until path.length == 3 || depth == 0
|
106
|
-
depth -= 1
|
107
|
-
ear_result = path.find_ear
|
108
|
-
ear = ear_result.first
|
109
|
-
new_path = ear_result.last
|
110
|
-
ear = ear.reverse! if reverse
|
111
|
-
ear_area = triangle_area(ear[0], ear[1], ear[2])
|
112
|
-
decomposed_vertices << ear unless ear_area < 0.0001
|
113
|
-
path = new_path
|
114
|
-
end
|
115
|
-
if reverse
|
116
|
-
decomposed_vertices << path.points.reverse
|
117
|
-
else
|
118
|
-
decomposed_vertices << path.points
|
119
|
-
end
|
120
|
-
decomposed_vertices.map do |triangle|
|
121
|
-
"f #{triangle.map { |v| v[:point_string] }.join(" ")}"
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
def triangle_area(a, b, c)
|
126
|
-
(a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1])).abs / 2.0
|
127
|
-
end
|
128
|
-
|
129
|
-
def plane_matrix(points)
|
130
|
-
raise "Not enough points to create a plane" if points.length < 3
|
131
|
-
|
132
|
-
origin_point = points[0]
|
133
|
-
a = points[1] - origin_point
|
134
|
-
b = nil
|
135
|
-
points[2..-1].each do |point|
|
136
|
-
diff = point - origin_point
|
137
|
-
b = diff if a.cross(diff).magnitude > 0.0001
|
138
|
-
end
|
139
|
-
raise "Points are collinear" if b.nil?
|
140
|
-
|
141
|
-
normal = a.cross(b).normalize
|
142
|
-
|
143
|
-
Matrix[
|
144
|
-
[a[0], a[1], a[2]],
|
145
|
-
[b[0], b[1], b[2]],
|
146
|
-
[normal[0], normal[1], normal[2]]
|
147
|
-
].transpose.inverse
|
148
|
-
end
|
149
|
-
|
150
|
-
def vertex_indices
|
151
|
-
@vertex_indices ||=
|
152
|
-
face_lines.map do |line|
|
153
|
-
_, v1, v2, v3 = line.split(" ")
|
154
|
-
v1, v2, v3 = [v1, v2, v3].map { |v| v.split("/").first.to_i }
|
155
|
-
[v1 - 1, v2 - 1, v3 - 1]
|
156
|
-
end.flatten
|
157
|
-
end
|
158
|
-
|
159
|
-
def texture_indices
|
160
|
-
@texture_indices ||=
|
161
|
-
face_lines.map do |line|
|
162
|
-
_, v1, v2, v3 = line.split(" ")
|
163
|
-
v1, v2, v3 = [v1, v2, v3].map { |v| v.split("/")[1].to_i }
|
164
|
-
[v1 - 1, v2 - 1, v3 - 1]
|
165
|
-
end.flatten
|
166
|
-
end
|
167
|
-
|
168
|
-
def normal_indices
|
169
|
-
@normal_indices ||=
|
170
|
-
face_lines.map do |line|
|
171
|
-
_, v1, v2, v3 = line.split(" ")
|
172
|
-
v1, v2, v3 = [v1, v2, v3].map { |v| v.split("/")[2].to_i }
|
173
|
-
[v1 - 1, v2 - 1, v3 - 1]
|
174
|
-
end.flatten
|
175
|
-
end
|
176
|
-
|
177
|
-
|
178
|
-
def material_for_line(line_number)
|
179
|
-
l = line_number
|
180
|
-
until l == 0
|
181
|
-
if @mesh_data[l].start_with?("usemtl ")
|
182
|
-
return @mesh_data[l].split(" ")[1]
|
183
|
-
end
|
184
|
-
l -= 1
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
def material_names
|
189
|
-
@material_names ||= []
|
190
|
-
end
|
191
|
-
|
192
|
-
def materials
|
193
|
-
@materials ||=
|
194
|
-
begin
|
195
|
-
current_material = nil
|
196
|
-
@mtl_data.each_with_object({}) do |line, materials|
|
197
|
-
if line.start_with?("newmtl ")
|
198
|
-
_, material_name = line.split(" ")
|
199
|
-
current_material = material_name
|
200
|
-
materials[material_name] = {}
|
201
|
-
elsif line.start_with?("Kd ")
|
202
|
-
_, r, g, b = line.split(" ")
|
203
|
-
materials[current_material][:diffuse] = Vector[r.to_f, g.to_f, b.to_f]
|
204
|
-
elsif line.start_with?("Ks ")
|
205
|
-
_, r, g, b = line.split(" ")
|
206
|
-
materials[current_material][:specular] = Vector[r.to_f, g.to_f, b.to_f]
|
207
|
-
elsif line.start_with?("Ka ")
|
208
|
-
_, r, g, b = line.split(" ")
|
209
|
-
materials[current_material][:albedo] = Vector[r.to_f, g.to_f, b.to_f]
|
210
|
-
end
|
211
|
-
end
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
def vertex_data
|
216
|
-
@vertex_data ||=
|
217
|
-
(0...vertex_indices.length).map do |i|
|
218
|
-
{
|
219
|
-
vertex: vertices[vertex_indices[i]],
|
220
|
-
normal: normals[normal_indices[i]],
|
221
|
-
texture_coord: texture_coords[texture_indices[i]],
|
222
|
-
tangent: tangents[i],
|
223
|
-
diffuse: materials.dig(material_names[i], :diffuse) || Vector[1, 1, 1],
|
224
|
-
specular: materials.dig(material_names[i], :specular) || Vector[1, 1, 1],
|
225
|
-
albedo: materials.dig(material_names[i], :albedo) || Vector[1, 1, 1],
|
226
|
-
}
|
227
|
-
end.map do |data|
|
228
|
-
[
|
229
|
-
data[:vertex][0], data[:vertex][1], data[:vertex][2],
|
230
|
-
data[:texture_coord][0], data[:texture_coord][1],
|
231
|
-
data[:normal][0], data[:normal][1], data[:normal][2],
|
232
|
-
data[:tangent][0].to_f, data[:tangent][1].to_f, data[:tangent][2].to_f,
|
233
|
-
data[:diffuse][0], data[:diffuse][1], data[:diffuse][2],
|
234
|
-
data[:specular][0], data[:specular][1], data[:specular][2],
|
235
|
-
data[:albedo][0], data[:albedo][1], data[:albedo][2],
|
236
|
-
]
|
237
|
-
end.flatten
|
238
|
-
end
|
239
|
-
|
240
|
-
def index_data
|
241
|
-
@index_data ||= (0...vertex_indices.length).map(&:to_i)
|
242
|
-
end
|
243
|
-
end
|
244
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Engine
|
4
|
+
class ObjFile
|
5
|
+
def initialize(file_path)
|
6
|
+
@mesh_data = File.readlines(file_path + ".obj")
|
7
|
+
if File.exist?(file_path + ".mtl")
|
8
|
+
@mtl_data = File.readlines(file_path + ".mtl").map(&:chomp)
|
9
|
+
else
|
10
|
+
@mtl_data = []
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def vertices
|
15
|
+
@vertices ||=
|
16
|
+
@mesh_data.select { |line| line.start_with?("v ") }.map do |line|
|
17
|
+
_, x, y, z = line.split(" ")
|
18
|
+
Vector[x.to_f, y.to_f, z.to_f]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def normals
|
23
|
+
@normals ||=
|
24
|
+
@mesh_data.select { |line| line.start_with?("vn ") }.map do |line|
|
25
|
+
_, x, y, z = line.split(" ")
|
26
|
+
Vector[x.to_f, y.to_f, z.to_f]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def tangents
|
31
|
+
@tangents ||=
|
32
|
+
(0...vertex_indices.length).each_slice(3).map do |i1, i2, i3|
|
33
|
+
vertex1 = vertices[vertex_indices[i1]]
|
34
|
+
normal1 = normals[normal_indices[i1]]
|
35
|
+
texture_coord1 = texture_coords[texture_indices[i1]]
|
36
|
+
|
37
|
+
vertex2 = vertices[vertex_indices[i2]]
|
38
|
+
normal2 = normals[normal_indices[i2]]
|
39
|
+
texture_coord2 = texture_coords[texture_indices[i2]]
|
40
|
+
|
41
|
+
vertex3 = vertices[vertex_indices[i3]]
|
42
|
+
normal3 = normals[normal_indices[i3]]
|
43
|
+
texture_coord3 = texture_coords[texture_indices[i3]]
|
44
|
+
|
45
|
+
triangle_vertices = [vertex1, vertex2, vertex3]
|
46
|
+
triangle_normals = [normal1, normal2, normal3]
|
47
|
+
triangle_texture_coords = [texture_coord1, texture_coord2, texture_coord3]
|
48
|
+
|
49
|
+
Engine::TangentCalculator.new(triangle_vertices, triangle_normals, triangle_texture_coords).calculate_tangents
|
50
|
+
end.flatten(1)
|
51
|
+
end
|
52
|
+
|
53
|
+
def texture_coords
|
54
|
+
@texture_coords ||=
|
55
|
+
@mesh_data.select { |line| line.start_with?("vt ") }.map do |line|
|
56
|
+
_, x, y = line.split(" ")
|
57
|
+
Vector[x.to_f, y.to_f]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def face_lines
|
62
|
+
@face_lines ||=
|
63
|
+
begin
|
64
|
+
split_faces(@mesh_data).select { |line| line.start_with?("f ") }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def split_faces(lines)
|
69
|
+
lines.map.with_index do |line, line_number|
|
70
|
+
if line.start_with?("f ")
|
71
|
+
new_faces = split_face(line)
|
72
|
+
(new_faces.length * 3).times do
|
73
|
+
material_names << material_for_line(line_number)
|
74
|
+
end
|
75
|
+
new_faces
|
76
|
+
else
|
77
|
+
line
|
78
|
+
end
|
79
|
+
end.flatten
|
80
|
+
end
|
81
|
+
|
82
|
+
def split_face(face)
|
83
|
+
face_vertices = face.split(" ")[1..-1]
|
84
|
+
return [face] if face_vertices.length == 3
|
85
|
+
|
86
|
+
world_points = face_vertices.map { |v| v.split("/")[0] }
|
87
|
+
.map { |i| vertices[i.to_i - 1] }
|
88
|
+
plane = plane_matrix(world_points)
|
89
|
+
flat_points = world_points.map { |point| plane * point }
|
90
|
+
packed_points = face_vertices.map.with_index do |v, i|
|
91
|
+
{ point_string: face_vertices[i], 0 => flat_points[i][0], 1 => flat_points[i][1] }
|
92
|
+
end
|
93
|
+
|
94
|
+
path = Path.new(packed_points.reverse)
|
95
|
+
begin
|
96
|
+
decompose_path(path, true)
|
97
|
+
rescue NoEarsException
|
98
|
+
decompose_path(Path.new(packed_points), false)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def decompose_path(path, reverse)
|
103
|
+
decomposed_vertices = []
|
104
|
+
depth = 100
|
105
|
+
until path.length == 3 || depth == 0
|
106
|
+
depth -= 1
|
107
|
+
ear_result = path.find_ear
|
108
|
+
ear = ear_result.first
|
109
|
+
new_path = ear_result.last
|
110
|
+
ear = ear.reverse! if reverse
|
111
|
+
ear_area = triangle_area(ear[0], ear[1], ear[2])
|
112
|
+
decomposed_vertices << ear unless ear_area < 0.0001
|
113
|
+
path = new_path
|
114
|
+
end
|
115
|
+
if reverse
|
116
|
+
decomposed_vertices << path.points.reverse
|
117
|
+
else
|
118
|
+
decomposed_vertices << path.points
|
119
|
+
end
|
120
|
+
decomposed_vertices.map do |triangle|
|
121
|
+
"f #{triangle.map { |v| v[:point_string] }.join(" ")}"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def triangle_area(a, b, c)
|
126
|
+
(a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1])).abs / 2.0
|
127
|
+
end
|
128
|
+
|
129
|
+
def plane_matrix(points)
|
130
|
+
raise "Not enough points to create a plane" if points.length < 3
|
131
|
+
|
132
|
+
origin_point = points[0]
|
133
|
+
a = points[1] - origin_point
|
134
|
+
b = nil
|
135
|
+
points[2..-1].each do |point|
|
136
|
+
diff = point - origin_point
|
137
|
+
b = diff if a.cross(diff).magnitude > 0.0001
|
138
|
+
end
|
139
|
+
raise "Points are collinear" if b.nil?
|
140
|
+
|
141
|
+
normal = a.cross(b).normalize
|
142
|
+
|
143
|
+
Matrix[
|
144
|
+
[a[0], a[1], a[2]],
|
145
|
+
[b[0], b[1], b[2]],
|
146
|
+
[normal[0], normal[1], normal[2]]
|
147
|
+
].transpose.inverse
|
148
|
+
end
|
149
|
+
|
150
|
+
def vertex_indices
|
151
|
+
@vertex_indices ||=
|
152
|
+
face_lines.map do |line|
|
153
|
+
_, v1, v2, v3 = line.split(" ")
|
154
|
+
v1, v2, v3 = [v1, v2, v3].map { |v| v.split("/").first.to_i }
|
155
|
+
[v1 - 1, v2 - 1, v3 - 1]
|
156
|
+
end.flatten
|
157
|
+
end
|
158
|
+
|
159
|
+
def texture_indices
|
160
|
+
@texture_indices ||=
|
161
|
+
face_lines.map do |line|
|
162
|
+
_, v1, v2, v3 = line.split(" ")
|
163
|
+
v1, v2, v3 = [v1, v2, v3].map { |v| v.split("/")[1].to_i }
|
164
|
+
[v1 - 1, v2 - 1, v3 - 1]
|
165
|
+
end.flatten
|
166
|
+
end
|
167
|
+
|
168
|
+
def normal_indices
|
169
|
+
@normal_indices ||=
|
170
|
+
face_lines.map do |line|
|
171
|
+
_, v1, v2, v3 = line.split(" ")
|
172
|
+
v1, v2, v3 = [v1, v2, v3].map { |v| v.split("/")[2].to_i }
|
173
|
+
[v1 - 1, v2 - 1, v3 - 1]
|
174
|
+
end.flatten
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
def material_for_line(line_number)
|
179
|
+
l = line_number
|
180
|
+
until l == 0
|
181
|
+
if @mesh_data[l].start_with?("usemtl ")
|
182
|
+
return @mesh_data[l].split(" ")[1]
|
183
|
+
end
|
184
|
+
l -= 1
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def material_names
|
189
|
+
@material_names ||= []
|
190
|
+
end
|
191
|
+
|
192
|
+
def materials
|
193
|
+
@materials ||=
|
194
|
+
begin
|
195
|
+
current_material = nil
|
196
|
+
@mtl_data.each_with_object({}) do |line, materials|
|
197
|
+
if line.start_with?("newmtl ")
|
198
|
+
_, material_name = line.split(" ")
|
199
|
+
current_material = material_name
|
200
|
+
materials[material_name] = {}
|
201
|
+
elsif line.start_with?("Kd ")
|
202
|
+
_, r, g, b = line.split(" ")
|
203
|
+
materials[current_material][:diffuse] = Vector[r.to_f, g.to_f, b.to_f]
|
204
|
+
elsif line.start_with?("Ks ")
|
205
|
+
_, r, g, b = line.split(" ")
|
206
|
+
materials[current_material][:specular] = Vector[r.to_f, g.to_f, b.to_f]
|
207
|
+
elsif line.start_with?("Ka ")
|
208
|
+
_, r, g, b = line.split(" ")
|
209
|
+
materials[current_material][:albedo] = Vector[r.to_f, g.to_f, b.to_f]
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
def vertex_data
|
216
|
+
@vertex_data ||=
|
217
|
+
(0...vertex_indices.length).map do |i|
|
218
|
+
{
|
219
|
+
vertex: vertices[vertex_indices[i]],
|
220
|
+
normal: normals[normal_indices[i]],
|
221
|
+
texture_coord: texture_coords[texture_indices[i]],
|
222
|
+
tangent: tangents[i],
|
223
|
+
diffuse: materials.dig(material_names[i], :diffuse) || Vector[1, 1, 1],
|
224
|
+
specular: materials.dig(material_names[i], :specular) || Vector[1, 1, 1],
|
225
|
+
albedo: materials.dig(material_names[i], :albedo) || Vector[1, 1, 1],
|
226
|
+
}
|
227
|
+
end.map do |data|
|
228
|
+
[
|
229
|
+
data[:vertex][0], data[:vertex][1], data[:vertex][2],
|
230
|
+
data[:texture_coord][0], data[:texture_coord][1],
|
231
|
+
data[:normal][0], data[:normal][1], data[:normal][2],
|
232
|
+
data[:tangent][0].to_f, data[:tangent][1].to_f, data[:tangent][2].to_f,
|
233
|
+
data[:diffuse][0], data[:diffuse][1], data[:diffuse][2],
|
234
|
+
data[:specular][0], data[:specular][1], data[:specular][2],
|
235
|
+
data[:albedo][0], data[:albedo][1], data[:albedo][2],
|
236
|
+
]
|
237
|
+
end.flatten
|
238
|
+
end
|
239
|
+
|
240
|
+
def index_data
|
241
|
+
@index_data ||= (0...vertex_indices.length).map(&:to_i)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
@@ -1,33 +1,33 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Engine
|
4
|
-
class ObjImporter
|
5
|
-
attr_reader :source, :destination_vertex, :destination_index
|
6
|
-
|
7
|
-
def initialize(source, destination_vertex, destination_index)
|
8
|
-
@source = source
|
9
|
-
@destination_vertex = destination_vertex
|
10
|
-
@destination_index = destination_index
|
11
|
-
end
|
12
|
-
|
13
|
-
def import
|
14
|
-
obj_file = Engine::ObjFile.new(source)
|
15
|
-
|
16
|
-
vertex_data = obj_file.vertex_data
|
17
|
-
|
18
|
-
FileUtils.mkdir_p(File.dirname(destination_vertex)) unless File.exist?(destination_vertex)
|
19
|
-
File.delete(destination_vertex) if File.exist?(destination_vertex)
|
20
|
-
File.open(destination_vertex, "w") do |file|
|
21
|
-
vertex_data.each { |d| file.puts d }
|
22
|
-
end
|
23
|
-
|
24
|
-
index_data = obj_file.index_data
|
25
|
-
|
26
|
-
FileUtils.mkdir_p(File.dirname(destination_index)) unless File.exist?(destination_index)
|
27
|
-
File.delete(destination_index) if File.exist?(destination_index)
|
28
|
-
File.open(destination_index, "w") do |file|
|
29
|
-
index_data.each { |i| file.puts i }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Engine
|
4
|
+
class ObjImporter
|
5
|
+
attr_reader :source, :destination_vertex, :destination_index
|
6
|
+
|
7
|
+
def initialize(source, destination_vertex, destination_index)
|
8
|
+
@source = source
|
9
|
+
@destination_vertex = destination_vertex
|
10
|
+
@destination_index = destination_index
|
11
|
+
end
|
12
|
+
|
13
|
+
def import
|
14
|
+
obj_file = Engine::ObjFile.new(source)
|
15
|
+
|
16
|
+
vertex_data = obj_file.vertex_data
|
17
|
+
|
18
|
+
FileUtils.mkdir_p(File.dirname(destination_vertex)) unless File.exist?(destination_vertex)
|
19
|
+
File.delete(destination_vertex) if File.exist?(destination_vertex)
|
20
|
+
File.open(destination_vertex, "w") do |file|
|
21
|
+
vertex_data.each { |d| file.puts d }
|
22
|
+
end
|
23
|
+
|
24
|
+
index_data = obj_file.index_data
|
25
|
+
|
26
|
+
FileUtils.mkdir_p(File.dirname(destination_index)) unless File.exist?(destination_index)
|
27
|
+
File.delete(destination_index) if File.exist?(destination_index)
|
28
|
+
File.open(destination_index, "w") do |file|
|
29
|
+
index_data.each { |i| file.puts i }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|