Add color to lua meshes
This commit is contained in:
parent
7340abedf6
commit
57e4544fee
|
@ -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,
|
||||
}
|
|
@ -131,8 +131,8 @@ std::unique_ptr<DataChunk> buildDataChunk(lua_State* L) {
|
|||
}
|
||||
|
||||
if (type == LUA_TUSERDATA) {
|
||||
if (luaIsLazyVector3DArray(L, -1)) {
|
||||
struct aiVector3DArray* array = (struct aiVector3DArray*)lua_touserdata(L, -1);
|
||||
if (luaIsLazyVector3DArray<aiVector3D>(L, -1)) {
|
||||
struct LazyVectorWithLength<aiVector3D>* array = (struct LazyVectorWithLength<aiVector3D>*)lua_touserdata(L, -1);
|
||||
|
||||
std::unique_ptr<StructureDataChunk> result(new StructureDataChunk());
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
@ -55,3 +65,19 @@ void fromLua(lua_State* L, aiQuaternion& quaternion) {
|
|||
|
||||
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);
|
||||
}
|
|
@ -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
|
|
@ -13,8 +13,9 @@
|
|||
#include "./LuaTransform.h"
|
||||
#include "./LuaUtils.h"
|
||||
|
||||
template <typename T>
|
||||
int luaGetVector3ArrayElement(lua_State* L) {
|
||||
struct aiVector3DArray* array = (struct aiVector3DArray*)luaL_checkudata(L, 1, "aiVector3DArray");
|
||||
struct LazyVectorWithLength<T>* array = (struct LazyVectorWithLength<T>*)luaL_checkudata(L, 1, luaGetVectorName<T>().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 <typename T>
|
||||
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<T>* array = (struct LazyVectorWithLength<T>*)luaL_checkudata(L, 1, luaGetVectorName<T>().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 <typename T>
|
||||
int luaGetVector3ArrayLength(lua_State* L) {
|
||||
struct aiVector3DArray* array = (struct aiVector3DArray*)luaL_checkudata(L, 1, "aiVector3DArray");
|
||||
struct LazyVectorWithLength<T>* array = (struct LazyVectorWithLength<T>*)luaL_checkudata(L, 1, luaGetVectorName<T>().c_str());
|
||||
lua_pushinteger(L, array->length);
|
||||
return 1;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int luaVector3ArrayNext(lua_State* L) {
|
||||
struct aiVector3DArray* array = (struct aiVector3DArray*)luaL_checkudata(L, 1, "aiVector3DArray");
|
||||
struct LazyVectorWithLength<T>* array = (struct LazyVectorWithLength<T>*)luaL_checkudata(L, 1, luaGetVectorName<T>().c_str());
|
||||
|
||||
if (lua_isnil(L, 2)) {
|
||||
if (array->length) {
|
||||
|
@ -82,41 +86,39 @@ int luaVector3ArrayNext(lua_State* L) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int luaVector3ArrayPairs(lua_State* L) {
|
||||
lua_pushcfunction(L, luaVector3ArrayNext);
|
||||
lua_pushcfunction(L, luaVector3ArrayNext<T>);
|
||||
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 <typename T>
|
||||
void toLuaLazyArray(lua_State* L, T* vertices, unsigned count) {
|
||||
struct LazyVectorWithLength<T>* result = (struct LazyVectorWithLength<T>*)lua_newuserdata(L, sizeof(struct LazyVectorWithLength<T>));
|
||||
|
||||
result->vertices = vertices;
|
||||
result->length = count;
|
||||
|
||||
if(luaL_newmetatable(L, "aiVector3DArray")) {
|
||||
lua_pushcfunction(L, luaGetVector3ArrayElement);
|
||||
if(luaL_newmetatable(L, luaGetVectorName<T>().c_str())) {
|
||||
lua_pushcfunction(L, luaGetVector3ArrayElement<T>);
|
||||
lua_setfield(L, -2, "__index");
|
||||
|
||||
lua_pushcfunction(L, luaSetVector3ArrayElement);
|
||||
lua_pushcfunction(L, luaSetVector3ArrayElement<T>);
|
||||
lua_setfield(L, -2, "__newindex");
|
||||
|
||||
lua_pushcfunction(L, luaGetVector3ArrayLength);
|
||||
lua_pushcfunction(L, luaGetVector3ArrayLength<T>);
|
||||
lua_setfield(L, -2, "__len");
|
||||
|
||||
lua_pushcfunction(L, luaVector3ArrayPairs);
|
||||
lua_pushcfunction(L, luaVector3ArrayPairs<T>);
|
||||
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<ExtendedMesh> mesh) {
|
|||
lua_pushcfunction(L, luaTransformMesh);
|
||||
lua_setfield(L, -2, "transform");
|
||||
|
||||
toLuaLazyArray(L, mesh->mMesh->mVertices, mesh->mMesh->mNumVertices);
|
||||
toLuaLazyArray<aiVector3D>(L, mesh->mMesh->mVertices, mesh->mMesh->mNumVertices);
|
||||
lua_setfield(L, -2, "vertices");
|
||||
|
||||
toLuaLazyArray(L, mesh->mMesh->mNormals, mesh->mMesh->mNumVertices);
|
||||
toLuaLazyArray<aiVector3D>(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<aiColor4D>(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());
|
||||
|
||||
|
|
|
@ -6,15 +6,25 @@
|
|||
#include "../CFileDefinition.h"
|
||||
#include "../DisplayListSettings.h"
|
||||
|
||||
struct aiVector3DArray {
|
||||
aiVector3D* vertices;
|
||||
template <typename T>
|
||||
struct LazyVectorWithLength {
|
||||
T* vertices;
|
||||
int length;
|
||||
};
|
||||
|
||||
void fromLua(lua_State* L, Material *& material);
|
||||
void meshToLua(lua_State* L, std::shared_ptr<ExtendedMesh> mesh);
|
||||
void meshFromLua(lua_State* L, std::shared_ptr<ExtendedMesh>& mesh);
|
||||
bool luaIsLazyVector3DArray(lua_State* L, int index);
|
||||
|
||||
template <typename T>
|
||||
std::string luaGetVectorName() {
|
||||
return std::string("LazyVectorWithLength<") + typeid(T).name() + std::string(">");
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool luaIsLazyVector3DArray(lua_State* L, int index) {
|
||||
return luaL_testudata(L, index, luaGetVectorName<T>().c_str());
|
||||
}
|
||||
|
||||
void populateLuaMesh(lua_State* L, const aiScene* scene, CFileDefinition& fileDefinition, const DisplayListSettings& settings);
|
||||
|
||||
|
|
Loading…
Reference in a new issue