public void UpdateInstanceData(FamilyInstance f) { instanceData.Transform = f.Transform; instanceData.HostId = f.HostId; this.transform.SetPosition(instanceData.Transform); if (instanceData.HostId != null) { host = GeometryLibrary.GetObject(instanceData.HostId); } #if UNITY_EDITOR this.ignoreNextDifference = this.transform.position != currentPostion || this.transform.rotation != currentRotation; #endif }
private IEnumerator UpdateHostGeometry(FamilyController behaviour, string hostId) { CoroutineWithData <Message> cd = new CoroutineWithData <Message>( behaviour, this.ServerRequestCoroutine(new Message { Type = "GET", Data = JsonConvert.SerializeObject(new { Id = hostId }) }) ); yield return(cd.coroutine); Message getResponse = cd.result; GeometryElement geo = JObject.Parse(getResponse.Data).ToObject <GeometryElement>(); GameObject obj = GeometryLibrary.GetObject(geo.Id); Helpers.MeshGenerator.ResetFaceMeshes(geo, obj); }
public static GameObject GenerateFaceMesh(Face f, GameObject parent) { Vector3[] vertices = f.Vertices.Select( v => new Vector3( (float)v.X * Helpers.Constants.M_PER_FT, (float)v.Z * Helpers.Constants.M_PER_FT, (float)v.Y * Helpers.Constants.M_PER_FT ) ).ToArray(); Vector2[] uvs = new Vector2[vertices.Length]; for (int i = 0; i < uvs.Length; i++) { uvs[i] = new Vector2(vertices[i].x, vertices[i].z); } Mesh msh = new Mesh(); msh.vertices = vertices; msh.triangles = f.Indices.ToList().ToArray().Reverse().ToArray(); msh.uv = uvs; msh.RecalculateNormals(); msh.RecalculateBounds(); GameObject newFace = new GameObject(); newFace.transform.position = Vector3.zero; newFace.transform.parent = parent.transform; newFace.name = $"Mesh Face"; newFace.gameObject.AddComponent(typeof(MeshRenderer)); ((MaterialFaceController)newFace.gameObject.AddComponent(typeof(MaterialFaceController))).LoadInstance(f); MeshFilter filter = newFace.gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter; filter.mesh = msh; newFace.gameObject.AddComponent(typeof(MeshCollider)); MeshRenderer mr = newFace.GetComponent <MeshRenderer>(); mr.material = (UnityEngine.Material)UnityEngine.Resources.Load($"Default"); if (f.MaterialId != null && f.MaterialId != "-1") { var mat = MaterialLibrary.LookupMaterial("_" + f.MaterialId); if (mat != null) { mr.material = mat; newFace.name = $"_Mesh Face (_{f.MaterialId})"; } else { mat = MaterialLibrary.LookupMaterial(f.MaterialId); if (mat != null) { mr.material = mat; newFace.name = $"_Mesh Face ({f.MaterialId})"; } } } if (GeometryLibrary.GetObject(f.ElementId) == null) { GeometryLibrary.Add(f.ElementId, parent); } return(newFace); }
private IEnumerator LoadInstance(FamilyInstance f) { this.instanceData = f; this.InstanceData = Newtonsoft.Json.JsonConvert.SerializeObject(f); this.fam = FamilyLibrary.GetFamily(f.FamilyId); if (this.fam == null) { throw new System.Exception("Family " + f.FamilyId + " is not loaded"); } if (f.HostId != null) { this.host = GeometryLibrary.GetObject(f.HostId); } #if UNITY_EDITOR this.name = $"Family ({f.Id} - {this.fam.Tag} - {this.fam.ModelName ?? this.fam.FamilyName})"; #else this.name = f.Id; #endif // GameObject model = (GameObject)Resources.Load($"Families/{this.fam.Name}/model"); CoroutineWithData <object> cd = new CoroutineWithData <object>(this, FamilyLibrary.ResolveFamilyOBJ(f.FamilyId, f.VariantId)); yield return(cd.coroutine); object result = cd.result; if (result != null) { byte[] objData = cd.result as byte[]; // Debug.Log("PLACING FAMILY"); GameObject modelInstance; using (var textStream = new MemoryStream(objData)) { modelInstance = new OBJLoader().Load(textStream); } var initialRotation = modelInstance.transform.localRotation; modelInstance.transform.parent = this.transform; modelInstance.transform.localPosition = Vector3.zero; modelInstance.transform.localRotation = initialRotation; modelInstance.transform.localScale = new Vector3( Helpers.Constants.M_PER_FT, Helpers.Constants.M_PER_FT, Helpers.Constants.M_PER_FT ); foreach (var l in modelInstance.GetComponentsInChildren <Light>()) { l.gameObject.transform.rotation = Quaternion.LookRotation(Vector3.down); } foreach (UnityEngine.Transform child in transform) { foreach (UnityEngine.Transform geo in child) { var c = geo.GetComponent <FamilyGeometryController>(); if (c != null) { c.CollisionEnter += this.OnCollsionEnterEvent; c.CollisionExit += this.OnCollisionExitEvent; } } } // Debug.Log("Children " + count); } #if UNITY_EDITOR StartCoroutine(CheckForUpdate()); #endif }