diff --git a/.gitignore b/.gitignore index 486b555..d1f9514 100644 --- a/.gitignore +++ b/.gitignore @@ -70,3 +70,4 @@ crashlytics-build.properties /[Aa]ssets/[Ss]treamingAssets/aa.meta /[Aa]ssets/[Ss]treamingAssets/aa/* .idea/ +*.meta diff --git a/.idea/.idea.Speckle Unity/.idea/contentModel.xml b/.idea/.idea.Speckle Unity/.idea/contentModel.xml index eff4146..564f5f8 100644 --- a/.idea/.idea.Speckle Unity/.idea/contentModel.xml +++ b/.idea/.idea.Speckle Unity/.idea/contentModel.xml @@ -19,6 +19,7 @@ + diff --git a/Assets/Speckle Connector/ConverterUnity.Geometry.cs b/Assets/Speckle Connector/ConverterUnity.Geometry.cs index efd2362..c6fec43 100644 --- a/Assets/Speckle Connector/ConverterUnity.Geometry.cs +++ b/Assets/Speckle Connector/ConverterUnity.Geometry.cs @@ -2,8 +2,8 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Objects.Other; +using Speckle.ConnectorUnity; using UnityEngine; using Mesh = Objects.Geometry.Mesh; @@ -94,7 +94,7 @@ namespace Objects.Converter.Unity List faces = new List(); int i = 0; //store them here, makes it like 1000000x faster? - var triangles = filter.mesh.triangles; + var triangles = filter.mesh.triangles; while (i < triangles.Length) { faces.Add(0); @@ -245,6 +245,33 @@ namespace Objects.Converter.Unity var mesh = go.AddComponent().mesh; var meshRenderer = go.AddComponent(); + //todo support more complex materials + var shader = Shader.Find("Standard"); + var mat = new Material(shader); + + var speckleMaterial = speckleMesh["renderMaterial"]; + if (speckleMaterial != null && speckleMaterial is RenderMaterial rm) + { + // 1. match shader by name, if any + shader = Shader.Find(rm.name); + if (shader != null) + { + mat = new Material(shader); + } + else + { + // 2. re-create material by setting diffuse color and transparency on standard shaders + shader = Shader.Find("Transparent/Diffuse"); + mat = new Material(shader); + var c = rm.diffuse.ToUnityColor(); + mat.color = new Color(c.r, c.g, c.b, Convert.ToSingle(rm.opacity)); + } + } + + // 3. if not renderMaterial was passed, the default shader will be used + meshRenderer.material = mat; + + if (verts.Count >= 65535) mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; diff --git a/Assets/Speckle Connector/Utils.cs b/Assets/Speckle Connector/Utils.cs new file mode 100644 index 0000000..84bfc1a --- /dev/null +++ b/Assets/Speckle Connector/Utils.cs @@ -0,0 +1,22 @@ +using System; +using UnityEngine; + +namespace Speckle.ConnectorUnity +{ + public static class Utils + { + public static int ToIntColor(this Color c) + { + return + System.Drawing.Color + .FromArgb(Convert.ToInt32(c.r * 255), Convert.ToInt32(c.r * 255), Convert.ToInt32(c.r * 255)) + .ToArgb(); + } + + public static Color ToUnityColor(this int c) + { + var argb = System.Drawing.Color.FromArgb(c); + return new Color(argb.R / 255.0f, argb.G / 255.0f, argb.B / 255.0f); + } + } +} \ No newline at end of file diff --git a/Assets/SpeckleExamples.cs b/Assets/SpeckleExamples.cs index 42cf10f..4aeefd5 100644 --- a/Assets/SpeckleExamples.cs +++ b/Assets/SpeckleExamples.cs @@ -31,7 +31,7 @@ namespace Speckle.ConnectorUnity void Start() { //hardcoded cuz I'm lazy, replace with what you need - ReceiveText.text = "4ad65b572e"; + ReceiveText.text = "d1a4748ba2";//4ad65b572e SendText.text = "cd83745025"; if (ReceiveBtn == null || ReceiveText == null || SendBtn == null || SendText == null) @@ -125,14 +125,9 @@ namespace Speckle.ConnectorUnity /// private void AddClasses(GameObject go) { - var mat = new Material(Shader.Find("Standard")); - for (var i = 0; i < go.transform.childCount; i++) { var child = go.transform.GetChild(i); - var renderer = child.GetComponent(); - renderer.material = mat; - child.gameObject.AddComponent(); } }