-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
298 lines (252 loc) · 6.61 KB
/
main.lua
File metadata and controls
298 lines (252 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
if os.getenv "LOCAL_LUA_DEBUGGER_VSCODE" == "1" then
require("lldebugger").start()
end
package.path = package.cpath
.. string.format(
";%s\\libs\\?.lua;%s\\libs\\?\\init.lua",
love.filesystem.getSource(),
love.filesystem.getSource()
)
local ffi = require "ffi"
local falg = require "falg"
local logging = require "logging"
local UI = require "ui"
---@class cimgui
local imgui = require "cimgui"
---
--- State
---
---@class State: StateInstance
local State = {}
State.__index = State
---@return State
function State.new()
---@class StateInstance
local instance = {
docking_space = require("ui.docking_space").new "DOCKSPACE",
time = require("lvrm.time").new(),
mesh = require("ui.mesh").new(),
animation = require("ui.animation").new(),
logger = require("ui.logger").new(),
skin = require("ui.skin").new(),
scene_ui = require("ui.scene").new(),
renderer = require("lvrm.rendertarget").new(),
}
---@type State
return setmetatable(instance, State)
end
---@param path string?
function State:load(path)
if not path then
return
end
logging.info("load %s...", path)
local lvrm_reader = require "lvrm.gltf_reader"
local reader = lvrm_reader.read_from_path(path)
if reader then
self.scene = nil
self.error = nil
self.json_root = reader.root
local Scene = require "lvrm.scene"
local ok, result = pcall(Scene.load, reader)
if ok then
self.scene = result
else
self.error = result
end
end
end
function State:draw()
self.docking_space:begin()
self.docking_space:draw()
end
local STATE = State.new()
-- love.data.newByteData()
love.load = function(args)
imgui.love.Init "RGBA32" -- or imgui.love.Init("RGBA32") or imgui.love.Init("Alpha8")
do
local io = imgui.GetIO()
-- Enable Docking
io.ConfigFlags =
bit.bor(io.ConfigFlags, imgui.ImGuiConfigFlags_DockingEnable)
end
local ja_font = "C:/Windows/Fonts/meiryo.ttc"
do
local util = require "lvrm.util"
local data = util.readfile(ja_font)
if not data then
return
end
local font = love.graphics.newFont(love.data.newByteData(data), 32)
if font then
love.graphics.setFont(font)
end
end
do
local io = imgui.GetIO()
local font =
UI.AddFont(0, ja_font, 20, false, io.Fonts:GetGlyphRangesJapanese())
-- emoji
UI.AddFont(
1,
"C:/Windows/Fonts/Seguiemj.ttf",
15,
true,
ffi.new("uint32_t[3]", 0x1, 0x1FFFF, 0)
)
io.FontDefault = font
imgui.love.BuildFontAtlas "RGBA32"
end
STATE:load(args[1])
local ShowJson = require "ui.gltf_json"
STATE.docking_space
:add("glTF", function()
ShowJson(STATE.json_root)
end)
:no_padding()
STATE.docking_space
:add("scene", function()
STATE.scene_ui:ShowScene(STATE.scene)
end)
:no_padding()
STATE.docking_space
:add("error", function()
imgui.TextUnformatted "emoji: 🌻"
if STATE.error then
imgui.TextWrapped(STATE.error)
end
end)
:no_padding()
STATE.docking_space
:add("mesh", function()
STATE.mesh:ShowMesh(STATE.json_root, STATE.scene)
end)
:no_padding()
STATE.docking_space
:add("selected_mesh", function()
STATE.mesh:ShowSelected(STATE.scene)
end)
:no_padding()
local ShowTime = require "ui.time"
STATE.docking_space:add("time", function()
ShowTime(STATE.time)
end)
STATE.docking_space
:add("animation", function()
STATE.animation:ShowAnimation(STATE.scene)
end)
:no_padding()
STATE.docking_space
:add("skin", function()
STATE.skin:ShowSkin(STATE.scene)
end)
:no_padding()
STATE.docking_space
:add("logger", function()
STATE.logger:show()
end)
:no_padding()
local gltf_sample_models = os.getenv "GLTF_SAMPLE_MODELS"
if gltf_sample_models then
local AssetViewer = require "ui.assetviewer"
local asset = AssetViewer.new(gltf_sample_models .. "/2.0")
STATE.docking_space
:add("gltf_sample_models", function()
local new_path = asset:Show()
if new_path then
STATE:load(new_path.path)
end
end)
:no_padding()
end
STATE.docking_space
:add("3d view", function()
-- update canvas size
local size = imgui.GetContentRegionAvail()
STATE.renderer:update_size(size.x, size.y)
local isActive, isHovered =
UI.DraggableImage("image_button", STATE.renderer.colorcanvas, size)
-- render scene to rendertarget
if STATE.scene and STATE.animation.selected then
STATE.scene:set_time(
STATE.time.seconds,
STATE.time.loop[0],
STATE.animation.selected
)
end
STATE.renderer:render(function(view, projection)
if STATE.scene then
STATE.scene:draw(view, projection)
end
end, {
isActive = isActive,
isHovered = isHovered,
target = STATE.scene,
})
end)
:no_padding()
:no_scrollbar()
if os.getenv "LOCAL_LUA_DEBUGGER_VSCODE" == "1" then
STATE.docking_space:add("imgui demo", imgui.ShowDemoWindow, true)
end
end
love.draw = function()
imgui.NewFrame()
local io = imgui.GetIO()
STATE.time:update(io.DeltaTime)
STATE:draw()
-- code to render imgui
imgui.Render()
imgui.love.RenderDrawLists()
end
love.update = function(dt)
imgui.love.Update(dt)
end
love.mousemoved = function(x, y, ...)
imgui.love.MouseMoved(x, y)
end
love.mousepressed = function(x, y, button, ...)
imgui.love.MousePressed(button)
end
love.mousereleased = function(x, y, button, ...)
imgui.love.MouseReleased(button)
end
love.wheelmoved = function(x, y)
logging.debug("wheel: %f, %f", x, y)
imgui.love.WheelMoved(x, y)
end
love.keypressed = function(key, ...)
imgui.love.KeyPressed(key)
end
love.keyreleased = function(key, ...)
imgui.love.KeyReleased(key)
end
love.textinput = function(t)
imgui.love.TextInput(t)
end
love.quit = function()
return imgui.love.Shutdown()
end
-- for gamepad support also add the following:
love.joystickadded = function(joystick)
imgui.love.JoystickAdded(joystick)
-- your code here
end
love.joystickremoved = function(joystick)
imgui.love.JoystickRemoved()
-- your code here
end
love.gamepadpressed = function(joystick, button)
imgui.love.GamepadPressed(button)
-- your code here
end
love.gamepadreleased = function(joystick, button)
imgui.love.GamepadReleased(button)
-- your code here
end
-- choose threshold for considering analog controllers active, defaults to 0 if unspecified
local threshold = 0.2
love.gamepadaxis = function(joystick, axis, value)
imgui.love.GamepadAxis(axis, value, threshold)
-- your code here
end