13 [
Attribute(
"{58F07022C12D0CF5}Assets/Editor/PlacingPreview/Preview.emat",
UIWidgets.ResourcePickerThumbnail,
"Material used when regenerating the preview.",
category:
"Preview",
params:
"emat")]
27 return SpawnPreview(GetPreviewEntriesFromPrefab(prefabResource), previewPrefab, world, spawnParams, material,
flags);
35 static array<ref SCR_BasePreviewEntry> GetPreviewEntriesFromPrefab(
Resource prefabResource)
38 array<ref SCR_BasePreviewEntry> entries = {};
39 GetPreviewEntries(entitySource, entries);
48 static void GetPreviewEntries(IEntitySource entitySource, out notnull array<ref SCR_BasePreviewEntry> outEntries,
int parentID = -1, SCR_BasePreviewEntry entry = null,
EPreviewEntityFlag flags = 0)
51 entitySource.Get(
"Flags", entityFlags);
56 SCR_BasePreviewEntry entryLocal = entry;
59 entryLocal =
new SCR_BasePreviewEntry(
true);
62 entitySource.Get(
"coords", entryLocal.m_vPosition);
66 entitySource.Get(
"scale",
scale);
67 entryLocal.SetScale(
scale);
72 entitySource.Get(
"angles", entryLocal.m_vAngles);
75 entryLocal.m_EntitySource = entitySource;
76 entryLocal.m_iParentID = parentID;
77 entryLocal.m_vAnglesTerrain =
Vector(0, entryLocal.m_vAngles[0], entryLocal.m_vAngles[2]);
78 entryLocal.m_fHeightTerrain = entryLocal.m_vPosition[1];
79 parentID = outEntries.Insert(entryLocal);
82 int componentCount = entitySource.GetComponentCount();
83 IEntityComponentSource componentSource;
84 bool componentEnabled;
87 for (
int i = 0; i < componentCount; i++)
89 componentSource = entitySource.GetComponent(i);
90 componentSource.Get(
"Enabled", componentEnabled);
91 if (!componentEnabled)
94 if (componentSource.GetClassName().ToType().IsInherited(SCR_PreviewEntityComponent) && !SCR_Enum.HasFlag(
flags,
EPreviewEntityFlag.IGNORE_PREFAB))
97 BaseContainerList entryList = componentSource.GetObjectArray(
"m_aEntries");
98 int entryCount = entryList.Count();
103 for (
int e = entryCount - 1; e >= 0; e--)
106 if (entryList.Get(e).Get(
"m_iParentID", itemParentID) && itemParentID == -1)
114 SCR_BasePreviewEntry listEntry;
115 for (
int e = indexStart; e < entryCount; e++)
117 listEntry = SCR_BasePreviewEntry.Cast(BaseContainerTools.CreateInstanceFromContainer(entryList.Get(e)));
118 if (listEntry.m_iParentID == -1)
122 entryLocal.m_vPosition == listEntry.m_vPosition;
124 entryLocal.m_Mesh = listEntry.m_Mesh;
125 entryLocal.m_Shape = listEntry.m_Shape;
126 entryLocal.m_Flags = listEntry.m_Flags;
130 outEntries.Insert(listEntry);
131 listEntry.m_iParentID += parentID;
132 listEntry.m_vAnglesTerrain =
Vector(0, listEntry.m_vAngles[0], listEntry.m_vAngles[2]);
133 listEntry.m_fHeightTerrain = listEntry.m_vPosition[1];
142 componentSource.Get(
"m_PreviewPrefab", entryLocal.m_Mesh);
150 string componentClassName;
151 typename componentType;
153 for (
int i = 0; i < componentCount; i++)
155 componentSource = entitySource.GetComponent(i);
156 componentSource.Get(
"Enabled", componentEnabled);
157 if (!componentEnabled)
160 componentClassName = componentSource.GetClassName();
161 componentType = componentClassName.ToType();
164 case (componentClassName ==
"Hierarchy" && !entryLocal.m_iPivotID):
166 componentSource.Get(
"PivotID", entryLocal.m_iPivotID);
169 case (componentClassName ==
"MeshObject"):
171 componentSource.Get(
"Object", entryLocal.m_Mesh);
175 case (componentType.IsInherited(SlotManagerComponent)):
177 BaseContainerList slots = componentSource.GetObjectArray(
"Slots");
178 for (
int s = 0, slotCount = slots.Count(); s < slotCount; s++)
184 case (componentType.IsInherited(BaseSlotComponent) || componentType.IsInherited(WeaponSlotComponent)):
186 BaseContainer attachType = componentSource.GetObject(
"AttachType");
190 ResourceName slotPrefab;
191 componentSource.Get(
"EntityPrefab", slotPrefab);
193 componentSource.Get(
"WeaponTemplate", slotPrefab);
198 case (componentType.IsInherited(SCR_EditorLinkComponent)):
200 BaseContainerList entries = componentSource.GetObjectArray(
"m_aEntries");
201 SCR_EditorLinkEntry link;
202 for (
int e = 0, linkCount = entries.Count(); e < linkCount; e++)
204 link = SCR_EditorLinkEntry.Cast(BaseContainerTools.CreateInstanceFromContainer(entries.Get(e)));
209 case (componentType.IsInherited(SCR_EditableEntityComponent)):
211 EEditableEntityFlag editableFlags = SCR_EditableEntityComponentClass.GetEntityFlags(componentSource);
222 componentSource.Get(
"m_vIconPos", iconPos);
223 if (iconPos != vector.Zero)
224 entryLocal.m_vPosition -= iconPos;
228 case (componentType.IsInherited(SCR_SlotCompositionComponent)):
231 componentSource.Get(
"m_bOrientChildrenToTerrain", orientChildren);
237 case (componentType.IsInherited(SCR_HorizontalAlignComponent)):
243 case (componentType.IsInherited(SCR_BaseAreaMeshComponent)):
246 SCR_EditorModeEntity mode = SCR_EditorModeEntity.GetInstance();
253 case (componentType.IsInherited(SCR_TriggerAreaMeshComponent)):
256 componentSource.Get(
"m_eShape", areaMeshShape);
264 float radius, height;
265 entitySource.Get(
"SphereRadius", radius);
266 componentSource.Get(
"m_fHeight", height);
268 entryLocal.m_vScale =
Vector(radius, height, radius);
272 case (componentType.IsInherited(SCR_WaypointAreaMeshComponent)):
275 componentSource.Get(
"m_eShape", areaMeshShape);
283 float radius, height;
284 entitySource.Get(
"CompletionRadius", radius);
285 componentSource.Get(
"m_fHeight", height);
287 entryLocal.m_vScale =
Vector(radius, height, radius);
291 case (componentType.IsInherited(SCR_SpawnPointAreaMeshComponent)):
294 componentSource.Get(
"m_eShape", areaMeshShape);
302 float radius, height;
303 entitySource.Get(
"m_fSpawnRadius", radius);
304 componentSource.Get(
"m_fHeight", height);
306 entryLocal.m_vScale =
Vector(radius, height, radius);
310 case (componentType.IsInherited(SCR_CustomAreaMeshComponent)):
313 componentSource.Get(
"m_eShape", areaMeshShape);
321 float radius, height;
322 componentSource.Get(
"m_fRadius", radius);
323 componentSource.Get(
"m_fHeight", height);
325 entryLocal.m_vScale =
Vector(radius, height, radius);
329 case (componentType.IsInherited(SCR_ZoneRestrictionAreaMeshComponent)):
332 componentSource.Get(
"m_eShape", areaMeshShape);
340 float radius, height;
341 entitySource.Get(
"m_fZoneRadius", radius);
342 componentSource.Get(
"m_fHeight", height);
344 entryLocal.m_vScale =
Vector(radius, height, radius);
348 case (componentType.IsInherited(SCR_SupportStationAreaMeshComponent)):
351 componentSource.Get(
"m_eShape", areaMeshShape);
360 for (
int c = 0; c < componentCount; c++)
362 IEntityComponentSource supportStation_componentSource = entitySource.GetComponent(c);
364 bool supportStation_componentEnabled;
365 supportStation_componentSource.Get(
"Enabled", supportStation_componentEnabled);
366 if (!supportStation_componentEnabled)
369 string supportStation_componentClassName = supportStation_componentSource.GetClassName();
370 if (!supportStation_componentClassName.ToType().IsInherited(SCR_BaseSupportStationComponent))
373 float radius, height;
374 supportStation_componentSource.Get(
"m_fRange", radius);
375 componentSource.Get(
"m_fHeight", height);
377 entryLocal.m_vScale =
Vector(radius, height, radius);
382 float radius, height;
383 componentSource.Get(
"m_fRadius", radius);
384 componentSource.Get(
"m_fHeight", height);
385 entryLocal.m_vScale =
Vector(radius, height, radius);
389 case (componentType.IsInherited(SCR_EffectsModuleAreaMeshComponent)):
392 componentSource.Get(
"m_eShape", areaMeshShape);
400 float width, lenght, height;
401 componentSource.Get(
"m_fWidth", width);
402 componentSource.Get(
"m_fLenght", lenght);
403 componentSource.Get(
"m_fHeight", height);
405 entryLocal.m_vScale =
Vector(width, height, lenght);
416 array<ResourceName> groupPrefabs =
new array<ResourceName>;
417 array<vector> groupOffsets =
new array<vector>;
418 int groupSize = SCR_AIGroupClass.GetMembers(entitySource, groupPrefabs, groupOffsets);
421 SCR_BasePreviewEntry groupMemberEntry;
422 IEntitySource groupMemberSource;
423 for (
int i = 0; i < groupSize; i++)
425 Resource resource = Resource.Load(groupPrefabs[i]);
426 groupMemberSource = SCR_BaseContainerTools.FindEntitySource(resource);
427 groupMemberEntry =
new SCR_BasePreviewEntry(
true);
428 groupMemberEntry.m_vPosition = groupOffsets[i];
429 groupMemberEntry.m_Resource = resource;
430 GetPreviewEntries(groupMemberSource, outEntries, parentID, groupMemberEntry);
435 for (
int i = 0, count = entitySource.GetNumChildren(); i < count; i++)
437 GetPreviewEntries(entitySource.GetChild(i), outEntries, parentID);
444 slotSource.Get(
"Prefab", slotPrefab);
450 slotSource.Get(
"Enabled", isEnabled);
456 if (slotSource.GetName().Contains(
"SupplyStorage"))
465 entry.m_Resource = resource;
466 slotSource.Get(
"Offset", entry.m_vPosition);
467 slotSource.Get(
"Angles", entry.m_vAngles);
468 slotSource.Get(
"PivotID", entry.m_iPivotID);
470 GetPreviewEntries(entitySource, outEntries, parentID, entry);
480 entry.m_Resource = resource;
481 entry.m_vPosition = link.m_vPosition;
482 entry.m_vAngles = link.m_vAngles;
483 entry.SetScale(link.m_fScale);
485 GetPreviewEntries(entitySource, outEntries, parentID, entry);
493 if (!baseResource.IsValid())
496 IEntitySource baseSource = baseResource.GetResource().ToEntitySource();
500 array<ref SCR_BasePreviewEntry> entries = {};
504 IEntitySource child, parent;
505 IEntityComponentSource component;
506 array<ref IEntitySource> children = {};
507 string className =
"SCR_PrefabPreviewEntity";
508 foreach (
int i, SCR_BasePreviewEntry entry: entries)
510 if (entry.m_iParentID == -1)
513 parent = children[entry.m_iParentID];
515 child = api.CreateEntity(className,
"", 0, parent, entry.m_vPosition, entry.m_vAngles);
516 children.Insert(child);
518 child.Set(
"scale", entry.GetScale());
519 child.Set(
"m_Flags", entry.m_Flags);
523 className =
"SCR_BasePreviewEntity";
527 api.CreateComponent(child,
"Hierarchy");
529 if (!entry.m_iPivotID.IsEmpty())
539 component = api.CreateComponent(child,
"MeshObject");
542 Resource resource = Resource.Load(entry.m_Mesh);
543 if (!resource.IsValid())
545 Print(
"Cannot load " + entry.m_Mesh +
" | " + FilePath.StripPath(__FILE__) +
":" + __LINE__,
LogLevel.WARNING);
549 VObject mesh = resource.GetResource().ToVObject();
550 string materials[256];
551 for (
int m = 0, materialsCount = mesh.GetMaterials(materials); m < materialsCount; m++)
553 api.CreateObjectArrayVariableMember(child, {
ContainerIdPathEntry(
"MeshObject")},
"Materials",
"MaterialAssignClass", m);
560 IEntitySource previewSource = children[0];
561 api.SetVariableValue(previewSource, null,
"m_SourcePrefab", basePrefab);
564 ResourceName prefab = SCR_BaseContainerTools.GetPrefabResourceName(api.EntityToSource(
this));
566 Workbench.GetAbsolutePath(prefab.GetPath(), absolutePath);
567 api.CreateEntityTemplate(previewSource, absolutePath);
569 api.DeleteEntity(previewSource);
575 IEntitySource entitySource = api.EntityToSource(
this);
577 ResourceName basePrefab;
578 entitySource.Get(
"m_SourcePrefab", basePrefab);
586 api.BeginEntityAction();
587 CreatePrefabFromSource(api, basePrefab);
588 api.EndEntityAction();
592 return {
new WB_UIMenuItem(
"Generate preview prefab", 0) };