Moving context writing to the individual semantic behaviours, adding extents.

parent 0dc3687b
This diff is collapsed.
......@@ -102,14 +102,7 @@ public class JSONHandler
jsonObject = sceneDict[name];
jsonObject["is_a"].Add(sb.is_a);
switch (sb)
{
case Entity entity: Write(jsonObject, entity); break;
case DesignedContainer designedContainer: Write(jsonObject, designedContainer); break;
default:
Debug.LogWarning($"No Write handler for SemanticBehaviour \"{sb.GetType().Name}\"");
break;
}
sb.WriteTo(jsonObject);
}
JSONObject metaObject = new JSONObject();
......@@ -119,47 +112,6 @@ public class JSONHandler
contextObject.Add("context", sceneArray);
return contextObject;
}
private static void Write(JSONObject jsonObject, Entity entity)
{
JSONObject transform = new JSONObject();
transform["position"] = entity.transform.position;
transform["orientation"] = entity.transform.rotation;
jsonObject["components"]["transform"] = transform;
Vector3 scale = entity.transform.lossyScale;
MeshFilter meshFilter = entity.GetComponent<MeshFilter>();
if (meshFilter != null)
{
Mesh mesh = meshFilter.sharedMesh;
Vector3 boundsCenterWorld = mesh.bounds.center;
boundsCenterWorld.Scale(scale);
Vector3 boundsExtentsWorld = mesh.bounds.center;
boundsExtentsWorld.Scale(scale);
Vector3 boundsSizeWorld = mesh.bounds.center;
boundsSizeWorld.Scale(scale);
JSONObject bounds = new JSONObject();
bounds["center"] = boundsCenterWorld;
bounds["extents"] = boundsExtentsWorld;
bounds["size"] = boundsSizeWorld;
jsonObject["components"]["bounds"] = bounds;
}
}
private static void Write(JSONObject jsonObject, DesignedContainer designedContainer)
{
JSONArray containment = new JSONArray();
foreach (Collider collider in designedContainer.containmentParts)
{
JSONObject part = new JSONObject();
part.Add("type", collider.GetType().Name);
part.Add("center", collider.bounds.center);
part.Add("size", collider.bounds.size);
part.Add("extents", collider.bounds.extents);
containment.Add(part);
}
jsonObject["components"]["containerVolume"] = containment;
}
}
}
\ No newline at end of file
using UnityEngine;
using SimpleJSON;
namespace dlu
{
[RequireComponent(typeof(InstanceIdentifier))]
public class SemanticBehaviour : MonoBehaviour
public abstract class SemanticBehaviour : MonoBehaviour
{
public string is_a;
public abstract void WriteTo(JSONObject jsonObject);
}
}
\ No newline at end of file
using System.Collections.Generic;
using UnityEngine;
using SimpleJSON;
namespace dlu
{
......@@ -12,6 +13,21 @@ public class DesignedContainer : SemanticBehaviour
{
is_a = "http://www.ease-crc.org/ont/EASE.owl#DesignedContainer";
}
public override void WriteTo(JSONObject jsonObject)
{
JSONArray containment = new JSONArray();
foreach (Collider collider in containmentParts)
{
JSONObject part = new JSONObject();
part.Add("type", collider.GetType().Name);
part.Add("center", collider.bounds.center);
part.Add("size", collider.bounds.size);
part.Add("extents", collider.bounds.extents);
containment.Add(part);
}
jsonObject["components"]["containerVolume"] = containment;
}
}
}
\ No newline at end of file
using UnityEngine;
using System.Collections.Generic;
using SimpleJSON;
namespace dlu
{
public class Entity : SemanticBehaviour
{
#if UNITY_EDITOR
[Header("Debug")]
public bool showExtents;
#endif
public Entity()
{
is_a = "http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#Entity";
}
public override void WriteTo(JSONObject jsonObject)
{
JSONObject transform = new JSONObject();
transform["position"] = this.transform.position;
transform["orientation"] = this.transform.rotation;
jsonObject["components"]["transform"] = transform;
Vector3 scale = this.transform.lossyScale;
MeshFilter meshFilter = this.GetComponent<MeshFilter>();
if (meshFilter != null)
{
Mesh mesh = meshFilter.sharedMesh;
JSONObject bounds = new JSONObject();
bounds["center"] = this.transform.TransformPoint(mesh.bounds.center);
bounds["extents"] = this.transform.TransformVector(mesh.bounds.extents);
bounds["size"] = this.transform.TransformVector(mesh.bounds.size);
jsonObject["components"]["bounds"] = bounds;
}
}
#if UNITY_EDITOR
void OnDrawGizmosSelected()
{
if (showExtents)
{
ShowExtents();
}
}
private void ShowExtents()
{
MeshFilter mf = GetComponent<MeshFilter>();
if (mf != null)
{
Vector3 center = mf.sharedMesh.bounds.center;
center = transform.TransformPoint(center);
Vector3 extents = mf.sharedMesh.bounds.extents;
extents = transform.TransformVector(extents);
// extents.Scale(transform.lossyScale);
float radius = 0.05f;
Gizmos.color = Color.red;
Gizmos.DrawSphere(center, radius);
Gizmos.color = Color.cyan;
Gizmos.DrawSphere(center + extents, radius);
Gizmos.DrawSphere(center - extents, radius);
}
}
#endif
}
}
\ No newline at end of file
m_EditorVersion: 2019.3.3f1
m_EditorVersionWithRevision: 2019.3.3f1 (7ceaae5f7503)
m_EditorVersion: 2019.3.6f1
m_EditorVersionWithRevision: 2019.3.6f1 (5c3fb0a11183)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment