Skip to content

Commit

Permalink
Load child entities from point_template for correct position
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Nov 15, 2023
1 parent 882247c commit 0e93f06
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
38 changes: 24 additions & 14 deletions GUI/Types/Renderer/WorldLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void Load()
}

var entityLump = (EntityLump)newResource.DataBlock;
LoadEntitiesFromLump(entityLump, "world_layer_base"); // TODO: Hardcoded layer name
LoadEntitiesFromLump(entityLump, "world_layer_base", Matrix4x4.Identity); // TODO: Hardcoded layer name
}

// Output is World_t we need to iterate m_worldNodes inside it.
Expand Down Expand Up @@ -175,9 +175,10 @@ private void LoadWorldLightingInfo()
};
}

private void LoadEntitiesFromLump(EntityLump entityLump, string layerName = null)
private void LoadEntitiesFromLump(EntityLump entityLump, string layerName, Matrix4x4 parentTransform)
{
var childEntities = entityLump.GetChildEntityNames();
var childEntityLumps = new Dictionary<string, EntityLump>(childEntities.Length);

foreach (var childEntityName in childEntities)
{
Expand All @@ -191,7 +192,7 @@ private void LoadEntitiesFromLump(EntityLump entityLump, string layerName = null
var childLump = (EntityLump)newResource.DataBlock;
var childName = childLump.Data.GetProperty<string>("m_name");

LoadEntitiesFromLump(childLump, childName);
childEntityLumps.Add(childName, childLump);
}

static bool IsCubemapOrProbe(string cls)
Expand Down Expand Up @@ -219,6 +220,8 @@ static bool IsFog(string cls)
continue; // do not draw
}

var transformationMatrix = parentTransform * EntityTransformHelper.CalculateTransformationMatrix(entity);

if (classname == "info_world_layer")
{
var spawnflags = entity.GetPropertyUnchecked<uint>("spawnflags");
Expand All @@ -234,6 +237,19 @@ static bool IsFog(string cls)
{
LoadSkybox(entity);
}
else if (classname == "point_template")
{
var entityLumpName = entity.GetProperty<string>("entitylumpname");

if (childEntityLumps.TryGetValue(entityLumpName, out var childLump))
{
LoadEntitiesFromLump(childLump, entityLumpName, transformationMatrix);
}
else
{
Log.Warn(nameof(WorldLoader), $"Failed to find child entity lump with name {entityLumpName}.");
}
}
else if (classname == "env_sky" || classname == "ent_dota_lightinfo")
{
var skyname = entity.GetProperty<string>("skyname") ?? entity.GetProperty<string>("skybox_material_day");
Expand All @@ -250,7 +266,7 @@ static bool IsFog(string cls)
};
}

var rotation = EntityTransformHelper.CalculateTransformationMatrix(entity) with
var rotation = transformationMatrix with
{
Translation = Vector3.Zero
};
Expand Down Expand Up @@ -341,8 +357,6 @@ static bool IsFog(string cls)
{
scene.FogInfo.CubeFogActive = true;

var transform = EntityTransformHelper.CalculateTransformationMatrix(entity);

var lodBias = entity.GetPropertyUnchecked<float>("cubemapfoglodbiase");

var falloffExponent = entity.GetPropertyUnchecked<float>("cubemapfogfalloffexponent");
Expand Down Expand Up @@ -409,7 +423,7 @@ static bool IsFog(string cls)
if (skyEntity != null)
{
material = skyEntity.GetProperty<string>("skyname") ?? skyEntity.GetProperty<string>("skybox_material_day");
transform = EntityTransformHelper.CalculateTransformationMatrix(skyEntity); // steal rotation from env_sky
transformationMatrix = EntityTransformHelper.CalculateTransformationMatrix(skyEntity); // steal rotation from env_sky
}
else
{
Expand Down Expand Up @@ -463,7 +477,7 @@ static bool IsFog(string cls)
HeightEnd = heightEnd,
HeightExponent = heightExponent,
LodBias = lodBias,
Transform = transform,
Transform = transformationMatrix,
CubemapFogTexture = fogTexture,
Opacity = opacity,
ExposureBias = exposureBias,
Expand All @@ -487,8 +501,6 @@ static bool IsFog(string cls)
handShake = 0;
}

var transform = EntityTransformHelper.CalculateTransformationMatrix(entity);

AABB bounds = default;
if (classname == "env_cubemap")
{
Expand Down Expand Up @@ -554,7 +566,7 @@ static bool IsFog(string cls)
var envMap = new SceneEnvMap(scene, bounds)
{
LayerName = layerName,
Transform = transform,
Transform = transformationMatrix,
HandShake = handShake,
ArrayIndex = arrayIndex,
IndoorOutdoorLevel = indoorOutdoorLevel,
Expand All @@ -578,7 +590,7 @@ static bool IsFog(string cls)
var lightProbe = new SceneLightProbe(scene, bounds)
{
LayerName = layerName,
Transform = transform,
Transform = transformationMatrix,
HandShake = handShake,
Irradiance = irradianceTexture,
};
Expand Down Expand Up @@ -609,8 +621,6 @@ static bool IsFog(string cls)
}
}

var transformationMatrix = EntityTransformHelper.CalculateTransformationMatrix(entity);

if (transformationMatrix == default)
{
continue;
Expand Down
4 changes: 2 additions & 2 deletions ValveResourceFormat/Resource/ResourceTypes/EntityLump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ public class EntityProperty
public object Data { get; set; }
}

public IEnumerable<string> GetChildEntityNames()
public string[] GetChildEntityNames()
{
return Data.GetArray<string>("m_childLumps");
}

public IEnumerable<Entity> GetEntities()
public List<Entity> GetEntities()
=> Data.GetArray("m_entityKeyValues")
.Select(ParseEntityProperties)
.ToList();
Expand Down

0 comments on commit 0e93f06

Please sign in to comment.