fix terrains, change slope_threshold in every subterrain for correct terrain generation.

This commit is contained in:
wertyuilife
2026-04-24 00:21:07 +08:00
parent 52b14b0c3f
commit ed50f647ac

View File

@@ -3,16 +3,40 @@ from __future__ import annotations
import numpy as np import numpy as np
import isaaclab.terrains as terrain_gen import isaaclab.terrains as terrain_gen
from isaaclab.terrains.terrain_generator import TerrainGenerator
from isaaclab.utils import configclass from isaaclab.utils import configclass
from isaaclab.terrains.height_field import hf_terrains from isaaclab.terrains.height_field import hf_terrains
from isaaclab.terrains.height_field.utils import height_field_to_mesh from isaaclab.terrains.height_field.utils import height_field_to_mesh
class PerSubTerrainSlopeThresholdGenerator(TerrainGenerator):
"""Terrain generator that lets each height-field sub-terrain override slope_threshold."""
def _get_terrain_mesh(self, difficulty, cfg):
override = getattr(cfg, "slope_threshold_override", None)
if override is None:
return super()._get_terrain_mesh(difficulty, cfg)
original_slope_threshold = getattr(cfg, "slope_threshold", None)
cfg.slope_threshold = override
try:
return super()._get_terrain_mesh(difficulty, cfg)
finally:
cfg.slope_threshold = original_slope_threshold
def with_slope_threshold(sub_terrain_cfg, slope_threshold: float | None):
"""Attach a per-sub-terrain slope-threshold override to a terrain config."""
sub_terrain_cfg.slope_threshold_override = slope_threshold
return sub_terrain_cfg
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Default RobotLab terrain setup # Default RobotLab terrain setup
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
DEFAULT_TERRAIN_CFG = terrain_gen.TerrainGeneratorCfg( DEFAULT_TERRAIN_CFG = terrain_gen.TerrainGeneratorCfg(
class_type=PerSubTerrainSlopeThresholdGenerator,
size=(8.0, 8.0), size=(8.0, 8.0),
border_width=25.0, border_width=25.0,
num_rows=10, num_rows=10,
@@ -107,65 +131,92 @@ class RoughSlopeTerrainCfg(terrain_gen.HfPyramidSlopedTerrainCfg):
TERRAIN_CFG = terrain_gen.TerrainGeneratorCfg( TERRAIN_CFG = terrain_gen.TerrainGeneratorCfg(
class_type=PerSubTerrainSlopeThresholdGenerator,
size=(8.0, 8.0), size=(8.0, 8.0),
border_width=25.0, border_width=25.0,
num_rows=10, num_rows=10,
num_cols=20, num_cols=20,
horizontal_scale=0.1, horizontal_scale=0.1,
vertical_scale=0.005, vertical_scale=0.005,
slope_threshold=2.0, # slope correction = 0.75 ~ 36.9 degrees by default,
# but recommended to set for each terrain type separately using with_slope_threshold
slope_threshold=0.75,
use_cache=False, use_cache=False,
sub_terrains={ sub_terrains={
"wave": WaveTerrainCfg( "wave": with_slope_threshold(
proportion=0.05, WaveTerrainCfg(
border_width=0.5, proportion=0.05,
border_width=0.5,
),
10.0, # effectively disable slope correction for wave terrain
), ),
"slope_up": terrain_gen.HfInvertedPyramidSlopedTerrainCfg( "slope_up": with_slope_threshold(
proportion=0.10, terrain_gen.HfInvertedPyramidSlopedTerrainCfg(
slope_range=(0.1, 0.568), proportion=0.10,
platform_width=3.0, slope_range=(0.1, 0.568),
border_width=0.5, platform_width=3.0,
border_width=0.5,
),
10.0, # effectively disable slope correction for slope_up terrain
), ),
"slope_down": terrain_gen.HfPyramidSlopedTerrainCfg( "slope_down": with_slope_threshold(
proportion=0.10, terrain_gen.HfPyramidSlopedTerrainCfg(
slope_range=(0.1, 0.568), proportion=0.10,
platform_width=3.0, slope_range=(0.1, 0.568),
border_width=0.5, platform_width=3.0,
border_width=0.5,
),
10.0, # effectively disable slope correction for slope_down terrain
), ),
"rough_slope": RoughSlopeTerrainCfg( "rough_slope": with_slope_threshold(
proportion=0.05, RoughSlopeTerrainCfg(
border_width=0.5, proportion=0.05,
border_width=0.5,
),
10.0, # effectively disable slope correction for rough_slope terrain
), ),
"stairs_up": terrain_gen.HfInvertedPyramidStairsTerrainCfg( "stairs_up": with_slope_threshold(
proportion=0.25, terrain_gen.HfInvertedPyramidStairsTerrainCfg(
step_height_range=(0.05, 0.257), proportion=0.25,
step_width=0.31, step_height_range=(0.05, 0.257),
platform_width=3.0, step_width=0.31,
border_width=0.5, platform_width=3.0,
border_width=0.5,
),
0.25, # enable slope correction for rough_slope terrain by 14.0 degrees, which is recommended for stairs terrain
), ),
"stairs_down": terrain_gen.HfPyramidStairsTerrainCfg( "stairs_down": with_slope_threshold(
proportion=0.10, terrain_gen.HfPyramidStairsTerrainCfg(
step_height_range=(0.05, 0.257), proportion=0.10,
step_width=0.31, step_height_range=(0.05, 0.257),
platform_width=3.0, step_width=0.31,
border_width=0.5, platform_width=3.0,
border_width=0.5,
),
0.25, # enable slope correction for rough_slope terrain by 14.0 degrees, which is recommended for stairs terrain
), ),
"obstacles": terrain_gen.HfDiscreteObstaclesTerrainCfg( "obstacles": with_slope_threshold(
proportion=0.20, terrain_gen.HfDiscreteObstaclesTerrainCfg(
obstacle_width_range=(1.0, 2.0), proportion=0.20,
obstacle_height_range=(0.05, 0.275), obstacle_width_range=(1.0, 2.0),
num_obstacles=20, obstacle_height_range=(0.05, 0.275),
platform_width=3.0, num_obstacles=20,
border_width=0.5, platform_width=3.0,
border_width=0.5,
),
0.25, # enable slope correction for rough_slope terrain by 14.0 degrees
), ),
"stepping_stones": terrain_gen.HfSteppingStonesTerrainCfg( "stepping_stones": with_slope_threshold(
proportion=0.0, terrain_gen.HfSteppingStonesTerrainCfg(
stone_height_max=0.0, proportion=0.0,
stone_width_range=(0.075, 1.575), stone_height_max=0.0,
stone_distance_range=(0.05, 0.10), stone_width_range=(0.075, 1.575),
holes_depth=-10.0, stone_distance_range=(0.05, 0.10),
platform_width=4.0, holes_depth=-10.0,
border_width=0.5, platform_width=4.0,
border_width=0.5,
),
0.25, # enable slope correction for rough_slope terrain by 14.0 degrees
), ),
"gap": terrain_gen.MeshGapTerrainCfg( "gap": terrain_gen.MeshGapTerrainCfg(
proportion=0.0, proportion=0.0,