19 static SCR_BasePreviewEntity SpawnPreviewFromEntity(IEntity entity, ResourceName previewPrefab, BaseWorld world =
null, EntitySpawnParams spawnParams =
null, ResourceName material = ResourceName.Empty,
EPreviewEntityFlag flags = 0)
23 Print(
"No entity defined", LogLevel.WARNING);
27 array<ref SCR_BasePreviewEntry> entries = GetPreviewEntriesFromEntity(entity, spawnParams, flags);
28 return SpawnPreview(entries, previewPrefab, world, spawnParams, material, flags);
38 static array<ref SCR_BasePreviewEntry> GetPreviewEntriesFromEntity(IEntity entity, out EntitySpawnParams spawnParams =
null,
EPreviewEntityFlag flags = 0)
41 spawnParams =
new EntitySpawnParams();
46 trace =
new TraceParam();
50 array<ref SCR_BasePreviewEntry> entries = {};
51 GetPreviewEntries(entity, entries, spawnParams.Transform, -1, flags, trace);
61 static void GetPreviewEntries(IEntity entity, out notnull array<ref SCR_BasePreviewEntry> outEntries, out vector rootTransform[4],
int parentID = -1,
EPreviewEntityFlag flags = 0, TraceParam trace =
null)
66 entity.GetWorldTransform(transform);
67 SaveRootTransform(transform, rootTransform);
72 if (entity.IsInherited(ChimeraCharacter))
82 (!entity.GetVObject().ToMeshObject())
85 (!(entity.GetFlags() & EntityFlags.VISIBLE) || (entity.GetFlags() & EntityFlags.EDITOR_ONLY))
90 entity.GetLocalTransform(transform);
94 entry.m_iParentID = parentID;
95 parentID = outEntries.Insert(entry);
97 entry.m_Entity = entity;
98 entry.SaveTransform(transform);
99 entry.SetScale(GetLocalScale(entity));
100 entry.m_iPivotID = GetPivotName(entity);
102 if (GetMesh(entity, flags, entry, outEntries))
106 if (compositionComponent && compositionComponent.CanOrientChildrenToTerrain())
109 SCR_HorizontalAlignComponent horizontalComponent = SCR_HorizontalAlignComponent.Cast(entity.FindComponent(SCR_HorizontalAlignComponent));
110 if (horizontalComponent)
118 IEntity child = entity.GetChildren();
121 GetPreviewEntries(child, outEntries, rootTransform, parentID, flags, trace);
122 child = child.GetSibling();
127 protected static float GetLocalScale(IEntity entity)
129 float scale = entity.GetScale();
130 if (entity.GetParent())
131 scale /= entity.GetParent().GetScale();
135 protected static string GetPivotName(IEntity entity)
137 IEntity parent = entity.GetParent();
141 TNodeId pivotIndex = entity.GetPivot();
142 if (pivotIndex == -1)
145 array<string> boneNames = {};
146 Animation anim = parent.GetAnimation();
147 anim.GetBoneNames(boneNames);
148 for (
int i = 0, count = boneNames.Count(); i < count; i++)
150 if (anim.GetBoneIndex(boneNames[i]) == pivotIndex)
157 SCR_BaseAreaMeshComponent areaMeshComponent = SCR_BaseAreaMeshComponent.Cast(entity.FindComponent(SCR_BaseAreaMeshComponent));
158 if (areaMeshComponent)
168 areaMeshComponent.GetDimensions3D(entry.m_vScale);
174 SCR_PreviewEntityComponent previewComponent = SCR_PreviewEntityComponent.Cast(entity.FindComponent(SCR_PreviewEntityComponent));
175 if (previewComponent && previewComponent.IsRuntime())
178 if (previewComponent.GetPreviewEntries(outEntries, entry) != 0)
182 entry.m_Mesh = previewComponent.GetPreviewPrefab();
188 VObject meshObject = entity.GetVObject();
191 entry.m_Mesh = meshObject.GetResourceName();
197 protected static void SaveTerrainTransform(IEntity entity,
SCR_BasePreviewEntry entry,
bool isUnderwater =
false, TraceParam trace =
null)
199 vector worldTransform[4], terrainTransform[4], surfaceBasis[4];
202 entity.GetWorldTransform(worldTransform);
205 if (!
SCR_TerrainHelper.GetTerrainBasis(worldTransform[3], surfaceBasis, entity.GetWorld(), !isUnderwater, trace))
209 vector angles = Math3D.MatrixToAngles(worldTransform);
210 Math3D.AnglesToMatrix(Vector(angles[0], 0, 0), terrainTransform);
213 Math3D.MatrixMultiply3(surfaceBasis, terrainTransform, terrainTransform);
216 Math3D.MatrixInvMultiply3(terrainTransform, worldTransform, terrainTransform);
217 entry.m_vAnglesTerrain = Math3D.MatrixToAngles(terrainTransform);
218 entry.m_vHeightTerrain = worldTransform[3][1] - surfaceBasis[3][1];
220 protected static void SaveRootTransform(out vector transform[4], out vector rootTransform[4])
222 if (
SCR_Math3D.IsMatrixIdentity(rootTransform))
225 Math3D.MatrixCopy(transform, rootTransform);
226 Math3D.MatrixIdentity4(transform);
229 vector angles = Math3D.MatrixToAngles(rootTransform);
230 if (
float.AlmostEqual(Math.AbsFloat(angles[1]), 90))
232 if (angles[1] > 0) angles[2] = angles[2] - 90;
233 Math3D.AnglesToMatrix(Vector(angles[2] + 180, 0, 0), rootTransform);
239 Math3D.MatrixInvMultiply4(rootTransform, transform, transform);