Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unity stand alone build support + unity editor mesh duplicate #507

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion unity/Editor/Components/AbsoluteValuePropertyEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

Expand All @@ -24,3 +24,4 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
}
}
}
#endif
2 changes: 2 additions & 0 deletions unity/Editor/Components/MjActuatorEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#if UNITY_EDITOR

using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -197,3 +198,4 @@ private void EditMuscleParams(MjActuator.CustomParameters parameters) {
}
}
}
#endif
3 changes: 2 additions & 1 deletion unity/Editor/Components/MjGeomEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

Expand Down Expand Up @@ -78,3 +78,4 @@ private static void AddMatchingGeom(MenuCommand menuCommand) {
}
}
}
#endif
3 changes: 2 additions & 1 deletion unity/Editor/Components/MjHandles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -56,3 +56,4 @@ public static void LinearLimits(Vector3 origin, float lower, float upper, Vector
}
}
}
#endif
3 changes: 2 additions & 1 deletion unity/Editor/Components/MjHingeJointEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -39,3 +39,4 @@ public static void DrawHandles(MjHingeJoint joint) {
}
}
}
#endif
3 changes: 2 additions & 1 deletion unity/Editor/Components/MjShapeComponentEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -36,3 +36,4 @@ public override void OnInspectorGUI() {
}
}
}
#endif
3 changes: 2 additions & 1 deletion unity/Editor/Components/MjSlideJointEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -46,3 +46,4 @@ public static void DrawHandles(MjSlideJoint joint) {
}
}
}
#endif
3 changes: 2 additions & 1 deletion unity/Editor/Importer/MjImporterEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEngine;
Expand All @@ -30,3 +30,4 @@ public static void Apply() {
}
}
}
#endif
3 changes: 2 additions & 1 deletion unity/Editor/Importer/MjImporterWithAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -285,3 +285,4 @@ private void ResolveOrCreateMaterial(MeshRenderer renderer, XmlElement parentNod
}
}
}
#endif
3 changes: 2 additions & 1 deletion unity/Editor/Importer/StlMeshImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if UNITY_EDITOR
using System;
using System.IO;
using System.Linq;
Expand All @@ -34,3 +34,4 @@ public override void OnImportAsset(AssetImportContext ctx) {
}
}
}
#endif
3 changes: 2 additions & 1 deletion unity/Editor/Importer/StlMeshParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -125,3 +125,4 @@ public static byte[] SerializeBinary(Mesh mesh) {
}
}
}
#endif
11 changes: 10 additions & 1 deletion unity/Runtime/Bindings/MujocoBinaryRetriever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if UNITY_EDITOR


using System;
using System.IO;
using System.Runtime.InteropServices;
////using UnityEditor.PackageManager;
using UnityEngine;


using UnityEditor;
using UnityEditor.PackageManager;
using UnityEngine;



namespace Mujoco {
public class MujocoBinaryRetriever {
Expand Down Expand Up @@ -59,3 +67,4 @@ static void RegisteredPackagesEventHandler(
}
}
}
#endif
100 changes: 57 additions & 43 deletions unity/Runtime/Components/Shapes/MjMeshFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,75 @@
using System.Linq;
using UnityEngine;

namespace Mujoco {
namespace Mujoco
{

[RequireComponent(typeof(MjShapeComponent))]
[RequireComponent(typeof(MeshFilter))]
[ExecuteInEditMode]
public class MjMeshFilter : MonoBehaviour {
private MjShapeComponent _geom;
private Vector4 _shapeChangeStamp;
private MeshFilter _meshFilter;
[RequireComponent(typeof(MjShapeComponent))]
[RequireComponent(typeof(MeshFilter))]
[ExecuteInEditMode]
public class MjMeshFilter : MonoBehaviour
{
private MjShapeComponent _geom;
private Vector4 _shapeChangeStamp;
private MeshFilter _meshFilter;

protected void Awake() {
_geom = GetComponent<MjShapeComponent>();
_shapeChangeStamp = new Vector4(0, 0, 0, -1);
_meshFilter = GetComponent<MeshFilter>();
}
protected void Awake()
{
_geom = GetComponent<MjShapeComponent>();
_shapeChangeStamp = new Vector4(0, 0, 0, -1);
_meshFilter = GetComponent<MeshFilter>();
}

protected void Update() {
var currentChangeStamp = _geom.GetChangeStamp();
if ((_shapeChangeStamp - currentChangeStamp).magnitude <= 1e-3f) {
return;
}
protected void Update()
{
var currentChangeStamp = _geom.GetChangeStamp();
if ((_shapeChangeStamp - currentChangeStamp).magnitude <= 1e-3f)
{
return;
}

_shapeChangeStamp = currentChangeStamp;
Tuple<Vector3[], int[]> meshData = _geom.BuildMesh();
_shapeChangeStamp = currentChangeStamp;
Tuple<Vector3[], int[]> meshData = _geom.BuildMesh();

if (meshData == null) {
throw new ArgumentException("Unsupported geom shape detected");
}
if (meshData == null)
{
throw new ArgumentException("Unsupported geom shape detected");
}

DisposeCurrentMesh();
if (Application.isPlaying)
{
DisposeCurrentMesh();

var mesh = new Mesh();
// Name this mesh to easily track resources in Unity analysis tools.
mesh.name = $"Mujoco mesh for {gameObject.name}, id:{mesh.GetInstanceID()}";
_meshFilter.sharedMesh = mesh;
mesh.vertices = meshData.Item1;
mesh.triangles = meshData.Item2;
mesh.RecalculateNormals();
mesh.RecalculateTangents();
}
}
//DisposeCurrentMesh();

protected void OnDestroy() {
DisposeCurrentMesh();
}
var mesh = new Mesh();
// Name this mesh to easily track resources in Unity analysis tools.
mesh.name = $"Mujoco mesh for {gameObject.name}, id:{mesh.GetInstanceID()}";
_meshFilter.sharedMesh = mesh;
mesh.vertices = meshData.Item1;
mesh.triangles = meshData.Item2;
mesh.RecalculateNormals();
mesh.RecalculateTangents();
}

// Dynamically created meshes with no references are only disposed automatically on scene changes.
// This prevents resource leaks in case the host environment doesn't reload scenes.
private void DisposeCurrentMesh() {
if (_meshFilter.sharedMesh != null) {
protected void OnDestroy()
{
DisposeCurrentMesh();
}

// Dynamically created meshes with no references are only disposed automatically on scene changes.
// This prevents resource leaks in case the host environment doesn't reload scenes.
private void DisposeCurrentMesh()
{
if (_meshFilter.sharedMesh != null)
{
#if UNITY_EDITOR
DestroyImmediate(_meshFilter.sharedMesh);
DestroyImmediate(_meshFilter.sharedMesh);
#else
Destroy(_meshFilter.sharedMesh);
#endif
}
}
}
}
}
}