fixed lightmap atlas generation for soft body and 8-influence skinned mesh
This commit is contained in:
+71
-8
@@ -61,8 +61,11 @@ struct Atlas_Dim
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
};
|
||||
static Atlas_Dim GenerateMeshAtlas(MeshComponent& meshcomponent, uint32_t resolution)
|
||||
static Atlas_Dim GenerateMeshAtlas(Scene& scene, Entity entity, uint32_t resolution)
|
||||
{
|
||||
MeshComponent& meshcomponent = *scene.meshes.GetComponent(entity);
|
||||
SoftBodyPhysicsComponent* softbody = scene.softbodies.GetComponent(entity);
|
||||
|
||||
Atlas_Dim dim;
|
||||
|
||||
xatlas::Atlas* atlas = xatlas::Create();
|
||||
@@ -114,16 +117,24 @@ static Atlas_Dim GenerateMeshAtlas(MeshComponent& meshcomponent, uint32_t resolu
|
||||
wi::vector<XMFLOAT3> positions(mesh.vertexCount);
|
||||
wi::vector<XMFLOAT2> atlas(mesh.vertexCount);
|
||||
wi::vector<XMFLOAT3> normals;
|
||||
wi::vector<uint8_t> winds;
|
||||
wi::vector<XMFLOAT4> tangents;
|
||||
wi::vector<XMFLOAT2> uvset_0;
|
||||
wi::vector<XMFLOAT2> uvset_1;
|
||||
wi::vector<uint32_t> colors;
|
||||
wi::vector<XMUINT4> boneindices;
|
||||
wi::vector<XMFLOAT4> boneweights;
|
||||
wi::vector<XMUINT4> boneindices2;
|
||||
wi::vector<XMFLOAT4> boneweights2;
|
||||
wi::vector<float> softbodyweights;
|
||||
if (!meshcomponent.vertex_normals.empty())
|
||||
{
|
||||
normals.resize(mesh.vertexCount);
|
||||
}
|
||||
if (!meshcomponent.vertex_windweights.empty())
|
||||
{
|
||||
winds.resize(mesh.vertexCount);
|
||||
}
|
||||
if (!meshcomponent.vertex_tangents.empty())
|
||||
{
|
||||
tangents.resize(mesh.vertexCount);
|
||||
@@ -148,6 +159,18 @@ static Atlas_Dim GenerateMeshAtlas(MeshComponent& meshcomponent, uint32_t resolu
|
||||
{
|
||||
boneweights.resize(mesh.vertexCount);
|
||||
}
|
||||
if (!meshcomponent.vertex_boneindices2.empty())
|
||||
{
|
||||
boneindices2.resize(mesh.vertexCount);
|
||||
}
|
||||
if (!meshcomponent.vertex_boneweights2.empty())
|
||||
{
|
||||
boneweights2.resize(mesh.vertexCount);
|
||||
}
|
||||
if (softbody != nullptr && !softbody->weights.empty())
|
||||
{
|
||||
softbodyweights.resize(mesh.vertexCount);
|
||||
}
|
||||
|
||||
for (uint32_t j = 0; j < mesh.indexCount; ++j)
|
||||
{
|
||||
@@ -161,6 +184,10 @@ static Atlas_Dim GenerateMeshAtlas(MeshComponent& meshcomponent, uint32_t resolu
|
||||
{
|
||||
normals[ind] = meshcomponent.vertex_normals[v.xref];
|
||||
}
|
||||
if (!winds.empty())
|
||||
{
|
||||
winds[ind] = meshcomponent.vertex_windweights[v.xref];
|
||||
}
|
||||
if (!tangents.empty())
|
||||
{
|
||||
tangents[ind] = meshcomponent.vertex_tangents[v.xref];
|
||||
@@ -185,6 +212,18 @@ static Atlas_Dim GenerateMeshAtlas(MeshComponent& meshcomponent, uint32_t resolu
|
||||
{
|
||||
boneweights[ind] = meshcomponent.vertex_boneweights[v.xref];
|
||||
}
|
||||
if (!boneindices2.empty())
|
||||
{
|
||||
boneindices2[ind] = meshcomponent.vertex_boneindices2[v.xref];
|
||||
}
|
||||
if (!boneweights2.empty())
|
||||
{
|
||||
boneweights2[ind] = meshcomponent.vertex_boneweights2[v.xref];
|
||||
}
|
||||
if (softbody != nullptr && !softbodyweights.empty())
|
||||
{
|
||||
softbodyweights[ind] = softbody->weights[v.xref];
|
||||
}
|
||||
}
|
||||
|
||||
meshcomponent.vertex_positions = positions;
|
||||
@@ -193,6 +232,10 @@ static Atlas_Dim GenerateMeshAtlas(MeshComponent& meshcomponent, uint32_t resolu
|
||||
{
|
||||
meshcomponent.vertex_normals = normals;
|
||||
}
|
||||
if (!winds.empty())
|
||||
{
|
||||
meshcomponent.vertex_windweights = winds;
|
||||
}
|
||||
if (!tangents.empty())
|
||||
{
|
||||
meshcomponent.vertex_tangents = tangents;
|
||||
@@ -217,8 +260,29 @@ static Atlas_Dim GenerateMeshAtlas(MeshComponent& meshcomponent, uint32_t resolu
|
||||
{
|
||||
meshcomponent.vertex_boneweights = boneweights;
|
||||
}
|
||||
if (!boneindices2.empty())
|
||||
{
|
||||
meshcomponent.vertex_boneindices2 = boneindices2;
|
||||
}
|
||||
if (!boneweights2.empty())
|
||||
{
|
||||
meshcomponent.vertex_boneweights2 = boneweights2;
|
||||
}
|
||||
if (softbody != nullptr && !softbodyweights.empty())
|
||||
{
|
||||
softbody->weights = softbodyweights;
|
||||
}
|
||||
meshcomponent.CreateRenderData();
|
||||
|
||||
if (softbody != nullptr)
|
||||
{
|
||||
// Recreate softbody
|
||||
softbody->physicsobject = {};
|
||||
softbody->physicsIndices.clear();
|
||||
softbody->physicsToGraphicsVertexMapping.clear();
|
||||
softbody->CreateFromMesh(meshcomponent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//// DEBUG
|
||||
@@ -601,7 +665,7 @@ void ObjectWindow::Create(EditorComponent* _editor)
|
||||
UV_GEN_TYPE gen_type = (UV_GEN_TYPE)lightmapSourceUVSetComboBox.GetSelected();
|
||||
|
||||
wi::unordered_set<ObjectComponent*> gen_objects;
|
||||
wi::unordered_map<MeshComponent*, Atlas_Dim> gen_meshes;
|
||||
wi::unordered_map<Entity, Atlas_Dim> gen_meshes;
|
||||
|
||||
for (auto& x : this->editor->translator.selected)
|
||||
{
|
||||
@@ -613,7 +677,7 @@ void ObjectWindow::Create(EditorComponent* _editor)
|
||||
if (meshcomponent != nullptr)
|
||||
{
|
||||
gen_objects.insert(objectcomponent);
|
||||
gen_meshes[meshcomponent] = Atlas_Dim();
|
||||
gen_meshes[objectcomponent->meshID] = Atlas_Dim();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -623,7 +687,7 @@ void ObjectWindow::Create(EditorComponent* _editor)
|
||||
|
||||
for (auto& it : gen_meshes)
|
||||
{
|
||||
MeshComponent& mesh = *it.first;
|
||||
MeshComponent& mesh = *scene.meshes.GetComponent(it.first);
|
||||
if (gen_type == UV_GEN_COPY_UVSET_0)
|
||||
{
|
||||
mesh.vertex_atlas = mesh.vertex_uvset_0;
|
||||
@@ -637,7 +701,7 @@ void ObjectWindow::Create(EditorComponent* _editor)
|
||||
else if (gen_type == UV_GEN_GENERATE_ATLAS)
|
||||
{
|
||||
wi::jobsystem::Execute(ctx, [&](wi::jobsystem::JobArgs args) {
|
||||
it.second = GenerateMeshAtlas(mesh, (uint32_t)lightmapResolutionSlider.GetValue());
|
||||
it.second = GenerateMeshAtlas(scene, it.first, (uint32_t)lightmapResolutionSlider.GetValue());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -646,11 +710,10 @@ void ObjectWindow::Create(EditorComponent* _editor)
|
||||
for (auto& x : gen_objects)
|
||||
{
|
||||
x->ClearLightmap();
|
||||
MeshComponent* meshcomponent = scene.meshes.GetComponent(x->meshID);
|
||||
if (gen_type == UV_GEN_GENERATE_ATLAS)
|
||||
{
|
||||
x->lightmapWidth = gen_meshes.at(meshcomponent).width;
|
||||
x->lightmapHeight = gen_meshes.at(meshcomponent).height;
|
||||
x->lightmapWidth = gen_meshes.at(x->meshID).width;
|
||||
x->lightmapHeight = gen_meshes.at(x->meshID).height;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user