Add heightfield geom type with Unity terrain, import not yet supported

This commit is contained in:
Bálint Hodossy
2023-12-04 13:55:38 +00:00
parent c0b7b9873d
commit 8ec1e4e2b8
5 changed files with 121 additions and 5 deletions
+30 -3
View File
@@ -34,6 +34,7 @@ public class MjcfGenerationContext {
private int _nuserSensor;
private int _numGeneratedNames = 0;
private Dictionary<Mesh, string> _meshAssets = new Dictionary<Mesh, string>();
private Dictionary<MjHeightFieldShape, string> _hFieldAssets = new Dictionary<MjHeightFieldShape, string>();
public void GenerateMjcf(XmlElement mjcf) {
GenerateConfigurationMjcf(mjcf);
@@ -50,6 +51,17 @@ public class MjcfGenerationContext {
}
return _meshAssets[mesh];
}
// Adds a mesh to the list of assets that will be added to the generated MJCF.
// Returns the unique name that identifies this new asset.
public string AddHeightFieldAsset(MjHeightFieldShape hField) {
if (!_hFieldAssets.ContainsKey(hField)) {
var uniqueName = $"hfield_{_numGeneratedNames}";
_numGeneratedNames++;
_hFieldAssets.Add(hField, uniqueName);
}
return _hFieldAssets[hField];
}
public string GenerateName(Component comp) {
var settings = MjGlobalSettings.Instance;
@@ -81,10 +93,15 @@ public class MjcfGenerationContext {
private void GenerateAssetsMjcf(XmlElement mjcf) {
var doc = mjcf.OwnerDocument;
var assetMjcf = (XmlElement)mjcf.AppendChild(doc.CreateElement("asset"));
foreach (var asset in _meshAssets) {
foreach (var meshAsset in _meshAssets) {
var meshMjcf = (XmlElement)assetMjcf.AppendChild(doc.CreateElement("mesh"));
meshMjcf.SetAttribute("name", asset.Value);
GenerateMeshMjcf(asset.Key, meshMjcf);
meshMjcf.SetAttribute("name", meshAsset.Value);
GenerateMeshMjcf(meshAsset.Key, meshMjcf);
}
foreach (var hFieldAsset in _hFieldAssets) {
var hFieldMjcf = (XmlElement)assetMjcf.AppendChild(doc.CreateElement("hfield"));
hFieldMjcf.SetAttribute("name", hFieldAsset.Value);
GenerateHeightFieldMjcf(hFieldAsset.Key, hFieldMjcf);
}
}
@@ -97,5 +114,15 @@ public class MjcfGenerationContext {
}
mjcf.SetAttribute("vertex", vertexPositionsStr.ToString());
}
private static void GenerateHeightFieldMjcf(MjHeightFieldShape hFieldComponent, XmlElement mjcf) {
mjcf.SetAttribute("nrow", "0");
mjcf.SetAttribute("ncol", "0");
mjcf.SetAttribute("content_type", "image/png");
mjcf.SetAttribute("file", hFieldComponent.FullHeightMapPath);
mjcf.SetAttribute("size", MjEngineTool.MakeLocaleInvariant($"{hFieldComponent.HeightMapScale.x*hFieldComponent.HeightMapHeight/2} {hFieldComponent.HeightMapScale.z * hFieldComponent.HeightMapWidth/2} {hFieldComponent.HeightMapScale.y*2} {hFieldComponent.terrain.transform.position.y}"));
}
}
}