Add texture coordinates to primitive shape types.

PiperOrigin-RevId: 964882123
Change-Id: I17723d21f29b3fe28e171de5c3d2bea1f55a94c2
This commit is contained in:
Sam Haves
2026-08-14 14:10:32 -07:00
committed by Copybara-Service
parent 4929f2cd99
commit cc7fb98cf4
18 changed files with 552 additions and 368 deletions
+43 -17
View File
@@ -157,15 +157,17 @@ static void FillMeshBuffer(MeshBuilder& builder, const mjModel* model, int meshi
static void FillHeightFieldBuffer(MeshBuilder& builder, const mjModel* model,
int hfieldid) {
auto append_tri = [&](float3 a, float3 b, float3 c) {
auto append_tri = [&](float3 a, float3 b, float3 c, float2 uv_a, float2 uv_b,
float2 uv_c) {
float4 orientation = CalculateOrientation(a, b, c);
builder.Append(a, orientation, float2(0, 0));
builder.Append(b, orientation, float2(0, 0));
builder.Append(c, orientation, float2(0, 0));
builder.Append(a, orientation, uv_a);
builder.Append(b, orientation, uv_b);
builder.Append(c, orientation, uv_c);
};
auto append_quad = [&](float3 a, float3 b, float3 c, float3 d) {
append_tri(a, b, d);
append_tri(d, b, c);
auto append_quad = [&](float3 a, float3 b, float3 c, float3 d, float2 uv_a,
float2 uv_b, float2 uv_c, float2 uv_d) {
append_tri(a, b, d, uv_a, uv_b, uv_d);
append_tri(d, b, c, uv_d, uv_b, uv_c);
};
const float* data = model->hfield_data + model->hfield_adr[hfieldid];
@@ -227,10 +229,21 @@ static void FillHeightFieldBuffer(MeshBuilder& builder, const mjModel* model,
}
const float3 mid = {mid_x, mid_y, mid_z};
append_tri(a, b, mid);
append_tri(b, c, mid);
append_tri(c, d, mid);
append_tri(d, a, mid);
const float2 uv_a = {(float)col / (ncol - 1),
1.f - (float)row / (nrow - 1)};
const float2 uv_b = {(float)(col + 1) / (ncol - 1),
1.f - (float)row / (nrow - 1)};
const float2 uv_c = {(float)(col + 1) / (ncol - 1),
1.f - (float)(row + 1) / (nrow - 1)};
const float2 uv_d = {(float)col / (ncol - 1),
1.f - (float)(row + 1) / (nrow - 1)};
const float2 uv_mid = {(float)(col + 0.5f) / (ncol - 1),
1.f - (float)(row + 0.5f) / (nrow - 1)};
append_tri(a, b, mid, uv_a, uv_b, uv_mid);
append_tri(b, c, mid, uv_b, uv_c, uv_mid);
append_tri(c, d, mid, uv_c, uv_d, uv_mid);
append_tri(d, a, mid, uv_d, uv_a, uv_mid);
}
}
// Build the left edge.
@@ -239,7 +252,9 @@ static void FillHeightFieldBuffer(MeshBuilder& builder, const mjModel* model,
const float3 b = get_pos(row + 1, 0);
const float3 c = {b.x, b.y, -sz[3]};
const float3 d = {a.x, a.y, -sz[3]};
append_quad(a, b, c, d);
const float2 uv_a = {0.f, 1.f - (float)row / (nrow - 1)};
const float2 uv_b = {0.f, 1.f - (float)(row + 1) / (nrow - 1)};
append_quad(a, b, c, d, uv_a, uv_b, uv_b, uv_a);
}
// Build the right edge.
for (int row = 0; row < nrow - 1; ++row) {
@@ -247,7 +262,9 @@ static void FillHeightFieldBuffer(MeshBuilder& builder, const mjModel* model,
const float3 b = get_pos(row, ncol - 1);
const float3 c = {b.x, b.y, -sz[3]};
const float3 d = {a.x, a.y, -sz[3]};
append_quad(a, b, c, d);
const float2 uv_a = {1.f, 1.f - (float)(row + 1) / (nrow - 1)};
const float2 uv_b = {1.f, 1.f - (float)row / (nrow - 1)};
append_quad(a, b, c, d, uv_a, uv_b, uv_b, uv_a);
}
// Build the front edge.
for (int col = 0; col < ncol - 1; ++col) {
@@ -255,7 +272,9 @@ static void FillHeightFieldBuffer(MeshBuilder& builder, const mjModel* model,
const float3 b = {a.x, a.y, -sz[3]};
const float3 d = get_pos(0, col + 1);
const float3 c = {d.x, d.y, -sz[3]};
append_quad(a, b, c, d);
const float2 uv_a = {(float)col / (ncol - 1), 1.f};
const float2 uv_d = {(float)(col + 1) / (ncol - 1), 1.f};
append_quad(a, b, c, d, uv_a, uv_a, uv_d, uv_d);
}
// Build the back edge.
for (int col = 0; col < ncol - 1; ++col) {
@@ -263,7 +282,9 @@ static void FillHeightFieldBuffer(MeshBuilder& builder, const mjModel* model,
const float3 b = {a.x, a.y, -sz[3]};
const float3 d = get_pos(nrow - 1, col);
const float3 c = {d.x, d.y, -sz[3]};
append_quad(a, b, c, d);
const float2 uv_a = {(float)(col + 1) / (ncol - 1), 0.f};
const float2 uv_d = {(float)col / (ncol - 1), 0.f};
append_quad(a, b, c, d, uv_a, uv_a, uv_d, uv_d);
}
// Build the base. We use the visualization quality as the size rather than
// the height field dimensions.
@@ -275,8 +296,13 @@ static void FillHeightFieldBuffer(MeshBuilder& builder, const mjModel* model,
const float x1 = sz[0] * ((col + 1) / base_width - 1.0f);
const float y0 = sz[1] * ((row + 0) / base_height - 1.0f);
const float y1 = sz[1] * ((row + 1) / base_height - 1.0f);
const float2 uv0 = {(col + 0) / (2.f * base_width),
1.f - (row + 0) / (2.f * base_height)};
const float2 uv1 = {(col + 1) / (2.f * base_width),
1.f - (row + 1) / (2.f * base_height)};
append_quad({x0, y0, -sz[3]}, {x0, y1, -sz[3]}, {x1, y1, -sz[3]},
{x1, y0, -sz[3]});
{x1, y0, -sz[3]}, {uv0.x, uv0.y}, {uv0.x, uv1.y},
{uv1.x, uv1.y}, {uv1.x, uv0.y});
}
}
}
@@ -303,7 +329,7 @@ static int CalculateHeightFieldVertexCount(const mjModel* model, int hfieldid) {
}
static bool HasUvs(const mjModel* model, int id, MeshType mesh_type) {
return mesh_type != MeshType::kHeightField &&
return mesh_type == MeshType::kHeightField ||
model->mesh_texcoordadr[id] >= 0;
}