Mesh AddMeshComponents(GameObject gameObject) { Mesh mesh = null; MeshFilter meshFilter = gameObject.GetComponent <MeshFilter>(); bool hasMesh = meshFilter != null && meshFilter.sharedMesh != null && meshFilter.sharedMesh.name.IndexOf("dyn: ") == 0; if (!hasMesh) { mesh = new Mesh { name = "dyn: " + gameObject.name }; #if UNITY_2017_3_OR_NEWER mesh.indexFormat = AlembicTreeNode.streamDescriptor.settings.use32BitsIndexBuffer ? IndexFormat.UInt32 : IndexFormat.UInt16; #endif mesh.MarkDynamic(); if (meshFilter == null) { meshFilter = gameObject.AddComponent <MeshFilter>(); } meshFilter.sharedMesh = mesh; MeshRenderer renderer = gameObject.GetComponent <MeshRenderer>(); if (renderer == null) { renderer = gameObject.AddComponent <MeshRenderer>(); } var mat = gameObject.transform.parent.GetComponentInChildren <MeshRenderer>().sharedMaterial; #if UNITY_EDITOR if (mat == null) { mat = UnityEngine.Object.Instantiate(AbcUtils.GetDefaultMaterial()); mat.name = "Material_0"; } #endif renderer.sharedMaterial = mat; } else { mesh = UnityEngine.Object.Instantiate(meshFilter.sharedMesh); meshFilter.sharedMesh = mesh; mesh.name = "dyn: " + gameObject.name; } return(mesh); }
Mesh AddMeshComponents(AbcAPI.aiObject abc, GameObject gameObject) { Mesh mesh = null; MeshFilter meshFilter = gameObject.GetComponent <MeshFilter>(); bool hasMesh = meshFilter != null && meshFilter.sharedMesh != null && meshFilter.sharedMesh.name.IndexOf("dyn: ") == 0; if (!hasMesh) { mesh = new Mesh { name = "dyn: " + gameObject.name }; 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 = (summary.topologyVariance == AbcAPI.aiTopologyVariance.Heterogeneous || sampleSummary.splitCount > 1); for (int s = 0; s < splits.Count; ++s) { Split split = splits[s]; if (split.active) { // Feshly created splits may not have their host set yet if (split.host == null) { if (useSubObjects) { string name = AlembicTreeNode.linkedGameObj.name + "_split_" + s; Transform trans = AlembicTreeNode.linkedGameObj.transform.Find(name); if (trans == null) { GameObject go = new GameObject(); go.name = name; trans = go.GetComponent <Transform>(); trans.parent = AlembicTreeNode.linkedGameObj.transform; trans.localPosition = Vector3.zero; trans.localEulerAngles = Vector3.zero; trans.localScale = Vector3.one; } split.host = trans.gameObject; } else { split.host = AlembicTreeNode.linkedGameObj; } } // Feshly created splits may not have their mesh set yet if (split.mesh == null) { split.mesh = AddMeshComponents(split.host); } if (split.clear) { split.mesh.Clear(); } split.mesh.vertices = split.positionCache; split.mesh.normals = split.normalCache; split.mesh.tangents = split.tangentCache; split.mesh.uv3 = split.velocitiesXYCache; split.mesh.uv4 = split.velocitiesZCache; 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 < submeshes.Count; ++s) { Submesh submesh = submeshes[s]; if (submesh.update) { splits[submesh.splitIndex].mesh.SetIndices(submesh.indexCache, MeshTopology.Triangles, submesh.index); submesh.update = false; } } if (!sampleSummary.hasNormals && !sampleSummary.hasTangents) { for (int s = 0; s < sampleSummary.splitCount; ++s) { splits[s].mesh.RecalculateNormals(); } } AbcClean(); }
public override void AbcSyncDataEnd() { #if UNITY_EDITOR for (int s = 0; s < m_splits.Count; ++s) { var split = m_splits[s]; var mf = split.host.GetComponent <MeshFilter>(); if (mf != null) { mf.sharedMesh = split.mesh; } } #endif if (!m_abcSchema.schema.isDataUpdated) { return; } // wait async copy complete var sample = m_abcSchema.sample; sample.Sync(); bool useSubObjects = (m_summary.topologyVariance == aiTopologyVariance.Heterogeneous || m_sampleSummary.splitCount > 1); for (int s = 0; s < m_splits.Count; ++s) { var 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 = abcTreeNode.linkedGameObj.name + "_split_" + s; Transform trans = abcTreeNode.linkedGameObj.transform.Find(name); if (trans == null) { GameObject go = new GameObject(); go.name = name; trans = go.GetComponent <Transform>(); trans.parent = abcTreeNode.linkedGameObj.transform; trans.localPosition = Vector3.zero; trans.localEulerAngles = Vector3.zero; trans.localScale = Vector3.one; } split.host = trans.gameObject; } else { split.host = abcTreeNode.linkedGameObj; } } // Feshly created splits may not have their mesh set yet if (split.mesh == null) { split.mesh = AddMeshComponents(split.host); } if (split.clear) { split.mesh.Clear(); } if (split.points.Count > 0) { split.mesh.SetVertices(split.points.List); } if (split.normals.Count > 0) { split.mesh.SetNormals(split.normals.List); } if (split.tangents.Count > 0) { split.mesh.SetTangents(split.tangents.List); } if (split.uv0.Count > 0) { split.mesh.SetUVs(0, split.uv0.List); } if (split.uv1.Count > 0) { split.mesh.SetUVs(1, split.uv1.List); } if (split.velocities.Count > 0) { split.mesh.SetUVs(3, split.velocities.List); } if (split.colors.Count > 0) { split.mesh.SetColors(split.colors.List); } // update the bounds var data = m_splitData[s]; split.mesh.bounds = new Bounds(data.center, data.size); if (split.clear) { int submeshCount = m_splitSummaries[s].submeshCount; split.mesh.subMeshCount = submeshCount; MeshRenderer renderer = split.host.GetComponent <MeshRenderer>(); Material[] currentMaterials = renderer.sharedMaterials; int nmat = currentMaterials.Length; if (nmat != submeshCount) { Material[] materials = new Material[submeshCount]; int copyTo = (nmat < submeshCount ? nmat : submeshCount); for (int i = 0; i < copyTo; ++i) { materials[i] = currentMaterials[i]; } #if UNITY_EDITOR for (int i = copyTo; i < 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 smi = 0; smi < m_sampleSummary.submeshCount; ++smi) { var submesh = m_submeshes[smi]; if (submesh.update) { var sum = m_submeshSummaries[smi]; var split = m_splits[sum.splitIndex]; split.mesh.SetTriangles(submesh.indexCache.List, sum.submeshIndex); } } }