commit a0e5dcd159e7f949eb7db1941a510905f488f378 Author: Matteo Cominetti Date: Mon Dec 28 20:08:51 2020 +0000 feat: adds receving logic diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6bb293 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.meta diff --git a/ConverterUnity.Geometry.cs b/ConverterUnity.Geometry.cs new file mode 100644 index 0000000..d3ee2b9 --- /dev/null +++ b/ConverterUnity.Geometry.cs @@ -0,0 +1,186 @@ +using Objects.Geometry; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; +using Mesh = Objects.Geometry.Mesh; + +namespace Objects.Converter.Unity +{ + public partial class ConverterUnity + { + /// + /// + /// + /// + /// + /// + /// + public Vector3 PointByCoordinates(double x, double y, double z) + { + // switch y and z + return new Vector3((float)x, (float)z, (float)y); + } + + /// + /// + /// + /// + /// + public Vector3 ArrayToPoint(double[] ptValues) + { + double x = ptValues[0]; + double y = ptValues[1]; + double z = ptValues[2]; + // switch y and z + return new Vector3((float)x, (float)z, (float)y); + } + + /// + /// + /// + /// + /// + public Vector3[] ArrayToPoints(IEnumerable arr) + { + if (arr.Count() % 3 != 0) throw new Exception("Array malformed: length%3 != 0."); + + Vector3[] points = new Vector3[arr.Count() / 3]; + var asArray = arr.ToArray(); + for (int i = 2, k = 0; i < arr.Count(); i += 3) + points[k++] = PointByCoordinates(asArray[i - 2], asArray[i - 1], asArray[i]); + + return points; + } + + + + /// + /// + /// + /// + /// + public Point PointToSpeckle(UnityPoint obj) + { + Vector3 p = obj.point; + + //switch y and z + return new Point(p.x, p.z, p.y); + } + + /// + /// + /// + /// + /// + public UnityPoint PointToNative(Point point) + { + + Vector3 newPt = ArrayToPoint(point.value.ToArray()); + return new UnityPoint(point.speckle_type, newPt); + } + + /// + /// + /// + /// + /// + public UnityPolyline LineToNative(Line line) + { + Vector3[] points = ArrayToPoints(line.value); + + if (points.Length == 0) return null; + + return new UnityPolyline(line.speckle_type, points); + } + + /// + /// + /// + /// + /// + public UnityPolyline PolylineToNative(Polyline polyline) + { + Vector3[] points = ArrayToPoints(polyline.value); + + if (points.Length == 0) return null; + + + return new UnityPolyline(polyline.speckle_type, points); + } + + /// + /// + /// + /// + /// + public UnityPolyline CurveToNative(Curve curve) + { + Vector3[] points = ArrayToPoints(curve.displayValue.value); + + if (points.Length == 0) return null; + + + return new UnityPolyline(curve.speckle_type, points); + } + + + /// + /// + /// + /// + /// + public UnityMesh MeshToNative(Mesh speckleMesh) + { + if (speckleMesh.vertices.Count == 0 || speckleMesh.faces.Count == 0) + { + return null; + } + + //convert speckleMesh.faces into triangle array + List tris = new List(); + int i = 0; + while (i < speckleMesh.faces.Count) + { + if (speckleMesh.faces[i] == 0) + { + //Triangles + tris.Add(speckleMesh.faces[i + 1]); + tris.Add(speckleMesh.faces[i + 3]); + tris.Add(speckleMesh.faces[i + 2]); + i += 4; + } + else + { + //Quads to triangles + tris.Add(speckleMesh.faces[i + 1]); + tris.Add(speckleMesh.faces[i + 3]); + tris.Add(speckleMesh.faces[i + 2]); + + tris.Add(speckleMesh.faces[i + 3]); + tris.Add(speckleMesh.faces[i + 1]); + tris.Add(speckleMesh.faces[i + 4]); + + i += 5; + } + } + + return new UnityMesh(speckleMesh.speckle_type, ArrayToPoints(speckleMesh.vertices), tris.ToArray()); + } + + /// + /// + /// + /// + /// + //public static UnityMesh ToNative(this Brep brep) + //{ + // Mesh speckleMesh = brep.displayValue; + // return MeshToNative(speckleMesh); + //} + + + } +} diff --git a/ConverterUnity.cs b/ConverterUnity.cs new file mode 100644 index 0000000..99dd010 --- /dev/null +++ b/ConverterUnity.cs @@ -0,0 +1,105 @@ +using Objects.Geometry; +using Speckle.Core.Kits; +using Speckle.Core.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Objects.Converter.Unity +{ + public partial class ConverterUnity : ISpeckleConverter + { + #region implemented methods + + public string Description => "Default Speckle Kit for Unity"; + public string Name => nameof(ConverterUnity); + public string Author => "Speckle"; + public string WebsiteOrEmail => "https://speckle.systems"; + + public IEnumerable GetServicedApplications() => new string[] { Applications.Other }; //TODO: add unity + + public HashSet ConversionErrors { get; private set; } = new HashSet(); + + + public List ContextObjects { get; set; } = new List(); + public void SetContextDocument(object doc) => throw new NotImplementedException(); + + public void SetContextObjects(List objects) => ContextObjects = objects; + + public void SetPreviousContextObjects(List objects) => throw new NotImplementedException(); + + public Base ConvertToSpeckle(object @object) + { + switch (@object) + { + + + default: + throw new NotSupportedException(); + } + } + + public object ConvertToNative(Base @object) + { + switch (@object) + { + case Point o: + return PointToNative(o); + case Line o: + return LineToNative(o); + case Polyline o: + return PolylineToNative(o); + case Curve o: + return CurveToNative(o); + case Mesh o: + return MeshToNative(o); + + default: + throw new NotSupportedException(); + } + } + + public List ConvertToSpeckle(List objects) + { + return objects.Select(x => ConvertToSpeckle(x)).ToList(); + } + + public List ConvertToNative(List objects) + { + return objects.Select(x => ConvertToNative(x)).ToList(); ; + } + + public bool CanConvertToSpeckle(object @object) + { + switch (@object) + { + + default: + return false; + } + } + + public bool CanConvertToNative(Base @object) + { + switch (@object) + { + case Point _: + return true; + case Line _: + return true; + case Polyline _: + return true; + case Curve _: + return true; + case Mesh _: + return true; + default: + return false; + } + } + + #endregion implemented methods + } +} diff --git a/ObjectsUnity.cs b/ObjectsUnity.cs new file mode 100644 index 0000000..fe2b501 --- /dev/null +++ b/ObjectsUnity.cs @@ -0,0 +1,248 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace Objects.Geometry +{ + + + /// + /// Base definition for all rendered stream objects. Any Speckle object that needs to be + /// displayed with a game object should inherit from this class. + /// + public abstract class UnityGeometry + { + /// + /// The gameobject that will be displayed. The Transform parent will be assigned + /// by the SpeckleUnityReceiver. + /// + public GameObject gameObject; + + /// + /// + /// + public Renderer renderer; + + /// + /// + /// + public UnityGeometry() + { + gameObject = new GameObject(); + + } + } + + /// + /// A stream object represented as a gameobject with a MeshRenderer. Also adds a + /// MeshCollider to the object. The material is assigned by the SpeckleUnityReceiver. + /// + public class UnityMesh : UnityGeometry + { + /// + /// + /// + public static bool RecentreMeshTransforms = false; + + /// + /// + /// + public UnityEngine.Mesh mesh; + + /// + /// + /// + public MeshRenderer meshRenderer; + + /// + /// + /// + /// + /// + /// + public UnityMesh(string type, Vector3[] verts, int[] tris) : base() + { + gameObject.name = type; + + mesh = gameObject.AddComponent().mesh; + renderer = meshRenderer = gameObject.AddComponent(); + + if (verts.Length >= 65535) + mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; + + // center transform pivot according to the bounds of the model + if (RecentreMeshTransforms) + { + Bounds meshBounds = new Bounds(); + meshBounds.center = verts[0]; + + for (int i = 1; i < verts.Length; i++) + { + meshBounds.Encapsulate(verts[i]); + } + + gameObject.transform.position = meshBounds.center; + + // offset mesh vertices + for (int i = 0; i < verts.Length; i++) + { + verts[i] -= meshBounds.center; + } + } + + // assign mesh properties + mesh.vertices = verts; + mesh.triangles = tris; + + mesh.RecalculateNormals(); + mesh.RecalculateTangents(); + + //generate uvs doesn't work as intended. Leaving out for now + //GenerateUVs (ref mesh); + + //Add mesh collider + MeshCollider mc = gameObject.AddComponent(); + mc.sharedMesh = mesh; + } + + /// + /// + /// + /// + /// + protected void GenerateUVs(ref UnityEngine.Mesh mesh) + { + Vector3 p = Vector3.up; + Vector3 u = Vector3.Cross(p, Vector3.forward); + if (Vector3.Dot(u, u) < 0.001f) + { + u = Vector3.right; + } + else + { + u = Vector3.Normalize(u); + } + + Vector3 v = Vector3.Normalize(Vector3.Cross(p, u)); + Vector3[] vertexs = mesh.vertices; + int[] tris = mesh.triangles; + Vector2[] uvs = new Vector2[vertexs.Length]; + + for (int i = 0; i < tris.Length; i += 3) + { + + Vector3 a = vertexs[tris[i]]; + Vector3 b = vertexs[tris[i + 1]]; + Vector3 c = vertexs[tris[i + 2]]; + Vector3 side1 = b - a; + Vector3 side2 = c - a; + Vector3 N = Vector3.Cross(side1, side2); + + N = new Vector3(Mathf.Abs(N.normalized.x), Mathf.Abs(N.normalized.y), Mathf.Abs(N.normalized.z)); + + + + if (N.x > N.y && N.x > N.z) + { + uvs[tris[i]] = new Vector2(vertexs[tris[i]].z, vertexs[tris[i]].y); + uvs[tris[i + 1]] = new Vector2(vertexs[tris[i + 1]].z, vertexs[tris[i + 1]].y); + uvs[tris[i + 2]] = new Vector2(vertexs[tris[i + 2]].z, vertexs[tris[i + 2]].y); + } + else if (N.y > N.x && N.y > N.z) + { + uvs[tris[i]] = new Vector2(vertexs[tris[i]].x, vertexs[tris[i]].z); + uvs[tris[i + 1]] = new Vector2(vertexs[tris[i + 1]].x, vertexs[tris[i + 1]].z); + uvs[tris[i + 2]] = new Vector2(vertexs[tris[i + 2]].x, vertexs[tris[i + 2]].z); + } + else if (N.z > N.x && N.z > N.y) + { + uvs[tris[i]] = new Vector2(vertexs[tris[i]].x, vertexs[tris[i]].y); + uvs[tris[i + 1]] = new Vector2(vertexs[tris[i + 1]].x, vertexs[tris[i + 1]].y); + uvs[tris[i + 2]] = new Vector2(vertexs[tris[i + 2]].x, vertexs[tris[i + 2]].y); + } + + } + + mesh.uv = uvs; + } + } + + /// + /// Used to display lines, curves, or polylines as a game object with a LineRenderer. + /// The material is assigned by the SpeckleUnityReceiver. + /// + public class UnityPolyline : UnityGeometry + { + /// + /// + /// + public static float LineWidth = 1; + + /// + /// + /// + public LineRenderer lineRenderer; + + /// + /// + /// + /// + /// + public UnityPolyline(string type, Vector3[] points) : base() + { + gameObject.name = type; + + //create line renderer + renderer = lineRenderer = gameObject.AddComponent(); + + lineRenderer.positionCount = points.Length; + lineRenderer.SetPositions(points); + lineRenderer.numCornerVertices = lineRenderer.numCapVertices = 8; + lineRenderer.startWidth = lineRenderer.endWidth = LineWidth; + } + } + + + /// + /// Display Point. Uses a line renderer for display. The material is assigned by the + /// SpeckleUnityReceiver. + /// + public class UnityPoint : UnityGeometry + { + /// + /// + /// + public static float PointDiameter = 1; + + /// + /// + /// + public Vector3 point; + + /// + /// + /// + public LineRenderer lineRenderer; + + /// + /// + /// + /// + /// + public UnityPoint(string type, Vector3 point) : base() + { + gameObject.name = type; + + this.point = point; + + //create line renderer + renderer = lineRenderer = gameObject.AddComponent(); + lineRenderer.SetPositions(new Vector3[2] { point, point }); + lineRenderer.numCornerVertices = lineRenderer.numCapVertices = 8; + lineRenderer.startWidth = lineRenderer.endWidth = PointDiameter; + } + } +} diff --git a/Plugins/Core/GraphQL.Client.Abstractions.Websocket.dll b/Plugins/Core/GraphQL.Client.Abstractions.Websocket.dll new file mode 100644 index 0000000..569e136 Binary files /dev/null and b/Plugins/Core/GraphQL.Client.Abstractions.Websocket.dll differ diff --git a/Plugins/Core/GraphQL.Client.Abstractions.dll b/Plugins/Core/GraphQL.Client.Abstractions.dll new file mode 100644 index 0000000..1e19108 Binary files /dev/null and b/Plugins/Core/GraphQL.Client.Abstractions.dll differ diff --git a/Plugins/Core/GraphQL.Client.Serializer.Newtonsoft.dll b/Plugins/Core/GraphQL.Client.Serializer.Newtonsoft.dll new file mode 100644 index 0000000..16bb909 Binary files /dev/null and b/Plugins/Core/GraphQL.Client.Serializer.Newtonsoft.dll differ diff --git a/Plugins/Core/GraphQL.Client.dll b/Plugins/Core/GraphQL.Client.dll new file mode 100644 index 0000000..125256c Binary files /dev/null and b/Plugins/Core/GraphQL.Client.dll differ diff --git a/Plugins/Core/GraphQL.Primitives.dll b/Plugins/Core/GraphQL.Primitives.dll new file mode 100644 index 0000000..6460f6e Binary files /dev/null and b/Plugins/Core/GraphQL.Primitives.dll differ diff --git a/Plugins/Core/Newtonsoft.Json.dll b/Plugins/Core/Newtonsoft.Json.dll new file mode 100644 index 0000000..b501fb6 Binary files /dev/null and b/Plugins/Core/Newtonsoft.Json.dll differ diff --git a/Plugins/Core/Piwik.Tracker.dll b/Plugins/Core/Piwik.Tracker.dll new file mode 100644 index 0000000..7d5737f Binary files /dev/null and b/Plugins/Core/Piwik.Tracker.dll differ diff --git a/Plugins/Core/SQLite.Interop.dll b/Plugins/Core/SQLite.Interop.dll new file mode 100644 index 0000000..9a745b7 Binary files /dev/null and b/Plugins/Core/SQLite.Interop.dll differ diff --git a/Plugins/Core/Sentry.PlatformAbstractions.dll b/Plugins/Core/Sentry.PlatformAbstractions.dll new file mode 100644 index 0000000..f341473 Binary files /dev/null and b/Plugins/Core/Sentry.PlatformAbstractions.dll differ diff --git a/Plugins/Core/Sentry.Protocol.dll b/Plugins/Core/Sentry.Protocol.dll new file mode 100644 index 0000000..d711ce7 Binary files /dev/null and b/Plugins/Core/Sentry.Protocol.dll differ diff --git a/Plugins/Core/Sentry.dll b/Plugins/Core/Sentry.dll new file mode 100644 index 0000000..0ac7834 Binary files /dev/null and b/Plugins/Core/Sentry.dll differ diff --git a/Plugins/Core/SpeckleCore2.dll b/Plugins/Core/SpeckleCore2.dll new file mode 100644 index 0000000..d3ad258 Binary files /dev/null and b/Plugins/Core/SpeckleCore2.dll differ diff --git a/Plugins/Core/SpeckleCore3.dll b/Plugins/Core/SpeckleCore3.dll new file mode 100644 index 0000000..d3ad258 Binary files /dev/null and b/Plugins/Core/SpeckleCore3.dll differ diff --git a/Plugins/Core/System.Data.SQLite.dll b/Plugins/Core/System.Data.SQLite.dll new file mode 100644 index 0000000..685ddc9 Binary files /dev/null and b/Plugins/Core/System.Data.SQLite.dll differ diff --git a/Plugins/Core/System.Reactive.dll b/Plugins/Core/System.Reactive.dll new file mode 100644 index 0000000..f13457c Binary files /dev/null and b/Plugins/Core/System.Reactive.dll differ diff --git a/Plugins/Core/System.Runtime.CompilerServices.Unsafe.dll b/Plugins/Core/System.Runtime.CompilerServices.Unsafe.dll new file mode 100644 index 0000000..3156239 Binary files /dev/null and b/Plugins/Core/System.Runtime.CompilerServices.Unsafe.dll differ diff --git a/Plugins/Core/System.Threading.Tasks.Extensions.dll b/Plugins/Core/System.Threading.Tasks.Extensions.dll new file mode 100644 index 0000000..e059050 Binary files /dev/null and b/Plugins/Core/System.Threading.Tasks.Extensions.dll differ diff --git a/Plugins/Objects/Objects.dll b/Plugins/Objects/Objects.dll new file mode 100644 index 0000000..7188d00 Binary files /dev/null and b/Plugins/Objects/Objects.dll differ diff --git a/Plugins/ServerTransport/ServerTransport.dll b/Plugins/ServerTransport/ServerTransport.dll new file mode 100644 index 0000000..c687a07 Binary files /dev/null and b/Plugins/ServerTransport/ServerTransport.dll differ diff --git a/Scenes/SampleScene.unity b/Scenes/SampleScene.unity new file mode 100644 index 0000000..a2c8fca --- /dev/null +++ b/Scenes/SampleScene.unity @@ -0,0 +1,316 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +OcclusionCullingSettings: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: 0.25 + backfaceThreshold: 100 + m_SceneGUID: 00000000000000000000000000000000 + m_OcclusionCullingData: {fileID: 0} +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 9 + m_Fog: 0 + m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} + m_FogMode: 3 + m_FogDensity: 0.01 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} + m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} + m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 3 + m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} + m_SkyboxMaterial: {fileID: 0} + m_HaloStrength: 0.5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} + m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} + m_UseRadianceAmbientProbe: 0 +--- !u!157 &3 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 12 + m_GIWorkflowMode: 1 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 0 + m_EnableRealtimeLightmaps: 0 + m_LightmapEditorSettings: + serializedVersion: 12 + m_Resolution: 2 + m_BakeResolution: 40 + m_AtlasSize: 1024 + m_AO: 0 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_ExtractAmbientOcclusion: 0 + m_Padding: 2 + m_LightmapParameters: {fileID: 0} + m_LightmapsBakeMode: 1 + m_TextureCompression: 1 + m_FinalGather: 0 + m_FinalGatherFiltering: 1 + m_FinalGatherRayCount: 256 + m_ReflectionCompression: 2 + m_MixedBakeMode: 2 + m_BakeBackend: 0 + m_PVRSampling: 1 + m_PVRDirectSampleCount: 32 + m_PVRSampleCount: 500 + m_PVRBounces: 2 + m_PVREnvironmentSampleCount: 500 + m_PVREnvironmentReferencePointCount: 2048 + m_PVRFilteringMode: 2 + m_PVRDenoiserTypeDirect: 0 + m_PVRDenoiserTypeIndirect: 0 + m_PVRDenoiserTypeAO: 0 + m_PVRFilterTypeDirect: 0 + m_PVRFilterTypeIndirect: 0 + m_PVRFilterTypeAO: 0 + m_PVREnvironmentMIS: 0 + m_PVRCulling: 1 + m_PVRFilteringGaussRadiusDirect: 1 + m_PVRFilteringGaussRadiusIndirect: 5 + m_PVRFilteringGaussRadiusAO: 2 + m_PVRFilteringAtrousPositionSigmaDirect: 0.5 + m_PVRFilteringAtrousPositionSigmaIndirect: 2 + m_PVRFilteringAtrousPositionSigmaAO: 1 + m_ExportTrainingData: 0 + m_TrainingDataDestination: TrainingData + m_LightProbeSampleCountMultiplier: 4 + m_LightingDataAsset: {fileID: 0} + m_LightingSettings: {fileID: 0} +--- !u!196 &4 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 2 + agentTypeID: 0 + agentRadius: 0.5 + agentHeight: 2 + agentSlope: 45 + agentClimb: 0.4 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + minRegionArea: 2 + manualCellSize: 0 + cellSize: 0.16666667 + manualTileSize: 0 + tileSize: 256 + accuratePlacement: 0 + maxJobWorkers: 0 + preserveTilesOutsideBounds: 0 + debug: + m_Flags: 0 + m_NavMeshData: {fileID: 0} +--- !u!1 &519420028 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 519420032} + - component: {fileID: 519420031} + - component: {fileID: 519420029} + m_Layer: 0 + m_Name: Main Camera + m_TagString: MainCamera + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!81 &519420029 +AudioListener: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 519420028} + m_Enabled: 1 +--- !u!20 &519420031 +Camera: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 519420028} + m_Enabled: 1 + serializedVersion: 2 + m_ClearFlags: 2 + m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} + m_projectionMatrixMode: 1 + m_GateFitMode: 2 + m_FOVAxisMode: 0 + m_SensorSize: {x: 36, y: 24} + m_LensShift: {x: 0, y: 0} + m_FocalLength: 50 + m_NormalizedViewPortRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 + near clip plane: 0.3 + far clip plane: 1000 + field of view: 60 + orthographic: 1 + orthographic size: 5 + m_Depth: -1 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingPath: -1 + m_TargetTexture: {fileID: 0} + m_TargetDisplay: 0 + m_TargetEye: 0 + m_HDR: 1 + m_AllowMSAA: 0 + m_AllowDynamicResolution: 0 + m_ForceIntoRT: 0 + m_OcclusionCulling: 0 + m_StereoConvergence: 10 + m_StereoSeparation: 0.022 +--- !u!4 &519420032 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 519420028} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: -10} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1441585350 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1441585355} + - component: {fileID: 1441585354} + - component: {fileID: 1441585353} + - component: {fileID: 1441585352} + - component: {fileID: 1441585351} + m_Layer: 0 + m_Name: Cube + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1441585351 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1441585350} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9f65af9eea4f24d488968efcdb7cf7d4, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!65 &1441585352 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1441585350} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + serializedVersion: 2 + m_Size: {x: 1, y: 1, z: 1} + m_Center: {x: 0, y: 0, z: 0} +--- !u!23 &1441585353 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1441585350} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 2 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_AdditionalVertexStreams: {fileID: 0} +--- !u!33 &1441585354 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1441585350} + m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} +--- !u!4 &1441585355 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1441585350} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Scenes/main.unity b/Scenes/main.unity new file mode 100644 index 0000000..c618979 --- /dev/null +++ b/Scenes/main.unity @@ -0,0 +1,316 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +OcclusionCullingSettings: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: 0.25 + backfaceThreshold: 100 + m_SceneGUID: 00000000000000000000000000000000 + m_OcclusionCullingData: {fileID: 0} +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 9 + m_Fog: 0 + m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} + m_FogMode: 3 + m_FogDensity: 0.01 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} + m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} + m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 3 + m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} + m_SkyboxMaterial: {fileID: 0} + m_HaloStrength: 0.5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} + m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} + m_UseRadianceAmbientProbe: 0 +--- !u!157 &3 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 12 + m_GIWorkflowMode: 1 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 0 + m_EnableRealtimeLightmaps: 0 + m_LightmapEditorSettings: + serializedVersion: 12 + m_Resolution: 2 + m_BakeResolution: 40 + m_AtlasSize: 1024 + m_AO: 0 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_ExtractAmbientOcclusion: 0 + m_Padding: 2 + m_LightmapParameters: {fileID: 0} + m_LightmapsBakeMode: 1 + m_TextureCompression: 1 + m_FinalGather: 0 + m_FinalGatherFiltering: 1 + m_FinalGatherRayCount: 256 + m_ReflectionCompression: 2 + m_MixedBakeMode: 2 + m_BakeBackend: 1 + m_PVRSampling: 1 + m_PVRDirectSampleCount: 32 + m_PVRSampleCount: 512 + m_PVRBounces: 2 + m_PVREnvironmentSampleCount: 256 + m_PVREnvironmentReferencePointCount: 2048 + m_PVRFilteringMode: 1 + m_PVRDenoiserTypeDirect: 1 + m_PVRDenoiserTypeIndirect: 1 + m_PVRDenoiserTypeAO: 1 + m_PVRFilterTypeDirect: 0 + m_PVRFilterTypeIndirect: 0 + m_PVRFilterTypeAO: 0 + m_PVREnvironmentMIS: 1 + m_PVRCulling: 1 + m_PVRFilteringGaussRadiusDirect: 1 + m_PVRFilteringGaussRadiusIndirect: 5 + m_PVRFilteringGaussRadiusAO: 2 + m_PVRFilteringAtrousPositionSigmaDirect: 0.5 + m_PVRFilteringAtrousPositionSigmaIndirect: 2 + m_PVRFilteringAtrousPositionSigmaAO: 1 + m_ExportTrainingData: 0 + m_TrainingDataDestination: TrainingData + m_LightProbeSampleCountMultiplier: 4 + m_LightingDataAsset: {fileID: 0} + m_LightingSettings: {fileID: 0} +--- !u!196 &4 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 2 + agentTypeID: 0 + agentRadius: 0.5 + agentHeight: 2 + agentSlope: 45 + agentClimb: 0.4 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + minRegionArea: 2 + manualCellSize: 0 + cellSize: 0.16666667 + manualTileSize: 0 + tileSize: 256 + accuratePlacement: 0 + maxJobWorkers: 0 + preserveTilesOutsideBounds: 0 + debug: + m_Flags: 0 + m_NavMeshData: {fileID: 0} +--- !u!1 &336074530 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 336074533} + - component: {fileID: 336074532} + - component: {fileID: 336074531} + m_Layer: 0 + m_Name: Main Camera + m_TagString: MainCamera + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!81 &336074531 +AudioListener: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 336074530} + m_Enabled: 1 +--- !u!20 &336074532 +Camera: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 336074530} + m_Enabled: 1 + serializedVersion: 2 + m_ClearFlags: 1 + m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} + m_projectionMatrixMode: 1 + m_GateFitMode: 2 + m_FOVAxisMode: 0 + m_SensorSize: {x: 36, y: 24} + m_LensShift: {x: 0, y: 0} + m_FocalLength: 50 + m_NormalizedViewPortRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 + near clip plane: 0.3 + far clip plane: 1000 + field of view: 60 + orthographic: 1 + orthographic size: 5 + m_Depth: -1 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingPath: -1 + m_TargetTexture: {fileID: 0} + m_TargetDisplay: 0 + m_TargetEye: 3 + m_HDR: 1 + m_AllowMSAA: 1 + m_AllowDynamicResolution: 0 + m_ForceIntoRT: 0 + m_OcclusionCulling: 1 + m_StereoConvergence: 10 + m_StereoSeparation: 0.022 +--- !u!4 &336074533 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 336074530} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: -10} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &602111269 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 602111274} + - component: {fileID: 602111273} + - component: {fileID: 602111272} + - component: {fileID: 602111271} + - component: {fileID: 602111270} + m_Layer: 0 + m_Name: Cube + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &602111270 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 602111269} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9f65af9eea4f24d488968efcdb7cf7d4, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!65 &602111271 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 602111269} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + serializedVersion: 2 + m_Size: {x: 1, y: 1, z: 1} + m_Center: {x: 0, y: 0, z: 0} +--- !u!23 &602111272 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 602111269} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 2 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_AdditionalVertexStreams: {fileID: 0} +--- !u!33 &602111273 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 602111269} + m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} +--- !u!4 &602111274 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 602111269} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Spockle.cs b/Spockle.cs new file mode 100644 index 0000000..99200c0 --- /dev/null +++ b/Spockle.cs @@ -0,0 +1,169 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using Speckle.Core; +using Speckle.Core.Models; +using Speckle.Core.Api; +using Speckle.Core.Credentials; +using System; +using System.Threading.Tasks; +using System.Linq; +using Speckle.Core.Transports; +using Objects.Converter.Unity; +using Objects.Geometry; + +namespace Speckle.ConnectorUnity +{ + public class Spockle : MonoBehaviour + { + + ConverterUnity _converter = new ConverterUnity(); + + async void Start() + { + + var items = new List { "aa", "bb", "cc" }; + + var gg = items.Select(x => x + "00"); + + var dd = gg.ToList(); + + await GetStreamLatestCommit("cd83745025"); + } + + + void Update() + { + + } + + private async Task GetStreamLatestCommit(string streamId) + { + try + { + + var account = AccountManager.GetDefaultAccount(); + var client = new Client(account); + var res = await client.StreamGet(streamId); + var mainBranch = res.branches.items.FirstOrDefault(b => b.name == "main"); + var commit = mainBranch.commits.items[0]; + var transport = new ServerTransport(account, streamId); + var @base = await Operations.Receive( + commit.referencedObject, + remoteTransport: transport + ); + + + + var data = ConvertRecursivelyToNative(@base); + + + } + catch (Exception e) + { + + } + } + + /// + /// Helper method to convert a tree-like structure (nested lists) to Native + /// + /// + /// + public object ConvertRecursivelyToNative(Base @base) + { + // case 1: it's an item that has a direct conversion method, eg a point + if (_converter.CanConvertToNative(@base)) + return TryConvertItemToNative(@base); + + // case 2: it's a wrapper Base + // 2a: if there's only one member unpack it + // 2b: otherwise return dictionary of unpacked members + var members = @base.GetDynamicMembers().ToList(); + + if (members.Count() == 1) + { + return RecurseTreeToNative(@base[members.First()]); + } + + return members.Select(x => RecurseTreeToNative(@base[x])).ToList(); + } + + + private object RecurseTreeToNative(object @object) + { + if (IsList(@object)) + { + var list = ((IEnumerable)@object).Cast(); + return list.Select(x => RecurseTreeToNative(x)).ToList(); + } + + return TryConvertItemToNative(@object); + } + + private object TryConvertItemToNative(object value) + { + if (value == null) + return value; + + //it's a simple type or not a Base + if (value.GetType().IsSimpleType() || !(value is Base)) + { + return value; + } + + var @base = (Base)value; + + //it's an unsupported Base, return a dictionary + if (!_converter.CanConvertToNative(@base)) + { + var members = @base.GetMembers().Values.ToList(); + + + return members.Select(x => RecurseTreeToNative(x)).ToList(); + } + + try + { + var converted = _converter.ConvertToNative(@base) as UnityGeometry; + + if (converted is UnityMesh) + { + MeshRenderer mr = converted.gameObject.GetComponent(); + mr.material = new Material(Shader.Find("Diffuse")); + } + + + + return converted; + } + catch (Exception ex) + { + Core.Logging.Log.CaptureAndThrow(ex); + } + + return null; + } + + + public static bool IsList(object @object) + { + if (@object == null) + return false; + + var type = @object.GetType(); + return (typeof(IEnumerable).IsAssignableFrom(type) && !typeof(IDictionary).IsAssignableFrom(type) && + type != typeof(string)); + } + + public static bool IsDictionary(object @object) + { + if (@object == null) + return false; + + Type type = @object.GetType(); + return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>); + } + } + +} \ No newline at end of file