Reformat scripts to better follow guidelines

This commit is contained in:
Bálint Hodossy
2023-12-19 13:54:49 +00:00
parent 315a2f334f
commit d0b505c67b
2 changed files with 65 additions and 44 deletions
@@ -18,29 +18,34 @@ using System.Linq;
using System.Xml; using System.Xml;
using UnityEngine; using UnityEngine;
namespace Mujoco namespace Mujoco {
{
[Serializable] [Serializable]
public class MjHeightFieldShape : IMjShape public class MjHeightFieldShape : IMjShape {
{
[Tooltip("Terrain's heightmap should have a minimum value of zero (fully black).")] [Tooltip("Terrain's heightmap should have a minimum value of zero (fully black).")]
public Terrain terrain; public Terrain Terrain;
[Tooltip("The path, relative to Application.dataPath, where the heightmap data will be save in PNG format.")] [Tooltip("The path, relative to Application.dataPath, where the heightmap " +
public string heightMapExportPath; "data will be save in PNG format.")]
public string HeightMapExportPath;
public string FullHeightMapPath => Path.GetFullPath(Path.Combine(Application.dataPath, heightMapExportPath)); public string FullHeightMapPath => Path.GetFullPath(Path.Combine(Application.dataPath,
public int HeightMapWidth => terrain.terrainData.heightmapTexture.width; HeightMapExportPath));
public int HeightMapLength => terrain.terrainData.heightmapTexture.height;
public Vector3 HeightMapScale => terrain.terrainData.heightmapScale;
[Tooltip("At least this many frames will have to pass before the scene is rebuilt with an updated heightmap. Leave as 0 to not update the hfield during simulation. Increasing this can improve performance.")] public int HeightMapWidth => Terrain.terrainData.heightmapTexture.width;
public int HeightMapLength => Terrain.terrainData.heightmapTexture.height;
public Vector3 HeightMapScale => Terrain.terrainData.heightmapScale;
[Tooltip("At least this many frames will have to pass before the scene is rebuilt with an " +
"updated heightmap. Leave as 0 to not update the hfield during simulation. " +
"Increasing this can improve performance.")]
public int UpdateLimit; public int UpdateLimit;
private int updateCountdown;
private int _updateCountdown;
[HideInInspector] [HideInInspector]
public float MinimumHeight { get; private set; } public float MinimumHeight { get; private set; }
[HideInInspector] [HideInInspector]
public float MaximumHeight { get; private set; } public float MaximumHeight { get; private set; }
@@ -48,21 +53,31 @@ public class MjHeightFieldShape : IMjShape
public unsafe void ToMjcf(XmlElement mjcf, Transform transform) { public unsafe void ToMjcf(XmlElement mjcf, Transform transform) {
ExportHeightMap(); ExportHeightMap();
if(terrain.transform.parent != transform) Debug.LogWarning($"The terrain of heightfield {transform.name} needs to be parented to the Geom for proper rendering."); if (Terrain.transform.parent != transform)
Debug.LogWarning(
$"The terrain of heightfield {transform.name} needs to be parented to the Geom " +
"for proper rendering.");
else { else {
if((terrain.transform.localPosition - new Vector3(-HeightMapLength * HeightMapScale.x / 2, terrain.transform.localPosition.y, -HeightMapWidth * HeightMapScale.z / 2)).magnitude > 0.001) { if ((Terrain.transform.localPosition - new Vector3(-HeightMapLength * HeightMapScale.x / 2,
Debug.LogWarning($"Terrain of heightfield {transform.name} not aligned with geom. The terrain will be moved to accurately represent the simulated position."); Terrain.transform.localPosition.y,
-HeightMapWidth * HeightMapScale.z / 2)).magnitude > 0.001) {
Debug.LogWarning($"Terrain of heightfield {transform.name} not aligned with geom. The " +
" terrain will be moved to accurately represent the simulated position.");
} }
terrain.transform.localPosition = new Vector3(-HeightMapLength*HeightMapScale.x / 2, terrain.transform.localPosition.y, -HeightMapWidth*HeightMapScale.z / 2); Terrain.transform.localPosition = new Vector3(-HeightMapLength * HeightMapScale.x / 2,
Terrain.transform.localPosition.y,
-HeightMapWidth * HeightMapScale.z / 2);
} }
var scene = MjScene.Instance; var scene = MjScene.Instance;
var assetName = scene.GenerationContext.AddHeightFieldAsset(this); var assetName = scene.GenerationContext.AddHeightFieldAsset(this);
scene.postInitEvent += (_,_) => HeightFieldId = MujocoLib.mj_name2id(scene.Model, (int)MujocoLib.mjtObj.mjOBJ_HFIELD, assetName); scene.postInitEvent += (_, _) =>
HeightFieldId =
MujocoLib.mj_name2id(scene.Model, (int)MujocoLib.mjtObj.mjOBJ_HFIELD, assetName);
if(UpdateLimit>0){ if (UpdateLimit > 0) {
updateCountdown = UpdateLimit; _updateCountdown = UpdateLimit;
scene.preUpdateEvent += (_, _) => CountdownUpdateCondition(); scene.preUpdateEvent += (_, _) => CountdownUpdateCondition();
TerrainCallbacks.heightmapChanged += RebuildScene; TerrainCallbacks.heightmapChanged += RebuildScene;
} }
@@ -71,30 +86,33 @@ public class MjHeightFieldShape : IMjShape
} }
public void FromMjcf(XmlElement mjcf) { public void FromMjcf(XmlElement mjcf) {
} }
public void ExportHeightMap() { public void ExportHeightMap() {
RenderTexture.active = terrain.terrainData.heightmapTexture; RenderTexture.active = Terrain.terrainData.heightmapTexture;
Texture2D texture = new Texture2D(RenderTexture.active.width, RenderTexture.active.height); Texture2D texture = new Texture2D(RenderTexture.active.width, RenderTexture.active.height);
texture.ReadPixels(new Rect(0, 0, RenderTexture.active.width, RenderTexture.active.height), 0, 0); texture.ReadPixels(new Rect(0, 0, RenderTexture.active.width, RenderTexture.active.height),
MaximumHeight = texture.GetPixels().Select(c => c.r).Max()*HeightMapScale.y*2; 0,
var minimumHeight = texture.GetPixels().Select(c => c.r).Min()*HeightMapScale.y*2; 0);
if (minimumHeight > 0.0001) Debug.LogWarning("Due to assumptions in MuJoCo heightfields, terrains should have a minimum heightmap value of 0."); MaximumHeight = texture.GetPixels().Select(c => c.r).Max() * HeightMapScale.y * 2;
var minimumHeight = texture.GetPixels().Select(c => c.r).Min() * HeightMapScale.y * 2;
if (minimumHeight > 0.0001)
Debug.LogWarning("Due to assumptions in MuJoCo heightfields, terrains should have a " +
"minimum heightmap value of 0.");
RenderTexture.active = null; RenderTexture.active = null;
File.WriteAllBytes(FullHeightMapPath, texture.EncodeToPNG()); File.WriteAllBytes(FullHeightMapPath, texture.EncodeToPNG());
} }
public void CountdownUpdateCondition() { public void CountdownUpdateCondition() {
if(updateCountdown < 1) return; if (_updateCountdown < 1) return;
updateCountdown -= 1; _updateCountdown -= 1;
} }
public void RebuildScene(Terrain terrain, RectInt heightRegion, bool synched) { public void RebuildScene(Terrain terrain, RectInt heightRegion, bool synched) {
if(updateCountdown > 0) return; if (_updateCountdown > 0) return;
if(!Application.isPlaying || !MjScene.InstanceExists) return; if (!Application.isPlaying || !MjScene.InstanceExists) return;
MjScene.Instance.SceneRecreationAtLateUpdateRequested = true; MjScene.Instance.SceneRecreationAtLateUpdateRequested = true;
updateCountdown = UpdateLimit; _updateCountdown = UpdateLimit;
} }
public Vector4 GetChangeStamp() { public Vector4 GetChangeStamp() {
+13 -10
View File
@@ -34,7 +34,9 @@ public class MjcfGenerationContext {
private int _nuserSensor; private int _nuserSensor;
private int _numGeneratedNames = 0; private int _numGeneratedNames = 0;
private Dictionary<Mesh, string> _meshAssets = new Dictionary<Mesh, string>(); private Dictionary<Mesh, string> _meshAssets = new Dictionary<Mesh, string>();
private Dictionary<MjHeightFieldShape, string> _hFieldAssets = new Dictionary<MjHeightFieldShape, string>();
private Dictionary<MjHeightFieldShape, string> _hFieldAssets =
new Dictionary<MjHeightFieldShape, string>();
public void GenerateMjcf(XmlElement mjcf) { public void GenerateMjcf(XmlElement mjcf) {
GenerateConfigurationMjcf(mjcf); GenerateConfigurationMjcf(mjcf);
@@ -98,7 +100,7 @@ public class MjcfGenerationContext {
meshMjcf.SetAttribute("name", meshAsset.Value); meshMjcf.SetAttribute("name", meshAsset.Value);
GenerateMeshMjcf(meshAsset.Key, meshMjcf); GenerateMeshMjcf(meshAsset.Key, meshMjcf);
} }
foreach (var hFieldAsset in _hFieldAssets) { foreach (var hFieldAsset in _hFieldAssets) {
var hFieldMjcf = (XmlElement)assetMjcf.AppendChild(doc.CreateElement("hfield")); var hFieldMjcf = (XmlElement)assetMjcf.AppendChild(doc.CreateElement("hfield"));
hFieldMjcf.SetAttribute("name", hFieldAsset.Value); hFieldMjcf.SetAttribute("name", hFieldAsset.Value);
GenerateHeightFieldMjcf(hFieldAsset.Key, hFieldMjcf); GenerateHeightFieldMjcf(hFieldAsset.Key, hFieldMjcf);
@@ -120,15 +122,16 @@ public class MjcfGenerationContext {
mjcf.SetAttribute("ncol", "0"); mjcf.SetAttribute("ncol", "0");
mjcf.SetAttribute("content_type", "image/png"); mjcf.SetAttribute("content_type", "image/png");
mjcf.SetAttribute("file", hFieldComponent.FullHeightMapPath); mjcf.SetAttribute("file", hFieldComponent.FullHeightMapPath);
var baseHeight = hFieldComponent.terrain.transform.localPosition.y + hFieldComponent.MinimumHeight; var baseHeight = hFieldComponent.Terrain.transform.localPosition.y +
var heightRange = Mathf.Clamp(hFieldComponent.MaximumHeight - hFieldComponent.MinimumHeight, 0.00001f, Mathf.Infinity); hFieldComponent.MinimumHeight;
var heightRange = Mathf.Clamp(
hFieldComponent.MaximumHeight - hFieldComponent.MinimumHeight, 0.00001f, Mathf.Infinity);
mjcf.SetAttribute( mjcf.SetAttribute(
"size", "size",
MjEngineTool.MakeLocaleInvariant( MjEngineTool.MakeLocaleInvariant(
$@"{hFieldComponent.HeightMapScale.x*hFieldComponent.HeightMapLength/2} $@"{hFieldComponent.HeightMapScale.x * hFieldComponent.HeightMapLength / 2} {
{hFieldComponent.HeightMapScale.z * hFieldComponent.HeightMapWidth/2} hFieldComponent.HeightMapScale.z * hFieldComponent.HeightMapWidth / 2} {heightRange} {
{heightRange} baseHeight}"));
{baseHeight}"));
} }
} }
} }