Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions Assets/Gothic-Core/Scripts/Domain/Vobs/VobInitializerDomain.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Gothic.Core.Adapters.Properties;
using Gothic.Core.Adapters.Vob;
Expand Down Expand Up @@ -53,6 +54,8 @@ public class VobInitializerDomain

// Ids of logic VOBs below lazily loaded VOBs that were created with the world (MarkCreatedEarly).
private readonly HashSet<int> _createdEarlyVobIds = new();
private readonly HashSet<string> _loggedHelperMobs = new();
private readonly HashSet<string> _loggedNoHierarchyMobs = new();


/// <summary>
Expand Down Expand Up @@ -1045,29 +1048,51 @@ private GameObject CreateDefaultMesh(IVirtualObject vob, GameObject parent)
{
var go = BuildDefaultMesh(vob, parent);
if (go != null && !vob.ShowVisual)
HideVisual(vob, go);
HideVisual(go, vob is IInteractiveObject);

return go;
}

/// <summary>
/// Draws nothing: the renderers stay enabled with forceRenderingOff, so their bounds still work (focus, dig
/// spots). Colliders are switched off unless keepColliders is set.
/// showVisual=FALSE (Spacer): like in the engine (OpenGothic ObjVisual::setVisual) the mesh isn't drawn and a
/// plain vob doesn't collide. Mods place such vobs as interaction anchors on world geometry (DoM: ore piles,
/// dig spots, campfires) - a mob stays usable: it keeps its colliders (focus, grab, use) and its nodes. The
/// renderers stay enabled, only drawing is off, so their bounds still work (focus, dig spots).
/// dig spots, campfires) - a mob stays usable: it keeps its colliders (focus, grab, use) and its nodes.
/// A helper mob (only its slots count) loses its colliders too: the hero can't focus or grab it (the helper
/// mesh would be an invisible plank on the wall).
/// </summary>
private static void HideVisual(IVirtualObject vob, GameObject go)
private static void HideVisual(GameObject go, bool keepColliders)
{
foreach (var renderer in go.GetComponentsInChildren<Renderer>(true))
renderer.forceRenderingOff = true;

if (vob is IInteractiveObject)
if (keepColliders)
return;

foreach (var collider in go.GetComponentsInChildren<Collider>(true))
collider.enabled = false;
}

/// <summary>
/// Slot nodes (ZS_POS0, ...) are where an NPC stands or uses the mob.
/// </summary>
private bool HasSlotNodes(ZenKit.IModel mdl, string meshName)
{
var nodes = mdl.Hierarchy?.Nodes;
if (nodes == null)
{
// Broken or modded models: no hierarchy, so no slots. Warn once per visual, don't fail the world load.
if (_loggedNoHierarchyMobs.Add(meshName))
Logger.LogWarning($"[VobInitializer] Model {meshName} has no hierarchy - no slot nodes.",
LogCat.Vob);
return false;
}

return nodes.Any(node => node?.Name != null
&& node.Name.StartsWith("ZS_", StringComparison.OrdinalIgnoreCase));
}

private GameObject BuildDefaultMesh(IVirtualObject vob, GameObject parent)
{
var go = GetPrefab(vob);
Expand All @@ -1088,14 +1113,28 @@ private GameObject BuildDefaultMesh(IVirtualObject vob, GameObject parent)
// those on purpose (G1's empty harp or WASH_SLOT), but the X needs its mesh to be diggable. Where the
// world hides it (showVisual=FALSE, Newworld's spots) it isn't drawn, like in the original.
var isDigSpot = _vmCacheService.IsDigSpot(vob);
// A mob whose model is only an untextured helper (G1/G2 REPAIR_PLANK: ZM_REP_HIDDEN and the slot
// ZS_POS0) is invisible in the original, but its slots are where the NPC stands. Without them
// Wld_IsMobAvailable said no, so Fellan in Khorinis' harbour never hammered at his hut.
// Only models with slot nodes: G1's empty harp, flute and WASH_SLOT stay skipped on purpose.
var isHiddenMob = !isDigSpot && vob is IInteractiveObject && !MeshService.HasTexturedMeshes(mdl.Mesh)
&& HasSlotNodes(mdl, meshName);
var isUntextured = isDigSpot || isHiddenMob;
var ret = _meshService.CreateVob(meshName, mdl, parent: parent, rootGo: go,
useTextureArray: !isDigSpot, allowUntextured: isDigSpot);
useTextureArray: !isUntextured, allowUntextured: isUntextured);

// A few objects are broken and have no meshes. We need to destroy them immediately again.
if (ret == null)
Object.Destroy(go);
else if (isDigSpot)
SnapDigSpotToLoader(ret);
else if (isHiddenMob)
{
HideVisual(ret, keepColliders: false);
if (_loggedHelperMobs.Add(meshName))
Logger.LogEditor($"[VobInitializer] Hidden helper mob {meshName} built for its slots.",
LogCat.Vob);
}

return ret;
}
Expand Down
9 changes: 6 additions & 3 deletions Assets/Gothic-Core/Scripts/Services/Meshes/MeshService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public GameObject CreateVob(string objectName, IModel mdl,
GameObject parent = null, GameObject rootGo = null, bool useTextureArray = true,
bool allowUntextured = false)
{
if (!allowUntextured && !HasMeshes(mdl.Mesh))
if (!allowUntextured && !HasTexturedMeshes(mdl.Mesh))
{
return null;
}
Expand Down Expand Up @@ -168,7 +168,7 @@ public GameObject CreateVob(string objectName, IModelMesh mdm, IModelHierarchy m
Vector3 position = default, Quaternion rotation = default,
GameObject parent = null, GameObject rootGo = null, bool useTextureArray = true)
{
if (!HasMeshes(mdm))
if (!HasTexturedMeshes(mdm))
{
return null;
}
Expand All @@ -192,7 +192,10 @@ private static bool HasTextures(IMultiResolutionMesh mrm)
return !mrm.Materials.All(m => m.Texture.IsEmpty());
}

private static bool HasMeshes(IModelMesh mdm)
/// <summary>
/// The model has something to draw: at least one textured mesh or attachment.
/// </summary>
public static bool HasTexturedMeshes(IModelMesh mdm)
{
// Check if there are completely empty elements without any texture.
// G1: e.g. Harp, Flute, and WASH_SLOT (usage moved to a FreePoint within daedalus functions)
Expand Down