/* # 076 License Copyright (c) 076 Studio LLC Permission is hereby granted to any person obtaining a copy of the software Shader Playground (the "Software") to use, modify, merge, copy, publish, distribute, sublicense, and/or sell copies of the Software, subject to the following conditions: 1. **Origin Attribution**: - You must not misrepresent the origin of the Software; you must not claim you created the original Software. - If the Software is used in a product, you must either: a. Provide clear attribution in the product's documentation, user interface, or other visible areas, **OR** b. Pay the original developers a fee they specify in writing. 2. **Usage Restriction**: - The Software, or any derivative works, dependencies, or libraries incorporating it, must not be used for censorship or to suppress freedom of speech, expression, or creativity. Prohibited uses include, but are not limited to: - Censorship of so-called "hate speech", visuals, non-mainstream opinions, ideas, or objective reality. - Tools or systems designed to restrict access to information or artistic works. 3. **Notice Preservation**: - This license and the above copyright notice must remain intact in all copies of the source code. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include namespace lua { LuaEngine::LuaEngine(const string &code, Program *prog, Info *info) : m_Code(code) , m_Program(prog) , m_Info(info) { m_Lua = MakeAPI(); Execute(m_Lua); } LuaEngine::~LuaEngine() {} void LuaEngine::Reload(const string &code) { m_Code = code; m_Objects.clear(); m_Lua = MakeAPI(); Execute(m_Lua); } void LuaEngine::Init() { if (m_IsInitialized) return; sol::function init = m_Lua["init"]; if (init.valid()) { sol::protected_function_result res = init(); if (!res.valid()) { sol::error err = res; LogError(err); } } m_IsInitialized = true; } void LuaEngine::Update() { if (!m_IsInitialized) return; sol::function update = m_Lua["update"]; if (update.valid()) { sol::protected_function_result res = update(); if (!res.valid()) { sol::error err = res; LogError(err); } } } bool LuaEngine::Validate(const string &code) { try { sol::load_result resSyntax = m_Lua.load(code); if (!resSyntax.valid()) { sol::error err = resSyntax; LogError(err); return false; } sol::state testApi = MakeAPI(); sol::load_result resApi = testApi.load(code); //sol::protected_function_result resApi = testApi.safe_script(code); if (!resApi.valid()) { sol::error err = resApi; LogError(err); return false; } return true; } catch (const sol::error &err) { LogError(err); return false; } } //////////////////// void LuaEngine::LogError(sol::error err) { gui::LogEntry entry = {}; entry.type = gui::LogType::Error; entry.text = err.what(); m_Info->cmd->Add(entry); std::cerr << entry.text << std::endl; } void LuaEngine::LogError(const std::exception &err) { gui::LogEntry entry = {}; entry.type = gui::LogType::Error; entry.text = err.what(); m_Info->cmd->Add(entry); std::cerr << entry.text << std::endl; } void LuaEngine::LogError(const string &err) { gui::LogEntry entry = {}; entry.type = gui::LogType::Error; entry.text = err; m_Info->cmd->Add(entry); std::cerr << entry.text << std::endl; } sol::state LuaEngine::MakeAPI() { sol::state out; out.open_libraries( sol::lib::base, sol::lib::math, sol::lib::table, sol::lib::string ); out.new_usertype( "Vector2", "x", sol::property( [](Vector2 &v) { return v.pos.x; }, [](Vector2 &v, f32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector2 &v) { return v.pos.y; }, [](Vector2 &v, f32 val) { v.pos.y = val; } ) ); out.new_usertype( "Vector2i", "x", sol::property( [](Vector2i &v) { return v.pos.x; }, [](Vector2i &v, i32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector2i &v) { return v.pos.y; }, [](Vector2i &v, i32 val) { v.pos.y = val; } ) ); out.new_usertype( "Vector2u", "x", sol::property( [](Vector2u &v) { return v.pos.x; }, [](Vector2u &v, u32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector2u &v) { return v.pos.y; }, [](Vector2u &v, u32 val) { v.pos.y = val; } ) ); out.new_usertype( "Vector2f", "x", sol::property( [](Vector2f &v) { return v.pos.x; }, [](Vector2f &v, f32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector2f &v) { return v.pos.y; }, [](Vector2f &v, f32 val) { v.pos.y = val; } ) ); out.new_usertype( "Vector2d", "x", sol::property( [](Vector2d &v) { return v.pos.x; }, [](Vector2d &v, i32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector2d &v) { return v.pos.y; }, [](Vector2d &v, f64 val) { v.pos.y = val; } ) ); out.new_usertype( "Vector3", "x", sol::property( [](Vector3 &v) { return v.pos.x; }, [](Vector3 &v, f32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector3 &v) { return v.pos.y; }, [](Vector3 &v, f32 val) { v.pos.y = val; } ), "z", sol::property( [](Vector3 &v) { return v.pos.z; }, [](Vector3 &v, f32 val) { v.pos.z = val; } ) ); out.new_usertype( "Vector3i", "x", sol::property( [](Vector3i &v) { return v.pos.x; }, [](Vector3i &v, i32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector3i &v) { return v.pos.y; }, [](Vector3i &v, i32 val) { v.pos.y = val; } ), "z", sol::property( [](Vector3i &v) { return v.pos.z; }, [](Vector3i &v, i32 val) { v.pos.z = val; } ) ); out.new_usertype( "Vector3u", "x", sol::property( [](Vector3u &v) { return v.pos.x; }, [](Vector3u &v, u32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector3u &v) { return v.pos.y; }, [](Vector3u &v, u32 val) { v.pos.y = val; } ), "z", sol::property( [](Vector3u &v) { return v.pos.z; }, [](Vector3u &v, u32 val) { v.pos.z = val; } ) ); out.new_usertype( "Vector3f", "x", sol::property( [](Vector3f &v) { return v.pos.x; }, [](Vector3f &v, f32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector3f &v) { return v.pos.y; }, [](Vector3f &v, f32 val) { v.pos.y = val; } ), "z", sol::property( [](Vector3f &v) { return v.pos.z; }, [](Vector3f &v, f32 val) { v.pos.z = val; } ) ); out.new_usertype( "Vector3d", "x", sol::property( [](Vector3d &v) { return v.pos.x; }, [](Vector3d &v, i32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector3d &v) { return v.pos.y; }, [](Vector3d &v, f64 val) { v.pos.y = val; } ), "z", sol::property( [](Vector3d &v) { return v.pos.z; }, [](Vector3d &v, f64 val) { v.pos.z = val; } ) ); out.new_usertype( "Vector4", "x", sol::property( [](Vector4 &v) { return v.pos.x; }, [](Vector4 &v, f32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector4 &v) { return v.pos.y; }, [](Vector4 &v, f32 val) { v.pos.y = val; } ), "z", sol::property( [](Vector4 &v) { return v.pos.z; }, [](Vector4 &v, f32 val) { v.pos.z = val; } ), "w", sol::property( [](Vector4 &v) { return v.pos.w; }, [](Vector4 &v, f32 val) { v.pos.w = val; } ) ); out.new_usertype( "Vector4i", "x", sol::property( [](Vector4i &v) { return v.pos.x; }, [](Vector4i &v, i32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector4i &v) { return v.pos.y; }, [](Vector4i &v, i32 val) { v.pos.y = val; } ), "z", sol::property( [](Vector4i &v) { return v.pos.z; }, [](Vector4i &v, i32 val) { v.pos.z = val; } ), "w", sol::property( [](Vector4i &v) { return v.pos.w; }, [](Vector4i &v, i32 val) { v.pos.w = val; } ) ); out.new_usertype( "Vector4u", "x", sol::property( [](Vector4u &v) { return v.pos.x; }, [](Vector4u &v, u32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector4u &v) { return v.pos.y; }, [](Vector4u &v, u32 val) { v.pos.y = val; } ), "z", sol::property( [](Vector4u &v) { return v.pos.z; }, [](Vector4u &v, u32 val) { v.pos.z = val; } ), "w", sol::property( [](Vector4u &v) { return v.pos.w; }, [](Vector4u &v, u32 val) { v.pos.w = val; } ) ); out.new_usertype( "Vector4f", "x", sol::property( [](Vector4f &v) { return v.pos.x; }, [](Vector4f &v, f32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector4f &v) { return v.pos.y; }, [](Vector4f &v, f32 val) { v.pos.y = val; } ), "z", sol::property( [](Vector4f &v) { return v.pos.z; }, [](Vector4f &v, f32 val) { v.pos.z = val; } ), "w", sol::property( [](Vector4f &v) { return v.pos.w; }, [](Vector4f &v, f32 val) { v.pos.w = val; } ) ); out.new_usertype( "Vector4d", "x", sol::property( [](Vector4d &v) { return v.pos.x; }, [](Vector4d &v, i32 val) { v.pos.x = val; } ), "y", sol::property( [](Vector4d &v) { return v.pos.y; }, [](Vector4d &v, f64 val) { v.pos.y = val; } ), "z", sol::property( [](Vector4d &v) { return v.pos.z; }, [](Vector4d &v, f64 val) { v.pos.z = val; } ), "w", sol::property( [](Vector4d &v) { return v.pos.w; }, [](Vector4d &v, f64 val) { v.pos.w = val; } ) ); try { BindEngine(out); BindSystem(out); BindIO(out); BindMath(out); BindGraphics(out); } catch (const std::exception &e) { LogError(e); return {}; } return out; } void LuaEngine::BindEngine(sol::state &lua) { lua["le"] = lua.create_table(); lua["le"]["api_version"] = m_ApiVersion; lua["le"]["log"] = [&](sol::variadic_args va) { string out; for (auto v : va) { if (v.is()) out += v.as(); else if (v.is()) out += std::to_string(v.as()); else if (v.is()) out += std::to_string(v.as()); else if (v.is()) out += v.as() ? "true" : "false"; else if (v.is()) out += "{" + std::to_string(v.as().pos.x) + ", " + std::to_string(v.as().pos.y) + "}"; else if (v.is()) out += "{" + std::to_string(v.as().pos.x) + ", " + std::to_string(v.as().pos.y) + "}"; else if (v.is()) out += "{" + std::to_string(v.as().pos.x) + ", " + std::to_string(v.as().pos.y) + "}"; else if (v.is()) out += "{" + std::to_string(v.as().pos.x) + ", " + std::to_string(v.as().pos.y) + "}"; else if (v.is()) out += "{" + std::to_string(v.as().pos.x) + ", " + std::to_string(v.as().pos.y) + ", " + std::to_string(v.as().pos.z) + "}"; else if (v.is()) out += "{" + std::to_string(v.as().pos.x) + ", " + std::to_string(v.as().pos.y) + ", " + std::to_string(v.as().pos.z) + "}"; else if (v.is()) out += "{" + std::to_string(v.as().pos.x) + ", " + std::to_string(v.as().pos.y) + ", " + std::to_string(v.as().pos.z) + "}"; else if (v.is()) out += "{" + std::to_string(v.as().pos.x) + ", " + std::to_string(v.as().pos.y) + ", " + std::to_string(v.as().pos.z) + "}"; else if (v.is()) out += "{" + std::to_string(v.as().pos.x) + ", " + std::to_string(v.as().pos.y) + ", " + std::to_string(v.as().pos.z) + ", " + std::to_string(v.as().pos.w) + "}"; else if (v.is()) out += "{" + std::to_string(v.as().pos.x) + ", " + std::to_string(v.as().pos.y) + ", " + std::to_string(v.as().pos.z) + ", " + std::to_string(v.as().pos.w) + "}"; else if (v.is()) out += "{" + std::to_string(v.as().pos.x) + ", " + std::to_string(v.as().pos.y) + ", " + std::to_string(v.as().pos.z) + ", " + std::to_string(v.as().pos.w) + "}"; else if (v.is()) out += "{" + std::to_string(v.as().pos.x) + ", " + std::to_string(v.as().pos.y) + ", " + std::to_string(v.as().pos.z) + ", " + std::to_string(v.as().pos.w) + "}"; else if (v.get_type() == sol::type::table) { sol::table t = v.as(); out += "{"; bool first = true; for (auto &kv : t) { if (!first) out += ", "; first = false; if (kv.second.is()) out += std::to_string(kv.second.as()); else if (kv.second.is()) out += std::to_string(kv.second.as()); else if (kv.second.is()) out += kv.second.as(); else out += "undefined"; } out += "}"; } else out += "undefined"; out += " "; } gui::LogEntry entry = {}; entry.type = gui::LogType::Normal; entry.text = out; m_Info->cmd->Add(entry); std::cerr << entry.text << std::endl; }; lua["le"]["sizeof"] = [&](const string &type) -> size_t { static const std::unordered_map sizes = { { "float", sizeof(f32) }, { "double", sizeof(f64) }, { "int", sizeof(i32) }, { "uint", sizeof(u32) }, { "vec2", sizeof(f32) * 2 }, { "vec3", sizeof(f32) * 3 }, { "vec4", sizeof(f32) * 4 }, { "ivec2", sizeof(i32) * 2 }, { "ivec3", sizeof(i32) * 3 }, { "ivec4", sizeof(i32) * 4 }, { "uvec2", sizeof(u32) * 2 }, { "uvec3", sizeof(u32) * 3 }, { "uvec4", sizeof(u32) * 4 }, }; auto it = sizes.find(type); if (it == sizes.end()) { LogError("不正な値類: " + type); return 0; } return it->second; }; } void LuaEngine::BindSystem(sol::state &lua) { lua["le"]["sys"] = lua.create_table(); lua["le"]["sys"]["get_time"] = [&]() -> f32 { return (f32)m_Info->instance->GetTime(); }; lua["le"]["sys"]["get_delta_time"] = [&]() -> f32 { f32 now = (f32)m_Info->instance->GetTime(); f32 delta = now - m_LastTime; m_LastTime = now; if (delta > 1.f / 5) delta = 1.f / 5; return delta; }; lua["le"]["sys"]["get_timer_frequency"] = [&]() -> u64 { return m_Info->instance->GetTimerFrequency(); }; lua["le"]["sys"]["get_resolution"] = [&]() -> Vector2i { Vector2i size = {}; m_Info->window->GetSize(&size.pos.x, &size.pos.y); return size; }; lua["le"]["sys"]["random"] = [&](i32 min, i32 max) -> i32 { return util::rand::GetRandom(min, max); }; } void LuaEngine::BindIO(sol::state &lua) { lua["le"]["io"] = lua.create_table(); lua["le"]["io"]["key"] = lua.create_table(); lua["le"]["io"]["get_mouse"] = [&]() -> Vector2d { Vector2d cursor{}; m_Info->window->GetCursorPos(&cursor.pos.x, &cursor.pos.y); return cursor; }; lua["le"]["io"]["key"]["is_press"] = [this](const string &key) -> bool { if (!m_Info->viewportFocused) return false; return m_Info->window->GetKey(StringToKey(key)) == GLFW_PRESS; }; lua["le"]["io"]["key"]["is_release"] = [this](const string &key) -> bool { if (!m_Info->viewportFocused) return false; return m_Info->window->GetKey(StringToKey(key)) == GLFW_RELEASE; }; lua["le"]["io"]["key"]["is_hold"] = [this](const string &key) -> bool { if (!m_Info->viewportFocused) return false; return m_Info->window->GetKey(StringToKey(key)) == GLFW_REPEAT; }; } void LuaEngine::BindMath(sol::state &lua) { lua["le"]["math"] = lua.create_table(); lua["le"]["math"]["identity"] = [](sol::this_state ts) -> sol::table { sol::state_view lua(ts); Matrix4 m = mat4Identity(); sol::table t = lua.create_table(); for (i32 i = 0; i < 16; ++i) t[i + 1] = m.m[i]; return t; }; lua["le"]["math"]["translate"] = [](sol::this_state ts, f32 x, f32 y, f32 z) -> sol::table { sol::state_view lua(ts); Matrix4 m = mat4Translate(x, y, z); sol::table t = lua.create_table(); for (i32 i = 0; i < 16; ++i) t[i + 1] = m.m[i]; return t; }; lua["le"]["math"]["scale"] = [](sol::this_state ts, f32 x, f32 y, f32 z) -> sol::table { sol::state_view lua(ts); Matrix4 m = mat4Scale(x, y, z); sol::table t = lua.create_table(); for (i32 i = 0; i < 16; ++i) t[i + 1] = m.m[i]; return t; }; lua["le"]["math"]["rotate_x"] = [](sol::this_state ts, f32 angle) -> sol::table { sol::state_view lua(ts); Matrix4 m = mat4RotateX(angle); sol::table t = lua.create_table(); for (i32 i = 0; i < 16; ++i) t[i + 1] = m.m[i]; return t; }; lua["le"]["math"]["rotate_y"] = [](sol::this_state ts, f32 angle) -> sol::table { sol::state_view lua(ts); Matrix4 m = mat4RotateY(angle); sol::table t = lua.create_table(); for (i32 i = 0; i < 16; ++i) t[i + 1] = m.m[i]; return t; }; lua["le"]["math"]["rotate_z"] = [](sol::this_state ts, f32 angle) -> sol::table { sol::state_view lua(ts); Matrix4 m = mat4RotateZ(angle); sol::table t = lua.create_table(); for (i32 i = 0; i < 16; ++i) t[i + 1] = m.m[i]; return t; }; lua["le"]["math"]["rotate"] = [](sol::this_state ts, f32 pitch, f32 yaw, f32 roll) -> sol::table { sol::state_view lua(ts); Matrix4 m = mat4Rotate(pitch, yaw, roll); sol::table t = lua.create_table(); for (i32 i = 0; i < 16; ++i) t[i + 1] = m.m[i]; return t; }; lua["le"]["math"]["perspective"] = [](sol::this_state ts, f32 fov, f32 aspect, f32 near, f32 far) -> sol::table { sol::state_view lua(ts); Matrix4 m = mat4Perspective(fov, aspect, near, far); sol::table t = lua.create_table(); for (i32 i = 0; i < 16; ++i) t[i + 1] = m.m[i]; return t; }; lua["le"]["math"]["mul"] = [](sol::this_state ts, sol::table a, sol::table b) -> sol::table { sol::state_view lua(ts); Matrix4 ma{}, mb{}; for (i32 i = 0; i < 16; ++i) { ma.m[i] = a[i + 1]; mb.m[i] = b[i + 1]; } Matrix4 out = mat4Mul(ma, mb); sol::table t = lua.create_table(); for (i32 i = 0; i < 16; ++i) t[i + 1] = out.m[i]; return t; }; lua["le"]["math"]["look_at"] = [](sol::this_state ts, f32 ex, f32 ey, f32 ez, f32 tx, f32 ty, f32 tz, f32 ux, f32 uy, f32 uz) -> sol::table { sol::state_view lua(ts); Matrix4 m = mat4LookAt( { ex, ey, ez }, { tx, ty, tz }, { ux, uy, uz } ); sol::table t = lua.create_table(); for (i32 i = 0; i < 16; ++i) t[i + 1] = m.m[i]; return t; }; } void LuaEngine::BindGraphics(sol::state &lua) { lua["le"]["gfx"] = lua.create_table(); lua["le"]["gfx"]["add_mesh"] = [&](sol::table t) { MeshObject obj{}; obj.scale = { 1.f, 1.f, 1.f }; obj.mesh.vertices.clear(); obj.mesh.indices.clear(); obj.mesh.attr.clear(); sol::table verts = t["vertices"]; for (auto &v : verts) obj.mesh.vertices.push_back(v.second.as()); sol::table inds = t["indices"]; for (auto &i : inds) obj.mesh.indices.push_back(i.second.as()); sol::table attrs = t["attributes"]; for (auto &k : attrs) { sol::table a = k.second; sol::optional location = a["location"]; sol::optional size = a["size"]; sol::optional stride = a["stride"]; sol::optional offset = a["offset"]; if (!location || !size || !stride || !offset) { throw std::runtime_error("メッシュアトリビュートが不正です。"); } obj.mesh.attr.push_back({ a["location"], a["size"], a["stride"], a["offset"], }); } m_Objects.push_back(obj); }; lua["le"]["gfx"]["get_position"] = [&](i32 index) -> Vector3 { if (index < 0 || index >= m_Objects.size()) return { 0, 0, 0 }; auto pos = m_Objects[index].position.pos; return { pos.x, pos.y, pos.z }; }; lua["le"]["gfx"]["set_position"] = [&](i32 index, f32 x, f32 y, f32 z) { if (index < 0 || index >= m_Objects.size()) return; m_Objects[index].position = { x, y, z }; }; lua["le"]["gfx"]["get_rotation"] = [&](i32 index) -> Vector3 { if (index < 0 || index >= m_Objects.size()) return { 0, 0, 0 }; auto pos = m_Objects[index].rotation.pos; return { pos.x, pos.y, pos.z }; }; lua["le"]["gfx"]["set_rotation"] = [&](i32 index, f32 x, f32 y, f32 z) { if (index < 0 || index >= m_Objects.size()) return; m_Objects[index].rotation = { x, y, z }; }; lua["le"]["gfx"]["get_scale"] = [&](i32 index) -> Vector3 { if (index < 0 || index >= m_Objects.size()) return { 0, 0, 0 }; auto pos = m_Objects[index].scale.pos; return { pos.x, pos.y, pos.z }; }; lua["le"]["gfx"]["set_scale"] = [&](i32 index, f32 x, f32 y, f32 z) { if (index < 0 || index >= m_Objects.size()) return; m_Objects[index].scale = { x, y, z }; }; lua["le"]["gfx"]["set_uniform1"] = [&](const string &name, f32 x) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform1f(loc, x); }; lua["le"]["gfx"]["set_uniform2"] = [&](const string &name, f32 x, f32 y) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform2f(loc, x, y); }; lua["le"]["gfx"]["set_uniform3"] = [&](const string &name, f32 x, f32 y, f32 z) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform3f(loc, x, y, z); }; lua["le"]["gfx"]["set_uniform4"] = [&](const string &name, f32 x, f32 y, f32 z, f32 w) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform4f(loc, x, y, z, w); }; lua["le"]["gfx"]["set_uniform_float1"] = [&](const string &name, f32 x) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform1f(loc, x); }; lua["le"]["gfx"]["set_uniform_float2"] = [&](const string &name, f32 x, f32 y) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform2f(loc, x, y); }; lua["le"]["gfx"]["set_uniform_float3"] = [&](const string &name, f32 x, f32 y, f32 z) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform3f(loc, x, y, z); }; lua["le"]["gfx"]["set_uniform_float4"] = [&](const string &name, f32 x, f32 y, f32 z, f32 w) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform4f(loc, x, y, z, w); }; lua["le"]["gfx"]["set_uniform_double1"] = [&](const string &name, f64 x) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform1d(loc, x); }; lua["le"]["gfx"]["set_uniform_double2"] = [&](const string &name, f64 x, f64 y) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform2d(loc, x, y); }; lua["le"]["gfx"]["set_uniform_double3"] = [&](const string &name, f64 x, f64 y, f64 z) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform3d(loc, x, y, z); }; lua["le"]["gfx"]["set_uniform_double4"] = [&](const string &name, f64 x, f64 y, f64 z, f64 w) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform4d(loc, x, y, z, w); }; lua["le"]["gfx"]["set_uniform_int1"] = [&](const string &name, i32 x) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform1i(loc, x); }; lua["le"]["gfx"]["set_uniform_int2"] = [&](const string &name, i32 x, i32 y) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform2i(loc, x, y); }; lua["le"]["gfx"]["set_uniform_int3"] = [&](const string &name, i32 x, i32 y, i32 z) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform3i(loc, x, y, z); }; lua["le"]["gfx"]["set_uniform_int4"] = [&](const string &name, i32 x, i32 y, i32 z, i32 w) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform4i(loc, x, y, z, w); }; lua["le"]["gfx"]["set_uniform_uint1"] = [&](const string &name, u32 x) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform1ui(loc, x); }; lua["le"]["gfx"]["set_uniform_uint2"] = [&](const string &name, u32 x, u32 y) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform2ui(loc, x, y); }; lua["le"]["gfx"]["set_uniform_uint3"] = [&](const string &name, u32 x, u32 y, u32 z) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform3ui(loc, x, y, z); }; lua["le"]["gfx"]["set_uniform_uint4"] = [&](const string &name, u32 x, u32 y, u32 z, u32 w) { if (!m_Program) return; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniform4ui(loc, x, y, z, w); }; lua["le"]["gfx"]["set_uniform_vec3"] = [&](const string &name, f32 x, f32 y, f32 z) { m_Program->SetUniformVec3(name, { x, y, z }); }; lua["le"]["gfx"]["set_uniform_mat4"] = [&](const string &name, sol::table t) { if (!m_Program) return; if (t.size() != 16) { if (m_Info->i18n->GetCurrentLanguage().code == "ja_JP") LogError("mat4は16個の値が必要です。"); else LogError("mat4 must have 16 values."); return; } Matrix4 mat{}; for (i32 i = 0; i < 16; ++i) mat.m[i] = t[i + 1]; GLint loc = m_Program->GetUniformLocation(name); if (loc == -1) { LogError("ユニフォーム見つけられません: " + name); return; } glUniformMatrix4fv(loc, 1, GL_FALSE, mat.m); }; } void LuaEngine::Execute(sol::state &lua) { sol::protected_function_result res = lua.safe_script(m_Code); if (!res.valid()) { sol::error err = res; LogError(err); } } i32 LuaEngine::StringToKey(const string &key) { static std::unordered_map map = { { "q", GLFW_KEY_Q }, { "w", GLFW_KEY_W }, { "e", GLFW_KEY_E }, { "r", GLFW_KEY_R }, { "t", GLFW_KEY_T }, { "y", GLFW_KEY_Y }, { "u", GLFW_KEY_U }, { "i", GLFW_KEY_I }, { "o", GLFW_KEY_O }, { "p", GLFW_KEY_P }, { "a", GLFW_KEY_A }, { "s", GLFW_KEY_S }, { "d", GLFW_KEY_D }, { "f", GLFW_KEY_F }, { "g", GLFW_KEY_G }, { "h", GLFW_KEY_H }, { "j", GLFW_KEY_J }, { "k", GLFW_KEY_K }, { "l", GLFW_KEY_L }, { "z", GLFW_KEY_Z }, { "x", GLFW_KEY_X }, { "c", GLFW_KEY_C }, { "v", GLFW_KEY_V }, { "b", GLFW_KEY_B }, { "n", GLFW_KEY_N }, { "m", GLFW_KEY_M }, { "1", GLFW_KEY_1 }, { "2", GLFW_KEY_2 }, { "3", GLFW_KEY_3 }, { "4", GLFW_KEY_4 }, { "5", GLFW_KEY_5 }, { "6", GLFW_KEY_6 }, { "7", GLFW_KEY_7 }, { "8", GLFW_KEY_8 }, { "9", GLFW_KEY_9 }, { "0", GLFW_KEY_0 }, { "enter", GLFW_KEY_ENTER }, { "space", GLFW_KEY_SPACE }, { "shift", GLFW_KEY_LEFT_SHIFT | GLFW_KEY_RIGHT_SHIFT }, { "backspace", GLFW_KEY_BACKSPACE }, { "up", GLFW_KEY_UP }, { "down", GLFW_KEY_DOWN }, { "left", GLFW_KEY_LEFT }, { "right", GLFW_KEY_RIGHT }, }; auto it = map.find(key); if (it != map.end()) return it->second; return GLFW_KEY_UNKNOWN; } } // namespace lua