Skip to content

Commit 9f674b0

Browse files
okmatijacopybara-github
authored andcommitted
Fix ground plane rendering when using filament backend
PiperOrigin-RevId: 848473849 Change-Id: I096b20c6a5538ff95396bdf09f84ed5f2d3015ed
1 parent 70bc7be commit 9f674b0

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

src/experimental/filament/filament/builtins.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <math/vec3.h>
2626
#include <math/vec4.h>
2727
#include <mujoco/mjmodel.h>
28+
#include <mujoco/mjvisualize.h>
2829
#include <mujoco/mujoco.h>
2930
#include "experimental/filament/filament/buffer_util.h"
3031
#include "experimental/filament/filament/vertex_util.h"
@@ -777,8 +778,7 @@ FilamentBuffers CreateLine(filament::Engine* engine, const mjModel* model) {
777778
}
778779

779780
FilamentBuffers CreatePlane(filament::Engine* engine, const mjModel* model) {
780-
const int num_quads = model->vis.quality.numquads;
781-
return CreateFromBuilder(engine, PlaneBuilder(num_quads));
781+
return CreateFromBuilder(engine, PlaneBuilder(mjMAXPLANEGRID));
782782
}
783783

784784
FilamentBuffers CreateTriangle(filament::Engine* engine, const mjModel* model) {

src/experimental/filament/filament/drawable.cc

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <math/vec3.h>
3030
#include <math/vec4.h>
3131
#include <utils/Entity.h>
32+
#include <mujoco/mjvisualize.h>
3233
#include <mujoco/mujoco.h>
3334
#include "experimental/filament/filament/buffer_util.h"
3435
#include "experimental/filament/filament/geom_util.h"
@@ -43,10 +44,6 @@ using filament::math::float3;
4344
using filament::math::float4;
4445
using filament::math::mat4;
4546

46-
// Planes with a 0-size dimension should be infinitely large. However, we
47-
// don't support that directly so we use a large but finite size instead.
48-
static constexpr float kInfinitePlaneFakeSize = 1000.0f;
49-
5047
// An arbitrary scale factor for arrows.
5148
static constexpr float kArrowScale = 1.f / 6.f;
5249
static constexpr float kArrowHeadSize = 1.75f;
@@ -69,6 +66,19 @@ static constexpr int kArrow2BottomCone = 2;
6966
static constexpr int kArrow2TopConeDisk = 3;
7067
static constexpr int kArrow2BottomConeDisk = 4;
7168

69+
// Returns the tile size for infinite plane texture alignment.
70+
// This is duplicated from engine_vis_visualize.c (re-center infinite plane)
71+
// to ensure UV scaling matches the re-centering increments.
72+
static float GetPlaneTileSize(const mjModel* model, int matid,
73+
float texrepeat) {
74+
if (matid >= 0 && texrepeat > 0) {
75+
return 2.0f / texrepeat;
76+
} else {
77+
const float zfar = model->vis.map.zfar * model->stat.extent;
78+
return 2.1f * zfar / (mjMAXPLANEGRID - 2);
79+
}
80+
}
81+
7282
Drawable::Drawable(ObjectManager* object_mgr, const mjvGeom& geom)
7383
: material_(object_mgr), renderables_(object_mgr->GetEngine()) {
7484
if (geom.category == mjCAT_DECOR) {
@@ -300,16 +310,20 @@ void Drawable::SetTransform(const mjvGeom& geom) {
300310
entity_transform *=
301311
mat4::scaling(float3{kArrowHeadSize, kArrowHeadSize, 1.0f});
302312
}
303-
} else if (geom.type == mjGEOM_PLANE) {
304-
// A plane with 0-size in any dimension is considered to be an infinite
305-
// plane, but we don't really support that so just scale it so that its
306-
// very large.
307-
if (size.x == 0 && size.y == 0) {
308-
size = float3(kInfinitePlaneFakeSize, kInfinitePlaneFakeSize, size.z);
309-
}
310313
}
311-
312-
if (geom.type != mjGEOM_MESH && geom.type != mjGEOM_HFIELD) {
314+
if (geom.type == mjGEOM_PLANE) {
315+
const bool is_infinite = !(size.x > 0 && size.y > 0);
316+
if (is_infinite) {
317+
// Infinite planes are scaled to match the tile size used by
318+
// re-centering in engine_vis_visualize.c.
319+
const float plane_scale = static_cast<float>(mjMAXPLANEGRID) / 2.0f;
320+
entity_transform *=
321+
mat4::scaling(float3{plane_scale, plane_scale, 1.0f});
322+
} else {
323+
// Regular planes are scaled by geom.size.
324+
entity_transform *= mat4::scaling(float3{size.x, size.y, 1.0f});
325+
}
326+
} else if (geom.type != mjGEOM_MESH && geom.type != mjGEOM_HFIELD) {
313327
entity_transform *= mat4::scaling(size);
314328
}
315329
tm.setTransform(tm.getInstance(entity), entity_transform);
@@ -456,13 +470,18 @@ void Drawable::UpdateMaterial(const mjvGeom& geom, bool use_segid_color) {
456470
params.uv_scale.y *= geom.size[1];
457471
}
458472
}
459-
if (geom.type == mjGEOM_PLANE) {
460-
if (geom.size[0] == 0) {
461-
params.uv_scale.x = kInfinitePlaneFakeSize;
462-
}
463-
if (geom.size[1] == 0) {
464-
params.uv_scale.y = kInfinitePlaneFakeSize;
465-
}
473+
const bool is_infinite_plane =
474+
geom.type == mjGEOM_PLANE && (geom.size[0] <= 0 || geom.size[1] <= 0);
475+
if (is_infinite_plane) {
476+
// Infinite planes are scaled to match the tile size used by
477+
// re-centering in engine_vis_visualize.c.
478+
const float plane_scale = static_cast<float>(mjMAXPLANEGRID) / 2.0f;
479+
const float tile_size_x =
480+
GetPlaneTileSize(model, geom.matid, params.tex_repeat.x);
481+
const float tile_size_y =
482+
GetPlaneTileSize(model, geom.matid, params.tex_repeat.y);
483+
params.uv_scale.x = 2.0f * plane_scale / tile_size_x;
484+
params.uv_scale.y = 2.0f * plane_scale / tile_size_y;
466485
}
467486

468487
params.uv_scale.x = 0.5f * params.uv_scale.x;

0 commit comments

Comments
 (0)