Switched to deterministic look-up table for cube uvs.

Changed code to snake case
Changed PoolArrays from real_t to float
Removed tangents from colored cube generator
master
Rathma 2020-11-09 00:45:49 -05:00
parent 04aaeab615
commit 3aeca4639f
5 changed files with 23 additions and 88 deletions

View File

@ -199,4 +199,12 @@ const Vector3i g_ordered_moore_area_3d[MOORE_AREA_3D_COUNT] = {
Vector3i(1, 1, 1) Vector3i(1, 1, 1)
}; };
const float g_side_tangents[SIDE_COUNT][4] = {
{0.0f, 0.0f, -1.0f, -1.0f},
{0.0f, 0.0f, 1.0f, -1.0f},
{1.0f, 0.0f, 0.0f, -1.0f},
{-1.0f, 0.0f, 0.0f, -1.0f},
{1.0f, 0.0f, 0.0f, -1.0f},
};
} // namespace Cube } // namespace Cube

View File

@ -60,6 +60,8 @@ enum Corner {
CORNER_COUNT CORNER_COUNT
}; };
extern const float g_side_tangents[SIDE_COUNT][4];
extern const Vector3 g_corner_position[CORNER_COUNT]; extern const Vector3 g_corner_position[CORNER_COUNT];
extern const int g_side_quad_triangles[SIDE_COUNT][6]; extern const int g_side_quad_triangles[SIDE_COUNT][6];

View File

@ -190,8 +190,6 @@ void Voxel::set_collision_mask(uint32_t mask) {
static void bake_cube_geometry(Voxel &config, Voxel::BakedData &baked_data, int p_atlas_size) { static void bake_cube_geometry(Voxel &config, Voxel::BakedData &baked_data, int p_atlas_size) {
const float sy = 1.0; const float sy = 1.0;
std::vector<Vector3> deltaPos;
for (unsigned int side = 0; side < Cube::SIDE_COUNT; ++side) { for (unsigned int side = 0; side < Cube::SIDE_COUNT; ++side) {
std::vector<Vector3> &positions = baked_data.model.side_positions[side]; std::vector<Vector3> &positions = baked_data.model.side_positions[side];
positions.resize(4); positions.resize(4);
@ -203,10 +201,6 @@ static void bake_cube_geometry(Voxel &config, Voxel::BakedData &baked_data, int
} }
positions[i] = p; positions[i] = p;
} }
//Saving them here so we don't have to loop through and get them later. Probably a better way of doing this.
deltaPos.push_back(Vector3(positions[2] - positions[0]));
deltaPos.push_back(Vector3(positions[1] - positions[0]));
deltaPos.push_back(Vector3(positions[3] - positions[0]));
std::vector<int> &indices = baked_data.model.side_indices[side]; std::vector<int> &indices = baked_data.model.side_indices[side];
indices.resize(6); indices.resize(6);
@ -230,8 +224,6 @@ static void bake_cube_geometry(Voxel &config, Voxel::BakedData &baked_data, int
CRASH_COND(atlas_size <= 0); CRASH_COND(atlas_size <= 0);
const float s = 1.0 / atlas_size; const float s = 1.0 / atlas_size;
std::vector<Vector2> deltaUV;
for (unsigned int side = 0; side < Cube::SIDE_COUNT; ++side) { for (unsigned int side = 0; side < Cube::SIDE_COUNT; ++side) {
baked_data.model.side_uvs[side].resize(4); baked_data.model.side_uvs[side].resize(4);
std::vector<Vector2> &uvs = baked_data.model.side_uvs[side]; std::vector<Vector2> &uvs = baked_data.model.side_uvs[side];
@ -239,42 +231,10 @@ static void bake_cube_geometry(Voxel &config, Voxel::BakedData &baked_data, int
uvs[i] = (config.get_cube_tile(side) + uv[i]) * s; uvs[i] = (config.get_cube_tile(side) + uv[i]) * s;
} }
//Tangent calculation modeled after http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-13-normal-mapping/#computing-the-tangents-and-bitangents
Vector2 deltaUV1 = uvs[2] - uvs[0];
Vector2 deltaUV2 = uvs[1] - uvs[0];
Vector2 deltaUV3 = uvs[3] - uvs[0];
float r1 = 1.0f / (deltaUV1[0] * deltaUV2[1] - deltaUV1[1] * deltaUV2[0]);
float r2 = 1.0f / (deltaUV3[0] * deltaUV1[1] - deltaUV3[1] * deltaUV1[0]);
Vector3 deltaPos1 = deltaPos[side * 3];
Vector3 deltaPos2 = deltaPos[side * 3+1];
Vector3 deltaPos3 = deltaPos[side * 3+2];
Vector3 t1 = (deltaPos1 * deltaUV2[1] - deltaPos2 * deltaUV1[1]) * r1;
Vector3 t2 = (deltaPos3 * deltaUV1[1] - deltaPos1 * deltaUV3[1]) * r2;
Vector3 bt1 = (deltaPos2 * deltaUV1[0] - deltaPos1 * deltaUV2[0]) * r1;
Vector3 bt2 = (deltaPos1 * deltaUV3[0] - deltaPos3 * deltaUV1[0]) * r2;
Vector3i ni = Cube::g_side_normals[side];
Vector3 n = Vector3(ni[0], ni[1], ni[2]);
float d1 = bt1.dot(n.cross(t1)) < 0 ? -1.0f : 1.0f;
float d2 = bt2.dot(n.cross(t2)) < 0 ? -1.0f : 1.0f;
std::vector<real_t> &tangents = baked_data.model.side_tangents[side]; std::vector<real_t> &tangents = baked_data.model.side_tangents[side];
for (unsigned int i = 0; i < 3; i++) { for (unsigned int i = 0; i < 12; ++i) {
tangents.push_back(t1[0]); for (unsigned int j = 0; j < 4; ++j)
tangents.push_back(t1[1]); tangents.push_back(Cube::g_side_tangents[side][j]);
tangents.push_back(t1[2]);
tangents.push_back(d1);
}
for (unsigned int i = 0; i < 3; i++) {
tangents.push_back(t2[0]);
tangents.push_back(t2[1]);
tangents.push_back(t2[2]);
tangents.push_back(d2);
} }
} }
@ -299,7 +259,7 @@ static void bake_mesh_geometry(Voxel &config, Voxel::BakedData &baked_data) {
PoolVector3Array positions = arrays[Mesh::ARRAY_VERTEX]; PoolVector3Array positions = arrays[Mesh::ARRAY_VERTEX];
PoolVector3Array normals = arrays[Mesh::ARRAY_NORMAL]; PoolVector3Array normals = arrays[Mesh::ARRAY_NORMAL];
PoolVector2Array uvs = arrays[Mesh::ARRAY_TEX_UV]; PoolVector2Array uvs = arrays[Mesh::ARRAY_TEX_UV];
PoolVector<real_t> tangents = arrays[Mesh::ARRAY_TANGENT]; PoolVector<float> tangents = arrays[Mesh::ARRAY_TANGENT];
baked_data.empty = positions.size() == 0; baked_data.empty = positions.size() == 0;
@ -342,11 +302,7 @@ static void bake_mesh_geometry(Voxel &config, Voxel::BakedData &baked_data) {
uvs.resize(positions.size()); uvs.resize(positions.size());
} }
bool tangents_empty = false; bool tangents_empty = tangents.size() == 0 ? false : true;
if(tangents.size() == 0) {
tangents = PoolVector<real_t>();
tangents_empty = true;
}
// Separate triangles belonging to faces of the cube // Separate triangles belonging to faces of the cube
@ -355,7 +311,7 @@ static void bake_mesh_geometry(Voxel &config, Voxel::BakedData &baked_data) {
PoolVector3Array::Read positions_read = positions.read(); PoolVector3Array::Read positions_read = positions.read();
PoolVector3Array::Read normals_read = normals.read(); PoolVector3Array::Read normals_read = normals.read();
PoolVector2Array::Read uvs_read = uvs.read(); PoolVector2Array::Read uvs_read = uvs.read();
PoolVector<real_t>::Read tangents_read = tangents.read(); PoolVector<float>::Read tangents_read = tangents.read();
FixedArray<HashMap<int, int>, Cube::SIDE_COUNT> added_side_indices; FixedArray<HashMap<int, int>, Cube::SIDE_COUNT> added_side_indices;
HashMap<int, int> added_regular_indices; HashMap<int, int> added_regular_indices;
@ -374,13 +330,13 @@ static void bake_mesh_geometry(Voxel &config, Voxel::BakedData &baked_data) {
if(tangents_empty){ if(tangents_empty){
//If tangents are empty then we calculate them //If tangents are empty then we calculate them
Vector2 deltaUV1 = uvs_read[indices_read[i+1]] - uvs_read[indices_read[i]]; Vector2 delta_uv1 = uvs_read[indices_read[i+1]] - uvs_read[indices_read[i]];
Vector2 deltaUV2 = uvs_read[indices_read[i+2]] - uvs_read[indices_read[i]]; Vector2 delta_uv2 = uvs_read[indices_read[i+2]] - uvs_read[indices_read[i]];
Vector3 deltaPos1 = tri_positions[1] - tri_positions[0]; Vector3 delta_pos1 = tri_positions[1] - tri_positions[0];
Vector3 deltaPos2 = tri_positions[2] - tri_positions[0]; Vector3 delta_pos2 = tri_positions[2] - tri_positions[0];
float r = 1.0f / (deltaUV1[0] * deltaUV2[1] - deltaUV1[1] * deltaUV2[0]); float r = 1.0f / (delta_uv1[0] * delta_uv2[1] - delta_uv1[1] * delta_uv2[0]);
Vector3 t = (deltaPos1 * deltaUV2[1] - deltaPos2 * deltaUV1[1])*r; Vector3 t = (delta_pos1 * delta_uv2[1] - delta_pos2 * delta_uv1[1])*r;
Vector3 bt = (deltaPos2 * deltaUV1[0] - deltaPos1 * deltaUV2[0])*r; Vector3 bt = (delta_pos2 * delta_uv1[0] - delta_pos1 * delta_uv2[0])*r;
tangent[0] = t[0]; tangent[0] = t[0];
tangent[1] = t[1]; tangent[1] = t[1];
tangent[2] = t[2]; tangent[2] = t[2];

View File

@ -343,11 +343,6 @@ void build_voxel_mesh_as_greedy_cubes(
Vector3 n; Vector3 n;
n[za] = m.side == SIDE_FRONT ? -1 : 1; n[za] = m.side == SIDE_FRONT ? -1 : 1;
Vector3 t = n.cross(v0);
Vector3 bt = t.cross(n);
float dt = bt.dot(n.cross(t));
float tangent[4] = {t[0], t[1], t[2], dt < 0 ? -1.0f : 1.0f};
// 2-----3 // 2-----3
// | | // | |
// | | // | |
@ -368,27 +363,6 @@ void build_voxel_mesh_as_greedy_cubes(
arrays.normals.push_back(n); arrays.normals.push_back(n);
arrays.normals.push_back(n); arrays.normals.push_back(n);
// One for each vertex
arrays.tangents.push_back(tangent[0]);
arrays.tangents.push_back(tangent[1]);
arrays.tangents.push_back(tangent[2]);
arrays.tangents.push_back(tangent[3]);
arrays.tangents.push_back(tangent[0]);
arrays.tangents.push_back(tangent[1]);
arrays.tangents.push_back(tangent[2]);
arrays.tangents.push_back(tangent[3]);
arrays.tangents.push_back(tangent[0]);
arrays.tangents.push_back(tangent[1]);
arrays.tangents.push_back(tangent[2]);
arrays.tangents.push_back(tangent[3]);
arrays.tangents.push_back(tangent[0]);
arrays.tangents.push_back(tangent[1]);
arrays.tangents.push_back(tangent[2]);
arrays.tangents.push_back(tangent[3]);
const unsigned int index_offset = index_offsets[material_index]; const unsigned int index_offset = index_offsets[material_index];
CRASH_COND(za >= 3 || m.side >= 2); CRASH_COND(za >= 3 || m.side >= 2);
const uint8_t *lut = g_indices_lut[za][m.side]; const uint8_t *lut = g_indices_lut[za][m.side];
@ -421,7 +395,6 @@ void VoxelMesherCubes::build(VoxelMesher::Output &output, const VoxelMesher::Inp
a.normals.clear(); a.normals.clear();
a.colors.clear(); a.colors.clear();
a.indices.clear(); a.indices.clear();
a.tangents.clear();
} }
const VoxelBuffer &voxels = input.voxels; const VoxelBuffer &voxels = input.voxels;
@ -629,19 +602,16 @@ void VoxelMesherCubes::build(VoxelMesher::Output &output, const VoxelMesher::Inp
PoolVector<Vector3> normals; PoolVector<Vector3> normals;
PoolVector<Color> colors; PoolVector<Color> colors;
PoolVector<int> indices; PoolVector<int> indices;
PoolVector<real_t> tangents;
raw_copy_to(positions, arrays.positions); raw_copy_to(positions, arrays.positions);
raw_copy_to(normals, arrays.normals); raw_copy_to(normals, arrays.normals);
raw_copy_to(colors, arrays.colors); raw_copy_to(colors, arrays.colors);
raw_copy_to(indices, arrays.indices); raw_copy_to(indices, arrays.indices);
raw_copy_to(tangents, arrays.tangents);
mesh_arrays[Mesh::ARRAY_VERTEX] = positions; mesh_arrays[Mesh::ARRAY_VERTEX] = positions;
mesh_arrays[Mesh::ARRAY_NORMAL] = normals; mesh_arrays[Mesh::ARRAY_NORMAL] = normals;
mesh_arrays[Mesh::ARRAY_COLOR] = colors; mesh_arrays[Mesh::ARRAY_COLOR] = colors;
mesh_arrays[Mesh::ARRAY_INDEX] = indices; mesh_arrays[Mesh::ARRAY_INDEX] = indices;
mesh_arrays[Mesh::ARRAY_TANGENT] = tangents;
} }
output.surfaces.push_back(mesh_arrays); output.surfaces.push_back(mesh_arrays);

View File

@ -51,7 +51,6 @@ public:
std::vector<Vector3> normals; std::vector<Vector3> normals;
std::vector<Color> colors; std::vector<Color> colors;
std::vector<int> indices; std::vector<int> indices;
std::vector<real_t> tangents;
}; };
protected: protected: