public GameObject CreateColliderObject(ItemSelectorInstanceInfo info) { _colliderCounter++; GameObject newColliderObject; if (_vegetationItemInfoPro.ColliderType == ColliderType.FromPrefab) { newColliderObject = Object.Instantiate(_sourceColliderObject); newColliderObject.name = "Collider_" + _colliderCounter.ToString(); newColliderObject.hideFlags = GetVisibilityHideFlags(); newColliderObject.transform.SetParent(_colliderParent); } else { newColliderObject = CreatePrimitiveCollider(info); } if (_vegetationItemInfoPro.ColliderTag != "") { newColliderObject.tag = _vegetationItemInfoPro.ColliderTag; } AddNavMesObstacle(newColliderObject); newColliderObject.SetActive(true); AddVegetationItemInstanceInfo(newColliderObject); PositionColliderObject(newColliderObject, info); newColliderObject.layer = _vegetationSystemPro.VegetationSettings.GetLayer(_vegetationItemInfoPro.VegetationType); return(newColliderObject); }
void PositionPrefabObject(GameObject prefabObject, ItemSelectorInstanceInfo info) { Vector3 scale = Vector3.one; if (_runtimePrefabRule.UseVegetationItemScale) { scale = new Vector3(scale.x * info.Scale.x, scale.y * info.Scale.y, scale.z * info.Scale.z); } Vector3 position = info.Position; Vector3 offset; if (_runtimePrefabRule.UseVegetationItemScale) { offset = new Vector3(info.Scale.x * _runtimePrefabRule.PrefabOffset.x, info.Scale.y * _runtimePrefabRule.PrefabOffset.y, info.Scale.z * _runtimePrefabRule.PrefabOffset.z); } else { offset = _runtimePrefabRule.PrefabOffset; } offset += info.Rotation * offset; prefabObject.transform.position = position + offset; prefabObject.transform.localScale = new Vector3(scale.x * _runtimePrefabRule.PrefabScale.x, scale.y * _runtimePrefabRule.PrefabScale.y, scale.z * _runtimePrefabRule.PrefabScale.z); prefabObject.transform.rotation = info.Rotation * Quaternion.Euler(_runtimePrefabRule.PrefabRotation); UpdateVegetationItemInstanceInfo(prefabObject, info); }
public GameObject CreateRuntimePrefabObject(ItemSelectorInstanceInfo info) { _prefabCounter++; GameObject newRuntimePrefab; if (_runtimePrefabRule.RuntimePrefab) { newRuntimePrefab = Object.Instantiate(_runtimePrefabRule.RuntimePrefab); newRuntimePrefab.name = _runtimePrefabRule.RuntimePrefab.name; } else { newRuntimePrefab = GameObject.CreatePrimitive(PrimitiveType.Cube); newRuntimePrefab.name = "Run-time prefab" + _prefabCounter.ToString() + "_" + newRuntimePrefab.name; } newRuntimePrefab.hideFlags = GetVisibilityHideFlags(); newRuntimePrefab.transform.SetParent(_prefabParent); newRuntimePrefab.SetActive(true); newRuntimePrefab.layer = _runtimePrefabRule.PrefabLayer; AddVegetationItemInstanceInfo(newRuntimePrefab); PositionPrefabObject(newRuntimePrefab, info); return(newRuntimePrefab); }
void PositionColliderObject(GameObject colliderObject, ItemSelectorInstanceInfo info) { colliderObject.transform.position = info.Position + _vegetationSystemPro.FloatingOriginOffset; colliderObject.transform.localScale = info.Scale; colliderObject.transform.rotation = info.Rotation; UpdateVegetationItemInstanceInfo(colliderObject, info); }
private void OnVegetationItemVisible(ItemSelectorInstanceInfo itemSelectorInstanceInfo, VegetationItemIndexes vegetationItemIndexes, string vegetationItemID) { GameObject colliderObject = RuntimePrefabPool.GetObject(itemSelectorInstanceInfo); RuntimePrefabStorage.AddRuntimePrefab(colliderObject, itemSelectorInstanceInfo.VegetationCellIndex, itemSelectorInstanceInfo.VegetationCellItemIndex); }
void PositionColliderObject(GameObject colliderObject, ItemSelectorInstanceInfo info) { colliderObject.transform.position = info.Position; colliderObject.transform.localScale = info.Scale; colliderObject.transform.rotation = info.Rotation; UpdateVegetationItemInstanceInfo(colliderObject, info); }
void UpdateVegetationItemInstanceInfo(GameObject colliderObject, ItemSelectorInstanceInfo info) { VegetationItemInstanceInfo vegetationItemInstanceInfo = colliderObject.GetComponent <VegetationItemInstanceInfo>(); if (vegetationItemInstanceInfo) { vegetationItemInstanceInfo.Position = info.Position; vegetationItemInstanceInfo.VegetationItemInstanceID = Mathf.RoundToInt(vegetationItemInstanceInfo.Position.x * 100f).ToString() + "_" + Mathf.RoundToInt(vegetationItemInstanceInfo.Position.y * 100f).ToString() + "_" + Mathf.RoundToInt(vegetationItemInstanceInfo.Position.z * 100f).ToString(); vegetationItemInstanceInfo.Rotation = info.Rotation; vegetationItemInstanceInfo.Scale = info.Scale; } }
public override GameObject GetObject(ItemSelectorInstanceInfo info) { if (_colliderPoolList.Count <= 0) { return(CreateColliderObject(info)); } GameObject colliderObject = _colliderPoolList[_colliderPoolList.Count - 1]; _colliderPoolList.RemoveAtSwapBack(_colliderPoolList.Count - 1); colliderObject.SetActive(true); PositionColliderObject(colliderObject, info); return(colliderObject); }
public override GameObject GetObject(ItemSelectorInstanceInfo info) { if (_prefabPoolList.Count <= 0) { return(CreateRuntimePrefabObject(info)); } GameObject prefabObject = _prefabPoolList[_prefabPoolList.Count - 1]; _prefabPoolList.RemoveAtSwapBack(_prefabPoolList.Count - 1); prefabObject.SetActive(true); PositionPrefabObject(prefabObject, info); return(prefabObject); }
private void OnVegetationItemInvisible(ItemSelectorInstanceInfo itemSelectorInstanceInfo, VegetationItemIndexes vegetationItemIndexes, string vegetationItemID) { if (OnBeforeDestroyColliderDelegate != null) { GameObject colliderObject = RuntimePrefabStorage.GetRuntimePrefab( itemSelectorInstanceInfo.VegetationCellIndex, itemSelectorInstanceInfo.VegetationCellItemIndex); OnBeforeDestroyColliderDelegate(colliderObject); } RuntimePrefabStorage.RemoveRuntimePrefab(itemSelectorInstanceInfo.VegetationCellIndex, itemSelectorInstanceInfo.VegetationCellItemIndex, ColliderPool); }
public virtual GameObject GetObject(ItemSelectorInstanceInfo info) { return null; }
void BakeVegetationItemColliders(ColliderManager colliderManager, VegetationItemInfoPro vegetationItemInfoPro) { GC.Collect(); GameObject rootItem = new GameObject("Baked colliders_" + vegetationItemInfoPro.Name + "_" + vegetationItemInfoPro.VegetationItemID); #if UNITY_EDITOR if (!Application.isPlaying) { EditorUtility.DisplayProgressBar("Bake Collider: " + vegetationItemInfoPro.Name, "Bake cell", 0); } #endif int colliderCount = 0; for (int i = 0; i <= VegetationSystemPro.VegetationCellList.Count - 1; i++) { VegetationCell vegetationCell = VegetationSystemPro.VegetationCellList[i]; #if UNITY_EDITOR if (i % 10 == 0) { if (!Application.isPlaying) { EditorUtility.DisplayProgressBar("Bake Collider: " + vegetationItemInfoPro.Name, "Bake cell " + i + "/" + (VegetationSystemPro.VegetationCellList.Count - 1), i / ((float)VegetationSystemPro.VegetationCellList.Count - 1)); } } #endif VegetationSystemPro.SpawnVegetationCell(vegetationCell, vegetationItemInfoPro.VegetationItemID); NativeList <MatrixInstance> vegetationInstanceList = VegetationSystemPro.GetVegetationItemInstances(vegetationCell, vegetationItemInfoPro.VegetationItemID); for (int j = 0; j <= vegetationInstanceList.Length - 1; j++) { Matrix4x4 vegetationItemMatrix = vegetationInstanceList[j].Matrix; Vector3 position = MatrixTools.ExtractTranslationFromMatrix(vegetationItemMatrix); Vector3 scale = MatrixTools.ExtractScaleFromMatrix(vegetationItemMatrix); Quaternion rotation = MatrixTools.ExtractRotationFromMatrix(vegetationItemMatrix); ItemSelectorInstanceInfo itemSelectorInstanceInfo = new ItemSelectorInstanceInfo { Position = position, Scale = scale, Rotation = rotation }; GameObject newCollider = colliderManager.ColliderPool.GetObject(itemSelectorInstanceInfo); newCollider.hideFlags = HideFlags.None; newCollider.transform.SetParent(rootItem.transform, true); SetNavmeshArea(newCollider, vegetationItemInfoPro.NavMeshArea); if (SetBakedCollidersStatic) { SetStatic(newCollider); } if (ConvertBakedCollidersToMesh) { CreateNavMeshColliderMeshes(newCollider); } colliderCount++; } vegetationCell.ClearCache(); } VegetationSystemPro.ClearCache(vegetationItemInfoPro.VegetationItemID); #if UNITY_EDITOR if (!Application.isPlaying) { EditorUtility.ClearProgressBar(); } #endif if (colliderCount == 0) { DestroyImmediate(rootItem); } }
private GameObject CreatePrimitiveCollider(ItemSelectorInstanceInfo info) { switch (_vegetationItemInfoPro.ColliderType) { case ColliderType.Capsule: GameObject newCapsuleColliderObject = new GameObject("CapsuleCollider_" + _colliderCounter); newCapsuleColliderObject.layer = _colliderLayer; newCapsuleColliderObject.tag = _colliderTag; newCapsuleColliderObject.transform.SetParent(_colliderParent); newCapsuleColliderObject.hideFlags = GetVisibilityHideFlags(); CapsuleCollider capsuleCollider = newCapsuleColliderObject.AddComponent <CapsuleCollider>(); capsuleCollider.height = _vegetationItemInfoPro.ColliderHeight; capsuleCollider.radius = _vegetationItemInfoPro.ColliderRadius; capsuleCollider.isTrigger = _vegetationItemInfoPro.ColliderTrigger; Vector3 capsuleColliderOffset = new Vector3(info.Scale.x * _vegetationItemInfoPro.ColliderOffset.x, info.Scale.y * _vegetationItemInfoPro.ColliderOffset.y, info.Scale.z * _vegetationItemInfoPro.ColliderOffset.z); capsuleColliderOffset += info.Rotation * capsuleColliderOffset; capsuleCollider.center = capsuleColliderOffset; return(newCapsuleColliderObject); case ColliderType.Sphere: GameObject newSphereColliderObject = new GameObject("SphereCollider_" + _colliderCounter); newSphereColliderObject.layer = _colliderLayer; newSphereColliderObject.tag = _colliderTag; newSphereColliderObject.transform.SetParent(_colliderParent); newSphereColliderObject.hideFlags = GetVisibilityHideFlags(); SphereCollider sphereCollider = newSphereColliderObject.AddComponent <SphereCollider>(); sphereCollider.radius = _vegetationItemInfoPro.ColliderRadius; sphereCollider.isTrigger = _vegetationItemInfoPro.ColliderTrigger; Vector3 sphereColliderOffset = new Vector3(info.Scale.x * _vegetationItemInfoPro.ColliderOffset.x, info.Scale.y * _vegetationItemInfoPro.ColliderOffset.y, info.Scale.z * _vegetationItemInfoPro.ColliderOffset.z); sphereColliderOffset += info.Rotation * sphereColliderOffset; sphereCollider.center = sphereColliderOffset; return(newSphereColliderObject); case ColliderType.Box: GameObject newColliderObject = new GameObject("BoxCollider_" + _colliderCounter); newColliderObject.layer = _colliderLayer; newColliderObject.tag = _colliderTag; newColliderObject.transform.SetParent(_colliderParent); newColliderObject.hideFlags = GetVisibilityHideFlags(); BoxCollider boxCollider = newColliderObject.AddComponent <BoxCollider>(); Vector3 boxColliderSize = new Vector3(info.Scale.x * _vegetationItemInfoPro.ColliderSize.x, info.Scale.y * _vegetationItemInfoPro.ColliderSize.y, info.Scale.z * _vegetationItemInfoPro.ColliderSize.z); boxCollider.size = boxColliderSize; boxCollider.isTrigger = _vegetationItemInfoPro.ColliderTrigger; Vector3 boxColliderOffset = new Vector3(info.Scale.x * _vegetationItemInfoPro.ColliderOffset.x, info.Scale.y * _vegetationItemInfoPro.ColliderOffset.y, info.Scale.z * _vegetationItemInfoPro.ColliderOffset.z); boxColliderOffset += info.Rotation * boxColliderOffset; boxCollider.center = boxColliderOffset; return(newColliderObject); case ColliderType.CustomMesh: GameObject newCustomMeshColliderObject = new GameObject("MeshCollider_" + _colliderCounter); newCustomMeshColliderObject.layer = _colliderLayer; newCustomMeshColliderObject.tag = _colliderTag; newCustomMeshColliderObject.transform.SetParent(_colliderParent); newCustomMeshColliderObject.hideFlags = GetVisibilityHideFlags(); MeshCollider customMeshCollider = newCustomMeshColliderObject.AddComponent <MeshCollider>(); customMeshCollider.isTrigger = _vegetationItemInfoPro.ColliderTrigger; customMeshCollider.sharedMesh = _vegetationItemInfoPro.ColliderMesh; customMeshCollider.convex = _vegetationItemInfoPro.ColliderConvex; return(newCustomMeshColliderObject); case ColliderType.Mesh: GameObject newMeshColliderObject = new GameObject("MeshCollider_" + _colliderCounter); newMeshColliderObject.layer = _colliderLayer; newMeshColliderObject.tag = _colliderTag; newMeshColliderObject.transform.SetParent(_colliderParent); newMeshColliderObject.hideFlags = GetVisibilityHideFlags(); MeshCollider meshCollider = newMeshColliderObject.AddComponent <MeshCollider>(); meshCollider.isTrigger = _vegetationItemInfoPro.ColliderTrigger; meshCollider.sharedMesh = _vegetationItemModelInfo.VegetationMeshLod0; meshCollider.convex = _vegetationItemInfoPro.ColliderConvex; return(newMeshColliderObject); } return(new GameObject("Empty collider object")); }
private void OnVegetationItemInvisible(ItemSelectorInstanceInfo itemSelectorInstanceInfo, VegetationItemIndexes vegetationItemIndexes, string vegetationItemID) { RuntimePrefabStorage.RemoveRuntimePrefab(itemSelectorInstanceInfo.VegetationCellIndex, itemSelectorInstanceInfo.VegetationCellItemIndex, RuntimePrefabPool); }