diff --git a/skelatool64/lua/sk_math.lua b/skelatool64/lua/sk_math.lua index c82364d..851e4f2 100644 --- a/skelatool64/lua/sk_math.lua +++ b/skelatool64/lua/sk_math.lua @@ -3,6 +3,7 @@ local Vector3 = {} local Box3 = {} local Quaternion = {} +local Color4 = {} --- creates a new 3d vector --- @function vector3 @@ -416,6 +417,25 @@ function Quaternion.slerp(a, b, t) ) end +--- creates a new 4d color +--- @function color +--- @tparam number r +--- @tparam number g +--- @tparam number b +--- @tparam number a +--- @treturn Color4 +local function color4(r, g, b, a) + return setmetatable({ r = r or 1, g = g or 1, b = b or 1, a = a or 1 }, Color4) +end + +--- determines if the input is a Vector3 +--- @function isColor4 +--- @tparam any obj +--- @treturn boolean +local function isColor4(obj) + return type(obj) == 'table' and type(obj.r) == 'number' and type(obj.g) == 'number' and type(obj.b) == 'number' and type(obj.a) == 'number' +end + return { vector3 = vector3, Vector3 = Vector3, @@ -426,4 +446,6 @@ return { quaternion = quaternion, axis_angle = axis_angle, isQuaternion = isQuaternion, + color4 = color4, + isColor4 = isColor4, } \ No newline at end of file diff --git a/skelatool64/src/lua_generator/LuaDefinitionWriter.cpp b/skelatool64/src/lua_generator/LuaDefinitionWriter.cpp index f4ce5b3..af00146 100644 --- a/skelatool64/src/lua_generator/LuaDefinitionWriter.cpp +++ b/skelatool64/src/lua_generator/LuaDefinitionWriter.cpp @@ -131,8 +131,8 @@ std::unique_ptr buildDataChunk(lua_State* L) { } if (type == LUA_TUSERDATA) { - if (luaIsLazyVector3DArray(L, -1)) { - struct aiVector3DArray* array = (struct aiVector3DArray*)lua_touserdata(L, -1); + if (luaIsLazyVector3DArray(L, -1)) { + struct LazyVectorWithLength* array = (struct LazyVectorWithLength*)lua_touserdata(L, -1); std::unique_ptr result(new StructureDataChunk()); diff --git a/skelatool64/src/lua_generator/LuaGeometry.cpp b/skelatool64/src/lua_generator/LuaGeometry.cpp index 7d52d2b..40bcee4 100644 --- a/skelatool64/src/lua_generator/LuaGeometry.cpp +++ b/skelatool64/src/lua_generator/LuaGeometry.cpp @@ -11,6 +11,16 @@ void toLua(lua_State* L, const aiVector3D& vector) { lua_call(L, 3, 1); } +void toLua(lua_State* L, const aiColor4D& color) { + luaLoadModuleFunction(L, "sk_math", "color4"); + toLua(L, color.r); + toLua(L, color.g); + toLua(L, color.b); + toLua(L, color.a); + lua_call(L, 4, 1); + +} + void toLua(lua_State* L, const aiQuaternion& quaternion) { luaLoadModuleFunction(L, "sk_math", "quaternion"); toLua(L, quaternion.x); @@ -53,5 +63,21 @@ void fromLua(lua_State* L, aiQuaternion& quaternion) { lua_getfield(L, -1, "w"); fromLua(L, quaternion.w); + lua_pop(L, 1); +} + +void fromLua(lua_State* L, aiColor4D& color) { + lua_getfield(L, -1, "r"); + fromLua(L, color.r); + + lua_getfield(L, -1, "g"); + fromLua(L, color.g); + + lua_getfield(L, -1, "b"); + fromLua(L, color.b); + + lua_getfield(L, -1, "a"); + fromLua(L, color.a); + lua_pop(L, 1); } \ No newline at end of file diff --git a/skelatool64/src/lua_generator/LuaGeometry.h b/skelatool64/src/lua_generator/LuaGeometry.h index 989cff3..f1ea1f6 100644 --- a/skelatool64/src/lua_generator/LuaGeometry.h +++ b/skelatool64/src/lua_generator/LuaGeometry.h @@ -6,9 +6,11 @@ void toLua(lua_State* L, const aiQuaternion& quaternion); void toLua(lua_State* L, const aiVector3D& vector); +void toLua(lua_State* L, const aiColor4D& color); void toLua(lua_State* L, const aiAABB& box); void fromLua(lua_State* L, aiVector3D& vector); void fromLua(lua_State* L, aiQuaternion& quaternion); +void fromLua(lua_State* L, aiColor4D& color); #endif \ No newline at end of file diff --git a/skelatool64/src/lua_generator/LuaMesh.cpp b/skelatool64/src/lua_generator/LuaMesh.cpp index d3e29c2..d8ec5aa 100644 --- a/skelatool64/src/lua_generator/LuaMesh.cpp +++ b/skelatool64/src/lua_generator/LuaMesh.cpp @@ -13,8 +13,9 @@ #include "./LuaTransform.h" #include "./LuaUtils.h" +template int luaGetVector3ArrayElement(lua_State* L) { - struct aiVector3DArray* array = (struct aiVector3DArray*)luaL_checkudata(L, 1, "aiVector3DArray"); + struct LazyVectorWithLength* array = (struct LazyVectorWithLength*)luaL_checkudata(L, 1, luaGetVectorName().c_str()); int index = luaL_checkinteger(L, 2); if (index <= 0 || index > array->length) { @@ -27,13 +28,14 @@ int luaGetVector3ArrayElement(lua_State* L) { return 1; } +template int luaSetVector3ArrayElement(lua_State* L) { lua_settop(L, 3); - aiVector3D value; + T value; fromLua(L, value); - struct aiVector3DArray* array = (struct aiVector3DArray*)luaL_checkudata(L, 1, "aiVector3DArray"); + struct LazyVectorWithLength* array = (struct LazyVectorWithLength*)luaL_checkudata(L, 1, luaGetVectorName().c_str()); int index = luaL_checkinteger(L, 2); if (index <= 0 || index > array->length) { @@ -45,14 +47,16 @@ int luaSetVector3ArrayElement(lua_State* L) { return 0; } +template int luaGetVector3ArrayLength(lua_State* L) { - struct aiVector3DArray* array = (struct aiVector3DArray*)luaL_checkudata(L, 1, "aiVector3DArray"); + struct LazyVectorWithLength* array = (struct LazyVectorWithLength*)luaL_checkudata(L, 1, luaGetVectorName().c_str()); lua_pushinteger(L, array->length); return 1; } +template int luaVector3ArrayNext(lua_State* L) { - struct aiVector3DArray* array = (struct aiVector3DArray*)luaL_checkudata(L, 1, "aiVector3DArray"); + struct LazyVectorWithLength* array = (struct LazyVectorWithLength*)luaL_checkudata(L, 1, luaGetVectorName().c_str()); if (lua_isnil(L, 2)) { if (array->length) { @@ -82,41 +86,39 @@ int luaVector3ArrayNext(lua_State* L) { return 1; } +template int luaVector3ArrayPairs(lua_State* L) { - lua_pushcfunction(L, luaVector3ArrayNext); + lua_pushcfunction(L, luaVector3ArrayNext); lua_pushnil(L); lua_copy(L, 1, -1); lua_pushnil(L); return 3; } -void toLuaLazyArray(lua_State* L, aiVector3D* vertices, unsigned count) { - struct aiVector3DArray* result = (struct aiVector3DArray*)lua_newuserdata(L, sizeof(struct aiVector3DArray)); +template +void toLuaLazyArray(lua_State* L, T* vertices, unsigned count) { + struct LazyVectorWithLength* result = (struct LazyVectorWithLength*)lua_newuserdata(L, sizeof(struct LazyVectorWithLength)); result->vertices = vertices; result->length = count; - if(luaL_newmetatable(L, "aiVector3DArray")) { - lua_pushcfunction(L, luaGetVector3ArrayElement); + if(luaL_newmetatable(L, luaGetVectorName().c_str())) { + lua_pushcfunction(L, luaGetVector3ArrayElement); lua_setfield(L, -2, "__index"); - lua_pushcfunction(L, luaSetVector3ArrayElement); + lua_pushcfunction(L, luaSetVector3ArrayElement); lua_setfield(L, -2, "__newindex"); - lua_pushcfunction(L, luaGetVector3ArrayLength); + lua_pushcfunction(L, luaGetVector3ArrayLength); lua_setfield(L, -2, "__len"); - lua_pushcfunction(L, luaVector3ArrayPairs); + lua_pushcfunction(L, luaVector3ArrayPairs); lua_setfield(L, -2, "__pairs"); } lua_setmetatable(L, -2); } -bool luaIsLazyVector3DArray(lua_State* L, int index) { - return luaL_testudata(L, index, "aiVector3DArray"); -} - /*** @table Material @tfield string name @@ -208,15 +210,22 @@ void meshToLua(lua_State* L, std::shared_ptr mesh) { lua_pushcfunction(L, luaTransformMesh); lua_setfield(L, -2, "transform"); - toLuaLazyArray(L, mesh->mMesh->mVertices, mesh->mMesh->mNumVertices); + toLuaLazyArray(L, mesh->mMesh->mVertices, mesh->mMesh->mNumVertices); lua_setfield(L, -2, "vertices"); - toLuaLazyArray(L, mesh->mMesh->mNormals, mesh->mMesh->mNumVertices); + toLuaLazyArray(L, mesh->mMesh->mNormals, mesh->mMesh->mNumVertices); lua_setfield(L, -2, "normals"); toLua(L, mesh->mMesh->mFaces, mesh->mMesh->mNumFaces); lua_setfield(L, -2, "faces"); + if (mesh->mMesh->mColors[0]) { + toLuaLazyArray(L, mesh->mMesh->mColors[0], mesh->mMesh->mNumVertices); + } else { + lua_pushnil(L); + } + lua_setfield(L, -2, "colors"); + if (mesh->mMesh->mMaterialIndex >= 0 && mesh->mMesh->mMaterialIndex < gLuaCurrentScene->mNumMaterials) { auto material = gLuaCurrentSettings->mMaterials.find(gLuaCurrentScene->mMaterials[mesh->mMesh->mMaterialIndex]->GetName().C_Str()); diff --git a/skelatool64/src/lua_generator/LuaMesh.h b/skelatool64/src/lua_generator/LuaMesh.h index 46f67ad..b03ae1e 100644 --- a/skelatool64/src/lua_generator/LuaMesh.h +++ b/skelatool64/src/lua_generator/LuaMesh.h @@ -6,15 +6,25 @@ #include "../CFileDefinition.h" #include "../DisplayListSettings.h" -struct aiVector3DArray { - aiVector3D* vertices; +template +struct LazyVectorWithLength { + T* vertices; int length; }; void fromLua(lua_State* L, Material *& material); void meshToLua(lua_State* L, std::shared_ptr mesh); void meshFromLua(lua_State* L, std::shared_ptr& mesh); -bool luaIsLazyVector3DArray(lua_State* L, int index); + +template +std::string luaGetVectorName() { + return std::string("LazyVectorWithLength<") + typeid(T).name() + std::string(">"); +} + +template +bool luaIsLazyVector3DArray(lua_State* L, int index) { + return luaL_testudata(L, index, luaGetVectorName().c_str()); +} void populateLuaMesh(lua_State* L, const aiScene* scene, CFileDefinition& fileDefinition, const DisplayListSettings& settings);