Mesh AddMeshComponents(AbcAPI.aiObject abc, GameObject gameObject) { Mesh mesh = null; MeshFilter meshFilter = gameObject.GetComponent <MeshFilter>(); if (meshFilter == null || meshFilter.sharedMesh == null) { mesh = new Mesh(); mesh.MarkDynamic(); if (meshFilter == null) { meshFilter = gameObject.AddComponent <MeshFilter>(); } meshFilter.sharedMesh = mesh; MeshRenderer renderer = gameObject.GetComponent <MeshRenderer>(); if (renderer == null) { renderer = gameObject.AddComponent <MeshRenderer>(); } #if UNITY_EDITOR Material material = UnityEngine.Object.Instantiate(AbcUtils.GetDefaultMaterial()); material.name = "Material_0"; renderer.sharedMaterial = material; #endif } else { mesh = meshFilter.sharedMesh; } return(mesh); }
public override void AbcUpdate() { if (!AbcIsDirty()) { return; } bool useSubObjects = (m_summary.topologyVariance == AbcAPI.aiTopologyVariance.Heterogeneous || m_sampleSummary.splitCount > 1); for (int s = 0; s < m_splits.Count; ++s) { Split split = m_splits[s]; if (split.active) { // Feshly created splits may not have their host set yet if (split.host == null) { if (useSubObjects) { string name = m_trans.gameObject.name + "_split_" + s; Transform trans = m_trans.FindChild(name); if (trans == null) { GameObject go = new GameObject(); go.name = name; trans = go.GetComponent <Transform>(); trans.parent = m_trans; trans.localPosition = Vector3.zero; trans.localEulerAngles = Vector3.zero; trans.localScale = Vector3.one; } split.host = trans.gameObject; } else { split.host = m_trans.gameObject; } } // Feshly created splits may not have their mesh set yet if (split.mesh == null) { split.mesh = AddMeshComponents(m_abcObj, split.host); split.mesh.name = split.host.name; } if (split.clear) { split.mesh.Clear(); } split.mesh.vertices = split.positionCache; split.mesh.normals = split.normalCache; split.mesh.tangents = split.tangentCache; split.mesh.uv = split.uvCache; // update the bounds split.mesh.bounds = new Bounds(split.center, split.size); if (split.clear) { split.mesh.subMeshCount = split.submeshCount; MeshRenderer renderer = split.host.GetComponent <MeshRenderer>(); Material[] currentMaterials = renderer.sharedMaterials; int nmat = currentMaterials.Length; if (nmat != split.submeshCount) { Material[] materials = new Material[split.submeshCount]; int copyTo = (nmat < split.submeshCount ? nmat : split.submeshCount); for (int i = 0; i < copyTo; ++i) { materials[i] = currentMaterials[i]; } #if UNITY_EDITOR for (int i = copyTo; i < split.submeshCount; ++i) { Material material = UnityEngine.Object.Instantiate(AbcUtils.GetDefaultMaterial()); material.name = "Material_" + Convert.ToString(i); materials[i] = material; } #endif renderer.sharedMaterials = materials; } } split.clear = false; split.host.SetActive(true); } else { split.host.SetActive(false); } } for (int s = 0; s < m_submeshes.Count; ++s) { Submesh submesh = m_submeshes[s]; if (submesh.update) { m_splits[submesh.splitIndex].mesh.SetIndices(submesh.indexCache, MeshTopology.Triangles, submesh.index); submesh.update = false; } } if (!m_sampleSummary.hasNormals && !m_sampleSummary.hasTangents) { for (int s = 0; s < m_sampleSummary.splitCount; ++s) { m_splits[s].mesh.RecalculateNormals(); } } AbcClean(); }