/// <summary> /// Return an input data structure containing mesh data that needs to be /// uploaded from the given inputObject. /// Supports child gameobjects with meshes from the given inputObject. /// </summary> /// <param name="inputObject">GameObject containing mesh components</param> /// <returns>A valid input data strcuture containing mesh data</returns> public HEU_InputDataMeshes GenerateMeshDatasFromGameObject(GameObject inputObject, bool bExportColliders = false) { HEU_InputDataMeshes inputMeshes = new HEU_InputDataMeshes(); inputMeshes._inputObject = inputObject; LODGroup lodGroup = inputObject.GetComponent<LODGroup>(); if (lodGroup != null) { inputMeshes._hasLOD = true; LOD[] lods = lodGroup.GetLODs(); for (int i = 0; i < lods.Length; ++i) { if (lods[i].renderers != null && lods[i].renderers.Length > 0) { GameObject childGO = lods[i].renderers[0].gameObject; HEU_InputDataMesh meshData = CreateSingleMeshData(childGO, bExportColliders); if (meshData != null) { meshData._LODScreenTransition = lods[i].screenRelativeTransitionHeight; inputMeshes._inputMeshes.Add(meshData); } } } } else { inputMeshes._hasLOD = false; // Create a HEU_InputDataMesh for each gameobject with a MeshFilter (including children) MeshFilter[] meshFilters = inputObject.GetComponentsInChildren<MeshFilter>(); foreach (MeshFilter filter in meshFilters) { HEU_InputDataMesh meshData = CreateSingleMeshData(filter.gameObject, bExportColliders); if (meshData != null) { inputMeshes._inputMeshes.Add(meshData); } } SkinnedMeshRenderer[] skinnedMeshRenderers = inputObject.GetComponentsInChildren<SkinnedMeshRenderer>(); foreach (SkinnedMeshRenderer skinnedMeshRend in skinnedMeshRenderers) { HEU_InputDataMesh meshData = CreateSingleMeshData(skinnedMeshRend.gameObject, bExportColliders); if (meshData != null) { inputMeshes._inputMeshes.Add(meshData); } } } return inputMeshes; }
/// <summary> /// Returns HEU_UploadMeshData with mesh data found on meshGameObject. /// </summary> /// <param name="meshGameObject">The GameObject to query mesh data from</param> /// <returns>A valid HEU_UploadMeshData if mesh data found or null</returns> public static HEU_InputDataMesh CreateSingleMeshData(GameObject meshGameObject) { HEU_InputDataMesh meshData = new HEU_InputDataMesh(); if (meshGameObject == null) { return null; } Mesh sharedMesh = GetMeshFromObject(meshGameObject); if (sharedMesh == null) { return null; } meshData._mesh = sharedMesh; meshData._numVertices = meshData._mesh.vertexCount; meshData._numSubMeshes = meshData._mesh.subMeshCount; meshData._meshName = meshGameObject.name; // Use project path is not saved in scene, otherwise just use name if (HEU_GeneralUtility.IsGameObjectInProject(meshGameObject)) { meshData._meshPath = HEU_AssetDatabase.GetAssetOrScenePath(meshGameObject); if (string.IsNullOrEmpty(meshData._meshPath)) { meshData._meshPath = meshGameObject.name; } } else { meshData._meshPath = meshGameObject.name; } //Debug.Log("Mesh Path: " + meshData._meshPath); MeshRenderer meshRenderer = meshGameObject.GetComponent<MeshRenderer>(); if (meshRenderer != null) { meshData._materials = meshRenderer.sharedMaterials; } meshData._transform = meshGameObject.transform; return meshData; }
/// <summary> /// Returns HEU_UploadMeshData with mesh data found on meshGameObject. /// </summary> /// <param name="meshGameObject">The GameObject to query mesh data from</param> /// <returns>A valid HEU_UploadMeshData if mesh data found or null</returns> public static HEU_InputDataMesh CreateSingleMeshData(GameObject meshGameObject) { HEU_InputDataMesh meshData = new HEU_InputDataMesh(); MeshFilter meshfilter = meshGameObject.GetComponent<MeshFilter>(); if (meshfilter == null) { return null; } if (meshfilter.sharedMesh == null) { return null; } meshData._mesh = meshfilter.sharedMesh; meshData._numVertices = meshData._mesh.vertexCount; meshData._numSubMeshes = meshData._mesh.subMeshCount; meshData._meshName = meshGameObject.name; meshData._meshPath = HEU_AssetDatabase.GetAssetOrScenePath(meshGameObject); if (string.IsNullOrEmpty(meshData._meshPath)) { meshData._meshPath = meshGameObject.name; } MeshRenderer meshRenderer = meshGameObject.GetComponent<MeshRenderer>(); if (meshRenderer != null) { meshData._materials = meshRenderer.sharedMaterials; } meshData._transform = meshGameObject.transform; return meshData; }
/// <summary> /// Returns HEU_UploadMeshData with mesh data found on meshGameObject. /// </summary> /// <param name="meshGameObject">The GameObject to query mesh data from</param> /// <returns>A valid HEU_UploadMeshData if mesh data found or null</returns> public static HEU_InputDataMesh CreateSingleMeshData(GameObject meshGameObject, bool bExportColliders) { HEU_InputDataMesh meshData = new HEU_InputDataMesh(); if (meshGameObject == null) { return null; } Mesh sharedMesh = GetMeshFromObject(meshGameObject); if (sharedMesh == null) { return null; } meshData._mesh = sharedMesh; meshData._numVertices = meshData._mesh.vertexCount; meshData._numSubMeshes = meshData._mesh.subMeshCount; meshData._meshName = meshGameObject.name; // Use project path is not saved in scene, otherwise just use name if (HEU_GeneralUtility.IsGameObjectInProject(meshGameObject)) { meshData._meshPath = HEU_AssetDatabase.GetAssetOrScenePath(meshGameObject); if (string.IsNullOrEmpty(meshData._meshPath)) { meshData._meshPath = meshGameObject.name; } } else { meshData._meshPath = meshGameObject.name; } //HEU_Logger.Log("Mesh Path: " + meshData._meshPath); MeshRenderer meshRenderer = meshGameObject.GetComponent<MeshRenderer>(); if (meshRenderer != null) { meshData._materials = meshRenderer.sharedMaterials; } meshData._transform = meshGameObject.transform; if (bExportColliders && meshGameObject != null) { meshData._colliders = new List<HEU_InputDataCollider>(); Collider[] colliders = meshGameObject.GetComponents<Collider>(); if (colliders != null) { for (int i = 0; i < colliders.Length; i++) { Collider collider = colliders[i]; if (collider == null) continue; HEU_InputDataCollider newCollider = new HEU_InputDataCollider(); newCollider._collider = collider; if (collider.GetType() == typeof(BoxCollider)) { newCollider._colliderType = HEU_InputColliderType.BOX; } else if (collider.GetType() == typeof(SphereCollider)) { newCollider._colliderType = HEU_InputColliderType.SPHERE; } else if (collider.GetType() == typeof(CapsuleCollider)) { newCollider._colliderType = HEU_InputColliderType.CAPSULE; } else if (collider.GetType() == typeof(MeshCollider)) { newCollider._colliderType = HEU_InputColliderType.MESH; } else { HEU_Logger.LogWarningFormat("Collider type not supported: {0}", meshGameObject.name); newCollider._collider = null; newCollider._colliderType = HEU_InputColliderType.NONE; } if (newCollider._colliderType != HEU_InputColliderType.NONE) { meshData._colliders.Add(newCollider); } } } } return meshData; }