Working with box rooms demo

This commit is contained in:
lawnjelly 2019-10-22 20:44:17 +01:00 committed by GitHub
parent 2a5b465831
commit 0c10ed45d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 1876 additions and 182 deletions

View File

@ -1,11 +1,15 @@
#pragma once
// most of the performance sensitive debug output will be compiled out in release builds
// you won't be able to get frame debugging of the visibility tree though.
#ifdef DEBUG_ENABLED
#define LPRINT_RUN(a, b) {String sz;\
for (int n=0; n<Lawn::LDebug::m_iTabDepth; n++)\
sz += "\t";\
LPRINT(a, sz + b);}
#else
#define LPRINT_RUN(a, b) ;
//#define LPRINT_RUN(a, b) {String sz;\
//for (int n=0; n<Lawn::LDebug::m_iTabDepth; n++)\
//sz += "\t";\
//LPRINT(a, sz + b);}
#endif
#define LPRINT(a, b) if (!Lawn::LDebug::m_bRunning) {\
if (a >= Lawn::LDebug::m_iLoggingLevel)\

View File

@ -111,6 +111,13 @@ bool LSob::IsShadowCaster() const
}
GeometryInstance * LSob::GetGI() const
{
Object * pObj = ObjectDB::get_instance(m_ID);
GeometryInstance * pGI = Object::cast_to<GeometryInstance>(pObj);
return pGI;
}
VisualInstance * LSob::GetVI() const
{
Object * pObj = ObjectDB::get_instance(m_ID);

2
ldob.h
View File

@ -28,6 +28,7 @@
#include "scene/3d/spatial.h"
class VisualInstance;
class GeometryInstance;
class Light;
class LHidable
@ -50,6 +51,7 @@ class LSob : public LHidable
public:
Spatial * GetSpatial() const;
VisualInstance * GetVI() const;
GeometryInstance * GetGI() const;
//void Show(bool bShow);
bool IsShadowCaster() const;

1126
lhelper.cpp Normal file

File diff suppressed because it is too large Load Diff

83
lhelper.h Normal file
View File

@ -0,0 +1,83 @@
#pragma once
#include "lroom_manager.h"
class LHelper
{
public:
struct LFace
{
Vector3 m_Pos[3];
Vector3 m_Norm[3];
int m_index[3];
String ToString() const;
};
// unique vert
struct LVert
{
Vector3 m_Pos;
Vector3 m_Norm;
Vector2 m_UV;
Vector2 m_UV2;
bool ApproxEqual(const LVert &o) const;
};
// the merged mesh data to be passed for unmerging meshes
struct LMerged
{
PoolVector<Vector3> m_Verts;
PoolVector<Vector3> m_Norms;
PoolVector<Vector2> m_UV2s;
PoolVector<int> m_Inds;
int m_nFaces;
// precreate LFaces
LVector<LFace> m_LFaces;
};
// one function to do the whole internal workflow
MeshInstance * CreateLightmapProxy(LRoomManager &manager);
// for lightmapping
bool MergeSOBs(LRoomManager &manager, MeshInstance * pMerged);
// take the UV2 coords from the merged mesh and attach these to the SOB meshes
bool UnMergeSOBs(LRoomManager &manager, MeshInstance * pMerged);
// bool UnMergeSOBs(LRoomManager &manager, const PoolVector<Vector2> &uv2s);
// main function for getting merged uv2 back to sobs
bool TransferUV2(const MeshInstance &mi_from, MeshInstance &mi_to);
private:
void Merge_MI(const MeshInstance &mi, PoolVector<Vector3> &verts, PoolVector<Vector3> &norms, PoolVector<int> &inds);
bool UnMerge_SOB(MeshInstance &mi, LMerged &merged);
unsigned int FindMatchingVertex(const PoolVector<Vector2> &uvs, const Vector2 &uv1) const;
bool BakeLightmap(BakedLightmap &baked_lightmap, MeshInstance * pMerged);
bool LightmapUnwrap(Ref<ArrayMesh> am, const Transform &trans);
// bool LightmapUnwrap(const PoolVector<Vector3> &p_verts, const PoolVector<Vector3> &p_normals, const PoolVector<int> &p_inds, PoolVector<Vector2> &r_uvs);
//Error lightmap_unwrap(ArrayMesh &am, const Transform &p_base_transform, float p_texel_size);
int DebugCountUVs(MeshInstance &mi);
void Transform_Verts(const PoolVector<Vector3> &ptsLocal, PoolVector<Vector3> &ptsWorld, const Transform &tr) const;
void Transform_Norms(const PoolVector<Vector3> &normsLocal, PoolVector<Vector3> &normsWorld, const Transform &tr) const;
int DoFacesMatch(const LFace& sob_f, const LFace &m_face) const;
bool DoFaceVertsApproxMatch(const LFace& sob_f, const LFace &m_face, int c0, int c1) const;
bool DoPosNormsApproxMatch(const Vector3 &a_pos, const Vector3 &a_norm, const Vector3 &b_pos, const Vector3 &b_norm) const;
int FindOrAddVert(LVector<LVert> &uni_verts, const LVert &vert) const;
void SetOwnerRecursive(Node * pNode, Node * pOwner);
bool FillMergedFromMesh(LMerged &merged, const MeshInstance &mesh);
// bool xatlas_mesh_lightmap_unwrap(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, const int *p_face_materials, int p_index_count, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y);
};

View File

@ -9,3 +9,5 @@
#include "ldob.cpp"
#include "lbound.cpp"
#include "lbitfield_dynamic.cpp"
#include "lhelper.cpp"
#include "lscene_saver.cpp"

View File

@ -58,9 +58,6 @@ Spatial * LRoom::GetGodotRoom() const
void LRoom::DOB_Add(const LDob &dob)
{
// LDob dob;
// dob.m_ID = pDOB->get_instance_id();
m_DOBs.push_back(dob);
}
@ -314,6 +311,23 @@ void LRoom::FinalizeVisibility(LRoomManager &manager)
}
}
// call when releasing a level, this should unregister all dobs within all rooms
void LRoom::Release(LRoomManager &manager)
{
for (int n=0; n<m_DOBs.size(); n++)
{
LDob &dob = m_DOBs[n];
Spatial * pS = dob.GetSpatial();
if (pS)
{
// signifies released or unregistered
manager.Obj_SetRoomNum(pS, -2);
}
}
}
// allows us to show / hide all dobs as the room visibility changes
void LRoom::Room_MakeVisible(bool bVisible)
{
@ -418,7 +432,7 @@ void LRoom::FirstTouch(LRoomManager &manager)
}
void LRoom::DetermineVisibility_Recursive(LRoomManager &manager, int depth, const LCamera &cam, const LVector<Plane> &planes, int portalID_from)
void LRoom::DetermineVisibility_Recursive(LRoomManager &manager, int depth, const LCamera &cam, const LVector<Plane> &planes, int first_portal_plane)
{
// prevent too much depth
if (depth > 8)
@ -567,12 +581,19 @@ void LRoom::DetermineVisibility_Recursive(LRoomManager &manager, int depth, cons
// is it culled by the planes?
LPortal::eClipResult overall_res = LPortal::eClipResult::CLIP_INSIDE;
// while clipping to the planes we maintain a list of partial planes, so we can add them to the
// recursive next iteration of planes to check
static LVector<int> partial_planes;
partial_planes.clear();
// for portals, we want to ignore the near clipping plane, as we might be right on the edge of a doorway
// and still want to look through the portal.
// So we are starting this loop from 1, ASSUMING that plane zero is the near clipping plane.
// If it isn't we would need a different strategy
for (int l=1; l<planes.size(); l++)
// Note that now this only occurs for the first portal out of the current room. After that,
// 0 is passed as first_portal_plane, because the near plane will probably be irrelevant,
// and we are now not necessarily copying the camera planes.
for (int l=first_portal_plane; l<planes.size(); l++)
{
LPortal::eClipResult res = port.ClipWithPlane(planes[l]);
@ -583,6 +604,7 @@ void LRoom::DetermineVisibility_Recursive(LRoomManager &manager, int depth, cons
break;
case LPortal::eClipResult::CLIP_PARTIAL:
overall_res = res;
partial_planes.push_back(l);
break;
default: // suppress warning
break;
@ -605,17 +627,28 @@ void LRoom::DetermineVisibility_Recursive(LRoomManager &manager, int depth, cons
{
// get a vector of planes from the pool
LVector<Plane> &new_planes = manager.m_Pool.Get(uiPoolMem);
new_planes.clear();
// NEW!! if portal is totally inside the planes, don't copy the old planes
if (overall_res != LPortal::eClipResult::CLIP_INSIDE)
{
// copy the existing planes
new_planes.copy_from(planes);
//new_planes.copy_from(planes);
// new .. only copy the partial planes that the portal cuts through
for (int n=0; n<partial_planes.size(); n++)
new_planes.push_back(planes[partial_planes[n]]);
}
// add the planes for the portal
// NOTE that we can also optimize by not adding portal planes for edges that
// were behind a partial plane. NYI
port.AddPlanes(manager, cam.m_ptPos, new_planes);
if (pLinkedRoom)
{
pLinkedRoom->DetermineVisibility_Recursive(manager, depth + 1, cam, new_planes, port_id);
pLinkedRoom->DetermineVisibility_Recursive(manager, depth + 1, cam, new_planes, 0);
// for debugging need to reset tab depth
Lawn::LDebug::m_iTabDepth = depth;
}

View File

@ -100,16 +100,15 @@ public:
const String &get_name() const {return m_szName;}
// main function
void DetermineVisibility_Recursive(LRoomManager &manager, int depth, const LCamera &cam, const LVector<Plane> &planes, int portalID_from = -1);
void DetermineVisibility_Recursive(LRoomManager &manager, int depth, const LCamera &cam, const LVector<Plane> &planes, int first_portal_plane = 1);
void FirstTouch(LRoomManager &manager);
// allows us to show / hide all dobs as the room visibility changes
void Room_MakeVisible(bool bVisible);
// hide godot room and all linked dobs
// USED AT RUNTIME
// void Hide_All();
// call when releasing a level, this should unregister all dobs within all rooms
void Release(LRoomManager &manager);
// show godot room and all linked dobs and all sobs
void Debug_ShowAll(bool bActive);

View File

@ -29,17 +29,47 @@
// save typing, I am lazy
#define LMAN m_pManager
#define LROOMLIST m_pRoomList
void LRoomConverter::Convert(LRoomManager &manager)
void LRoomConverter::Convert(LRoomManager &manager, bool bVerbose, bool bPreparationRun, bool bDeleteLights)
{
m_bFinalRun = (bPreparationRun == false);
m_bDeleteLights = bDeleteLights;
// This just is simply used to set how much debugging output .. more during conversion, less during running
// except when requested by explicitly clearing this flag.
Lawn::LDebug::m_bRunning = false;
Lawn::LDebug::m_bRunning = (bVerbose == false);
// test pool vector
// PoolVector<Vector2> arr;
// arr.append(Vector2(0, 0));
// arr.append(Vector2(1, 0));
// arr.append(Vector2(2, 0));
// arr.insert(1, arr[1]);
// LPRINT(5, "DEBUG POOLVECTOR");
// for (int n=0; n<arr.size(); n++)
// {
// LPRINT(2, String(Variant(arr[n])));
// }
if (!m_bFinalRun)
{
LPRINT(5, "running convert PREPARATION RUN");
}
else
{
LPRINT(5, "running convert");
}
LMAN = &manager;
LROOMLIST = manager.GetRoomList();
// force clear all arrays
manager.ReleaseResources(true);
@ -77,7 +107,7 @@ void LRoomConverter::Convert(LRoomManager &manager)
Convert_ShadowCasters();
// hide all in preparation for first frame
Convert_HideAll();
//LMAN->ShowAll(false);
// temp rooms no longer needed
m_TempRooms.clear(true);
@ -97,9 +127,9 @@ void LRoomConverter::Convert_Rooms()
// first find all room empties and convert to LRooms
int count = 0;
for (int n=0; n<LMAN->get_child_count(); n++)
for (int n=0; n<LROOMLIST->get_child_count(); n++)
{
Node * pChild = LMAN->get_child(n);
Node * pChild = LROOMLIST->get_child(n);
if (!Node_IsRoom(pChild))
continue;
@ -188,8 +218,11 @@ void LRoomConverter::Convert_Room_FindObjects_Recursive(Node * pParent, LRoom &l
LRoom_PushBackSOB(lroom, sob);
// take away layer 0 from the sob, so it can be culled effectively
if (m_bFinalRun)
{
pVI->set_layer_mask(0);
}
}
else
{
// not visual instances
@ -339,23 +372,23 @@ bool LRoomConverter::Convert_Bound(LRoom &lroom, MeshInstance * pMI)
}
// hide all in preparation for first frame
void LRoomConverter::Convert_HideAll()
{
for (int n=0; n<LMAN->m_SOBs.size(); n++)
{
LSob &sob = LMAN->m_SOBs[n];
sob.Show(false);
}
//void LRoomConverter::Convert_HideAll()
//{
// for (int n=0; n<LMAN->m_SOBs.size(); n++)
// {
// LSob &sob = LMAN->m_SOBs[n];
// sob.Show(false);
// }
// hide all lights that are non global
for (int n=0; n<LMAN->m_Lights.size(); n++)
{
LLight &light = LMAN->m_Lights[n];
if (!light.IsGlobal())
light.Show(false);
}
// // hide all lights that are non global
// for (int n=0; n<LMAN->m_Lights.size(); n++)
// {
// LLight &light = LMAN->m_Lights[n];
// if (!light.IsGlobal())
// light.Show(false);
// }
}
//}
void LRoomConverter::Convert_Lights()
{
@ -641,6 +674,7 @@ void LRoomConverter::Convert_Bounds()
// delete the mesh
pGRoom->remove_child(pChild);
pChild->queue_delete();
break;
}
}
@ -681,12 +715,12 @@ void LRoomConverter::Convert_Portals()
int LRoomConverter::CountRooms()
{
int nChildren = LMAN->get_child_count();
int nChildren = LROOMLIST->get_child_count();
int count = 0;
for (int n=0; n<nChildren; n++)
{
if (Node_IsRoom(LMAN->get_child(n)))
if (Node_IsRoom(LROOMLIST->get_child(n)))
count++;
}
@ -989,6 +1023,10 @@ void LRoomConverter::LRoom_DetectPortalMeshes(LRoom &lroom, LTempRoom &troom)
}
}
// delete portal meshes
if (m_bFinalRun)
// if (true)
{
// we need an enclosing while loop because we might be deleting children and mucking up the iterator
bool bDetectedOne = true;
@ -1006,7 +1044,6 @@ void LRoomConverter::LRoom_DetectPortalMeshes(LRoom &lroom, LTempRoom &troom)
// pMeshInstance->hide();
pChild->get_parent()->remove_child(pChild);
pChild->queue_delete();
bDetectedOne = true;
}
@ -1015,6 +1052,7 @@ void LRoomConverter::LRoom_DetectPortalMeshes(LRoom &lroom, LTempRoom &troom)
} // for loop
} // while
} // if we want to delete portal meshes
}
@ -1057,63 +1095,17 @@ void LRoomConverter::LRoom_DetectedLight(LRoom &lroom, Node * pNode)
Light * pLight = Object::cast_to<Light>(pNode);
assert (pLight);
if (m_bDeleteLights)
{
LPRINT(2, "Deleting Light : " + pLight->get_name());
// delete light now we are using lightmaps for test
pLight->queue_delete();
}
else
{
LPRINT(2, "Detected Light : " + pLight->get_name());
LMAN->LightCreate(pLight, lroom.m_RoomID);
/*
// create new light
LLight l;
l.SetDefaults();
l.m_GodotID = pLight->get_instance_id();
// direction
Transform tr = pLight->get_global_transform();
l.m_ptPos = tr.origin;
l.m_ptDir = -tr.basis.get_axis(2); // or possibly get_axis .. z is what we want
l.m_fMaxDist = pLight->get_param(Light::PARAM_SHADOW_MAX_DISTANCE);
// source room ID
l.m_RoomID = lroom.m_RoomID;
bool bOK = false;
// what kind of light?
SpotLight * pSL = Object::cast_to<SpotLight>(pNode);
if (pSL)
{
LPRINT(2, "\tSPOTLIGHT detected " + pNode->get_name());
l.m_eType = LLight::LT_SPOTLIGHT;
l.m_fSpread = pSL->get_param(Light::PARAM_SPOT_ANGLE);
bOK = true;
}
OmniLight * pOL = Object::cast_to<OmniLight>(pNode);
if (pOL)
{
LPRINT(2, "\tOMNILIGHT detected " + pNode->get_name());
l.m_eType = LLight::LT_OMNI;
bOK = true;
}
DirectionalLight * pDL = Object::cast_to<DirectionalLight>(pNode);
if (pDL)
{
LPRINT(2, "\tDIRECTIONALLIGHT detected " + pNode->get_name());
l.m_eType = LLight::LT_DIRECTIONAL;
bOK = true;
}
// don't add if not recognised
if (!bOK)
{
LPRINT(2, "\tLIGHT type unrecognised " + pNode->get_name());
return;
}
// turn the local light off to start with
pLight->hide();
LMAN->m_Lights.push_back(l);
*/
}
// found a portal mesh! create a matching LPortal
@ -1135,6 +1127,7 @@ void LRoomConverter::LRoom_DetectedPortalMesh(LRoom &lroom, LTempRoom &troom, Me
Array arrays = rmesh->surface_get_arrays(0);
PoolVector<Vector3> p_vertices = arrays[VS::ARRAY_VERTEX];
// create a new LPortal to fill with this wonderful info
LPortal &lport = *troom.m_Portals.request();
lport.m_szName = szLinkRoom;

View File

@ -81,7 +81,7 @@ public:
};
// this function calls everything else in the converter
void Convert(LRoomManager &manager);
void Convert(LRoomManager &manager, bool bVerbose, bool bPreparationRun, bool bDeleteLights);
private:
int CountRooms();
@ -96,7 +96,7 @@ private:
bool Convert_Bound(LRoom &lroom, MeshInstance * pMI);
void Convert_ShadowCasters();
void Convert_Lights();
void Convert_HideAll();
// void Convert_HideAll();
void LRoom_DetectPortalMeshes(LRoom &lroom, LTempRoom &troom);
@ -133,9 +133,17 @@ private:
// set up on entry
LRoomManager * m_pManager;
Spatial * m_pRoomList; // room list pointed to by the manager nodepath
LVector<LTempRoom> m_TempRooms;
bool Bound_AddPlaneIfUnique(LVector<Plane> &planes, const Plane &p);
// whether we are preparing the level, or doing a final run,
// in which case we should delete lights and set vis flags
bool m_bFinalRun;
bool m_bDeleteLights;
};

View File

@ -26,12 +26,27 @@
#include "ldebug.h"
#include "scene/3d/immediate_geometry.h"
#include "scene/3d/light.h"
#include "scene/3d/baked_lightmap.h"
#include "lroom.h"
#include "lhelper.h"
#include "lscene_saver.h"
#define LROOMLIST m_pRoomList
#define CHECK_ROOM_LIST if (!CheckRoomList())\
{\
WARN_PRINT_ONCE("rooms is unset");\
return false;\
}
LRoomManager::LRoomManager()
{
m_ID_camera = 0;
m_ID_DebugPlanes = 0;
m_ID_DebugBounds = 0;
m_ID_DebugLights = 0;
m_ID_RoomList = 0;
m_uiFrameCounter = 0;
m_iLoggingLevel = 2;
m_bActive = true;
@ -44,6 +59,13 @@ LRoomManager::LRoomManager()
m_bDebugPlanes = false;
m_bDebugBounds = false;
m_bDebugLights = false;
m_pRoomList = 0;
if (!Engine::get_singleton()->is_editor_hint())
{
CreateDebug();
}
}
int LRoomManager::FindClosestRoom(const Vector3 &pt) const
@ -146,9 +168,17 @@ int LRoomManager::Obj_GetRoomNum(Node * pNode) const
LRoom * LRoomManager::GetRoomFromDOB(Node * pNode)
{
int iRoom = Obj_GetRoomNum(pNode);
if (iRoom < 0)
{
if (iRoom == -1)
{
WARN_PRINT_ONCE("LRoomManager::GetRoomFromDOB : metadata is empty");
}
if (iRoom == -2)
{
WARN_PRINT_ONCE("LRoomManager::GetRoomFromDOB : are you updating an unregistered DOB?");
}
return 0;
}
@ -164,6 +194,8 @@ LRoom * LRoomManager::GetRoomFromDOB(Node * pNode)
// register but let LPortal know which room the dob should start in
bool LRoomManager::dob_register_hint(Node * pDOB, float radius, Node * pRoom)
{
CHECK_ROOM_LIST
if (!pDOB)
{
WARN_PRINT_ONCE("dob_register_hint : pDOB is NULL");
@ -198,6 +230,7 @@ void LRoomManager::CreateDebug()
ImmediateGeometry * p = memnew(ImmediateGeometry);
p->set_name("debug_planes");
add_child(p);
move_child(p, get_child_count()-1);
m_ID_DebugPlanes = p->get_instance_id();
@ -217,6 +250,7 @@ void LRoomManager::CreateDebug()
ImmediateGeometry * b = memnew(ImmediateGeometry);
b->set_name("debug_bounds");
add_child(b);
move_child(b, get_child_count()-1);
m_ID_DebugBounds = b->get_instance_id();
m_mat_Debug_Bounds = Ref<SpatialMaterial>(memnew(SpatialMaterial));
//m_mat_Debug_Bounds->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
@ -230,6 +264,7 @@ void LRoomManager::CreateDebug()
ImmediateGeometry * b = memnew(ImmediateGeometry);
b->set_name("debug_lights");
add_child(b);
move_child(b, get_child_count()-1);
m_ID_DebugLights = b->get_instance_id();
b->set_material_override(m_mat_Debug_Bounds);
//b->hide();
@ -297,6 +332,8 @@ bool LRoomManager::DobRegister(Spatial * pDOB, float radius, int iRoom)
bool LRoomManager::dob_register(Node * pDOB, float radius)
{
CHECK_ROOM_LIST
if (!pDOB)
{
WARN_PRINT_ONCE("dob_register : pDOB is NULL");
@ -341,6 +378,11 @@ int LRoomManager::dob_update(Node * pDOB)
// get dob data to move to new room
unsigned int dob_id = pRoom->DOB_Find(pDOB);
// if (dob_id == -1)
// {
// WARN_PRINT_ONCE("DOB is not found in room");
// return -1;
// }
assert (dob_id != -1);
// copy across data before removing
@ -366,6 +408,8 @@ int LRoomManager::dob_update(Node * pDOB)
bool LRoomManager::dob_teleport_hint(Node * pDOB, Node * pRoom)
{
CHECK_ROOM_LIST
if (!pDOB)
{
WARN_PRINT_ONCE("dob_teleport_hint : pDOB is NULL");
@ -439,6 +483,8 @@ bool LRoomManager::DobTeleport(Spatial * pDOB, int iNewRoomID)
// not tested...
bool LRoomManager::dob_teleport(Node * pDOB)
{
CHECK_ROOM_LIST
Spatial * pSpat = Object::cast_to<Spatial>(pDOB);
if (!pSpat)
return false;
@ -458,8 +504,13 @@ bool LRoomManager::dob_teleport(Node * pDOB)
bool LRoomManager::dob_unregister(Node * pDOB)
{
CHECK_ROOM_LIST
LRoom * pRoom = GetRoomFromDOB(pDOB);
// change the meta data on the DOB .. this will catch trying to update an unregistered DOB
Obj_SetRoomNum(pDOB, -2);
if (pRoom)
{
unsigned int dob_id = pRoom->DOB_Find(pDOB);
@ -549,6 +600,8 @@ bool LRoomManager::LightCreate(Light * pLight, int roomID)
bool LRoomManager::light_register(Node * pLightNode)
{
CHECK_ROOM_LIST
if (!pLightNode)
{
WARN_PRINT_ONCE("light_register : pLightNode is NULL");
@ -565,26 +618,6 @@ bool LRoomManager::light_register(Node * pLightNode)
}
return LightCreate(pLight, -1);
// // set culling flag for light
// // 1 is for lighting objects outside the room system
// pLight->set_cull_mask(1 | LRoom::LAYER_MASK_LIGHT);
// // create new light
// LLight l;
// l.SetDefaults();
// l.m_GodotID = pLight->get_instance_id();
// // direction
// Transform tr = pLight->get_global_transform();
// l.m_ptPos = tr.origin;
// l.m_ptDir = -tr.basis.get_axis(2); // or possibly get_axis .. z is what we want
// l.m_RoomID = -1;
// l.m_eType = LLight::LT_DIRECTIONAL;
// m_Lights.push_back(l);
// return true;
}
@ -616,6 +649,35 @@ int LRoomManager::dob_get_room_id(Node * pDOB)
return Obj_GetRoomNum(pDOB);
}
// helpers to enable the client to manage switching on and off physics and AI
int LRoomManager::rooms_get_num_rooms() const
{
return m_Rooms.size();
}
bool LRoomManager::rooms_is_room_visible(int room_id) const
{
if (room_id >= m_Rooms.size())
{
LWARN(5, "LRoomManager::rooms_is_room_visible : room id higher than number of rooms");
return false;
}
return m_BF_visible_rooms.GetBit(room_id) != 0;
}
Array LRoomManager::rooms_get_visible_rooms() const
{
Array rooms;
for (int n=0; n<m_pCurr_VisibleRoomList->size(); n++)
{
rooms.push_back((*m_pCurr_VisibleRoomList)[n]);
}
return rooms;
}
Node * LRoomManager::rooms_get_room(int room_id)
{
const LRoom * pRoom = GetRoom(room_id);
@ -673,11 +735,36 @@ void LRoomManager::rooms_set_debug_planes(bool bActive)
}
// move the initial hiding to where the camera is set, so we can save the scene etc
void LRoomManager::ShowAll(bool bShow)
{
for (int n=0; n<m_SOBs.size(); n++)
{
LSob &sob = m_SOBs[n];
sob.Show(bShow);
}
// hide all lights that are non global
for (int n=0; n<m_Lights.size(); n++)
{
LLight &light = m_Lights[n];
if (!light.IsGlobal())
light.Show(bShow);
}
}
// turn on and off culling for debugging
void LRoomManager::rooms_set_active(bool bActive)
{
if (bActive == m_bActive)
{
LWARN(2, "LRoomManager::rooms_set_active : noop, no state change");
return;
}
CheckRoomList();
m_bActive = bActive;
@ -699,12 +786,13 @@ void LRoomManager::rooms_set_active(bool bActive)
for (int n=0; n<m_SOBs.size(); n++)
{
LSob &sob = m_SOBs[n];
Spatial * pS = sob.GetSpatial();
if (pS)
if (!bActive)
pS->show();
else
pS->hide();
sob.Show(!bActive);
// Spatial * pS = sob.GetSpatial();
// if (pS)
// if (!bActive)
// pS->show();
// else
// pS->hide();
VisualInstance * pVI = sob.GetVI();
if (pVI)
@ -734,18 +822,24 @@ void LRoomManager::rooms_log_frame()
}
void LRoomManager::rooms_set_camera(Node * pCam)
bool LRoomManager::rooms_set_camera(Node * pCam)
{
CHECK_ROOM_LIST
// is it the first setting of the camera? if so hide all
if (m_ID_camera == 0)
ShowAll(false);
m_ID_camera = 0;
if (!pCam)
return;
return false;
Camera * pCamera = Object::cast_to<Camera>(pCam);
if (!pCamera)
{
WARN_PRINT("Not a camera");
return;
return false;
}
m_ID_camera = pCam->get_instance_id();
@ -756,19 +850,137 @@ void LRoomManager::rooms_set_camera(Node * pCam)
// use this temporarily to force debug
// rooms_log_frame();
return true;
}
// convert empties and meshes to rooms and portals
void LRoomManager::rooms_convert()
bool LRoomManager::rooms_convert(bool bVerbose, bool bDeleteLights)
{
ResolveRoomListPath();
CHECK_ROOM_LIST
LRoomConverter conv;
conv.Convert(*this);
conv.Convert(*this, bVerbose, false, bDeleteLights);
return true;
}
bool LRoomManager::rooms_transfer_uv2s(Node * pMeshInstance_From, Node * pMeshInstance_To)
{
CheckRoomList();
MeshInstance * pMI_from = Object::cast_to<MeshInstance>(pMeshInstance_From);
MeshInstance * pMI_to = Object::cast_to<MeshInstance>(pMeshInstance_To);
if (!pMI_from)
return false;
if (!pMI_to)
return false;
LHelper helper;
Lawn::LDebug::m_bRunning = false;
bool res = helper.TransferUV2(*pMI_from, *pMI_to);
Lawn::LDebug::m_bRunning = true;
return res;
}
bool LRoomManager::rooms_unmerge_sobs(Node * pMergeMeshInstance)
{
MeshInstance * pMI = Object::cast_to<MeshInstance>(pMergeMeshInstance);
LHelper helper;
Lawn::LDebug::m_bRunning = false;
bool res = helper.UnMergeSOBs(*this, pMI);
Lawn::LDebug::m_bRunning = true;
return res;
}
bool LRoomManager::rooms_save_scene(Node * pNode, String szFilename)
{
LSceneSaver saver;
return saver.SaveScene(pNode, szFilename);
}
MeshInstance * LRoomManager::rooms_convert_lightmap_internal(String szProxyFilename, String szLevelFilename)
{
ResolveRoomListPath();
if (!CheckRoomList())
{
WARN_PRINT_ONCE("rooms_convert_lightmap_internal : rooms not set");
return 0;
}
LRoomConverter conv;
conv.Convert(*this, true, true, false);
LHelper helper;
Lawn::LDebug::m_bRunning = false;
MeshInstance * pMI = helper.CreateLightmapProxy(*this);
// if there is a save filename for the proxy, save
if ((szProxyFilename != "") && pMI)
{
LSceneSaver saver;
saver.SaveScene(pMI, szProxyFilename);
}
Lawn::LDebug::m_bRunning = true;
// save the UV2 mapped level
if (szLevelFilename != "")
rooms_save_scene(LROOMLIST, szLevelFilename);
return pMI;
}
bool LRoomManager::rooms_merge_sobs(Node * pMergeMeshInstance)
{
MeshInstance * pMI = Object::cast_to<MeshInstance>(pMergeMeshInstance);
LHelper helper;
Lawn::LDebug::m_bRunning = false;
bool res = helper.MergeSOBs(*this, pMI);
Lawn::LDebug::m_bRunning = true;
return res;
}
// free memory for current set of rooms, prepare for converting a new game level
void LRoomManager::rooms_release()
{
CheckRoomList();
// unhide all the objects and reattach to scene graph
rooms_set_active(false);
// unregister all the dobs
for (int n=0; n<m_Rooms.size(); n++)
{
LRoom &lroom = m_Rooms[n];
lroom.Release(*this);
}
ReleaseResources(false);
m_ID_camera = 0;
m_ID_RoomList = 0;
m_uiFrameCounter = 0;
// m_iLoggingLevel = 2;
m_bActive = true;
m_bFrustumOnly = false;
// m_bDebugPlanes = false;
// m_bDebugBounds = false;
// m_bDebugLights = false;
m_pRoomList = 0;
}
void LRoomManager::ReleaseResources(bool bPrepareConvert)
@ -837,23 +1049,25 @@ void LRoomManager::FrameUpdate_Prepare()
m_Pool.Reset();
}
void LRoomManager::FrameUpdate()
bool LRoomManager::FrameUpdate()
{
if (Engine::get_singleton()->is_editor_hint())
{
WARN_PRINT_ONCE("LRoomManager::FrameUpdate should not be called in editor");
return;
return false;
}
// could turn off internal processing? not that important
if (!m_bActive)
return;
return false;
CHECK_ROOM_LIST
if (m_bFrustumOnly)
{
// debugging emulate view frustum
FrameUpdate_FrustumOnly();
return;
return true;
}
// we keep a frame counter to prevent visiting things multiple times on the same frame in recursive functions
@ -872,11 +1086,11 @@ void LRoomManager::FrameUpdate()
}
else
// camera not set .. do nothing
return;
return false;
// camera not a camera?? shouldn't happen but we'll check
if (!pCamera)
return;
return false;
// Which room is the camera currently in?
LRoom * pRoom = GetRoomFromDOB(pCamera);
@ -884,7 +1098,7 @@ void LRoomManager::FrameUpdate()
if (!pRoom)
{
WARN_PRINT_ONCE("LRoomManager::FrameUpdate : Camera is not in an LRoom");
return;
return false;
}
// lcamera contains the info needed for culling
@ -938,6 +1152,8 @@ void LRoomManager::FrameUpdate()
// when running, emit less debugging output so as not to choke the IDE
Lawn::LDebug::m_bRunning = true;
return true;
}
void LRoomManager::FrameUpdate_FinalizeRooms()
@ -1112,13 +1328,6 @@ void LRoomManager::FrameUpdate_FinalizeVisibility_WithinRooms()
// show / hide is relatively expensive because of propagating messages between nodes ...
// should be minimized
sob.Show(true);
// see how expensive show is
// for (int t=0; t<10000; t++)
// {
// sob.Show(false);
// sob.Show(true);
// }
}
}
@ -1214,7 +1423,7 @@ void LRoomManager::_notification(int p_what) {
if (!Engine::get_singleton()->is_editor_hint())
{
set_process_internal(true);
CreateDebug();
//CreateDebug();
}
else
set_process_internal(false);
@ -1232,34 +1441,176 @@ void LRoomManager::_notification(int p_what) {
void LRoomManager::_bind_methods()
{
// main functions
ClassDB::bind_method(D_METHOD("rooms_convert"), &LRoomManager::rooms_convert);
ClassDB::bind_method(D_METHOD("rooms_convert", "verbose", "delete lights"), &LRoomManager::rooms_convert);
ClassDB::bind_method(D_METHOD("rooms_release"), &LRoomManager::rooms_release);
ClassDB::bind_method(D_METHOD("rooms_set_camera"), &LRoomManager::rooms_set_camera);
ClassDB::bind_method(D_METHOD("rooms_get_room"), &LRoomManager::rooms_get_room);
ClassDB::bind_method(D_METHOD("rooms_set_camera", "camera"), &LRoomManager::rooms_set_camera);
ClassDB::bind_method(D_METHOD("rooms_save_scene", "node", "filename"), &LRoomManager::rooms_save_scene);
// stuff for external workflow .. works but don't expose yet
// ClassDB::bind_method(D_METHOD("rooms_merge_sobs"), &LRoomManager::rooms_merge_sobs);
// ClassDB::bind_method(D_METHOD("rooms_unmerge_sobs"), &LRoomManager::rooms_unmerge_sobs);
// ClassDB::bind_method(D_METHOD("rooms_transfer_uv2s"), &LRoomManager::rooms_transfer_uv2s);
// debugging
ClassDB::bind_method(D_METHOD("rooms_set_logging"), &LRoomManager::rooms_set_logging);
ClassDB::bind_method(D_METHOD("rooms_set_logging", "level 0 to 6"), &LRoomManager::rooms_set_logging);
ClassDB::bind_method(D_METHOD("rooms_log_frame"), &LRoomManager::rooms_log_frame);
ClassDB::bind_method(D_METHOD("rooms_set_active"), &LRoomManager::rooms_set_active);
ClassDB::bind_method(D_METHOD("rooms_set_debug_planes"), &LRoomManager::rooms_set_debug_planes);
ClassDB::bind_method(D_METHOD("rooms_set_debug_bounds"), &LRoomManager::rooms_set_debug_bounds);
ClassDB::bind_method(D_METHOD("rooms_set_debug_lights"), &LRoomManager::rooms_set_debug_lights);
ClassDB::bind_method(D_METHOD("rooms_set_active", "active"), &LRoomManager::rooms_set_active);
ClassDB::bind_method(D_METHOD("rooms_set_debug_planes", "active"), &LRoomManager::rooms_set_debug_planes);
ClassDB::bind_method(D_METHOD("rooms_set_debug_bounds", "active"), &LRoomManager::rooms_set_debug_bounds);
ClassDB::bind_method(D_METHOD("rooms_set_debug_lights", "active"), &LRoomManager::rooms_set_debug_lights);
// lightmapping
ClassDB::bind_method(D_METHOD("rooms_convert_lightmap_internal", "proxy filename", "level filename"), &LRoomManager::rooms_convert_lightmap_internal);
// functions to add dynamic objects to the culling system
// Note that these should not be placed directly in rooms, the system will 'soft link' to them
// so they can be held, e.g. in pools elsewhere in the scene graph
ClassDB::bind_method(D_METHOD("dob_register"), &LRoomManager::dob_register);
ClassDB::bind_method(D_METHOD("dob_unregister"), &LRoomManager::dob_unregister);
ClassDB::bind_method(D_METHOD("dob_update"), &LRoomManager::dob_update);
ClassDB::bind_method(D_METHOD("dob_teleport"), &LRoomManager::dob_teleport);
ClassDB::bind_method(D_METHOD("dob_register", "dob", "radius"), &LRoomManager::dob_register);
ClassDB::bind_method(D_METHOD("dob_unregister", "dob"), &LRoomManager::dob_unregister);
ClassDB::bind_method(D_METHOD("dob_update", "dob"), &LRoomManager::dob_update);
ClassDB::bind_method(D_METHOD("dob_teleport", "dob"), &LRoomManager::dob_teleport);
ClassDB::bind_method(D_METHOD("dob_register_hint"), &LRoomManager::dob_register_hint);
ClassDB::bind_method(D_METHOD("dob_teleport_hint"), &LRoomManager::dob_teleport_hint);
ClassDB::bind_method(D_METHOD("dob_register_hint", "dob", "radius", "room"), &LRoomManager::dob_register_hint);
ClassDB::bind_method(D_METHOD("dob_teleport_hint", "dob", "room"), &LRoomManager::dob_teleport_hint);
ClassDB::bind_method(D_METHOD("dob_get_room_id"), &LRoomManager::dob_get_room_id);
ClassDB::bind_method(D_METHOD("dob_get_room_id", "dob"), &LRoomManager::dob_get_room_id);
ClassDB::bind_method(D_METHOD("light_register"), &LRoomManager::light_register);
ClassDB::bind_method(D_METHOD("light_register", "light"), &LRoomManager::light_register);
// helper
ClassDB::bind_method(D_METHOD("rooms_get_room", "room id"), &LRoomManager::rooms_get_room);
ClassDB::bind_method(D_METHOD("rooms_get_num_rooms"), &LRoomManager::rooms_get_num_rooms);
ClassDB::bind_method(D_METHOD("rooms_is_room_visible", "room id"), &LRoomManager::rooms_is_room_visible);
ClassDB::bind_method(D_METHOD("rooms_get_visible_rooms"), &LRoomManager::rooms_get_visible_rooms);
ClassDB::bind_method(D_METHOD("set_rooms", "rooms"), &LRoomManager::set_rooms);
ClassDB::bind_method(D_METHOD("set_rooms_path", "rooms"), &LRoomManager::set_rooms_path);
ClassDB::bind_method(D_METHOD("get_rooms_path"), &LRoomManager::get_rooms_path);
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "rooms"), "set_rooms_path", "get_rooms_path");
}
//////////////////////////////
void LRoomManager::_set_rooms(Object *p_rooms)
{
if (p_rooms)
{
LPRINT(2, "\t_set_rooms");
}
else
{
LPRINT(2, "\t_set_rooms NULL");
}
m_ID_RoomList = 0;
m_pRoomList = 0;
if (p_rooms)
{
// check is spatial
Spatial *pSpatial = Object::cast_to<Spatial>(p_rooms);
if (pSpatial)
{
m_ID_RoomList = pSpatial->get_instance_id();
m_pRoomList = pSpatial;
LPRINT(2, "\t\tRoomlist was Godot ID " + itos(m_ID_RoomList));
}
else
{
// should never get here?
WARN_PRINT("_set_rooms must be a Spatial");
}
}
}
void LRoomManager::set_rooms(const Object *p_rooms)
{
// handle null
if (p_rooms == NULL)
{
WARN_PRINT("SCRIPT set_rooms NULL");
remove_rooms_path();
return;
}
LPRINT(2, "SCRIPT set_rooms");
// is it a spatial?
const Spatial * pSpatial = Object::cast_to<Spatial>(p_rooms);
if (!pSpatial)
{
WARN_PRINT("set_rooms must be a spatial.");
remove_rooms_path();
return;
}
NodePath path = get_path_to(pSpatial);
set_rooms_path(path);
}
void LRoomManager::set_rooms_path(const NodePath &p_path)
{
LPRINT(2, "set_rooms_path " + p_path);
m_path_RoomList = p_path;
ResolveRoomListPath();
}
NodePath LRoomManager::get_rooms_path() const
{
return m_path_RoomList;
}
void LRoomManager::remove_rooms_path()
{
NodePath pathnull;
set_rooms_path(pathnull);
}
Spatial * LRoomManager::GetRoomList_Checked()
{
if (m_ID_RoomList == 0)
return 0;
m_pRoomList = (Spatial *) ObjectDB::get_instance(m_ID_RoomList);
if (!m_pRoomList)
m_ID_RoomList = 0; // the node is no longer valid
return m_pRoomList;
}
void LRoomManager::ResolveRoomListPath()
{
LPRINT(2, "ResolveRoomListPath " + m_path_RoomList);
if (has_node(m_path_RoomList))
{
LPRINT(2, "has_node");
Spatial * pNode = Object::cast_to<Spatial>(get_node(m_path_RoomList));
if (pNode)
{
_set_rooms(pNode);
return;
}
else
{
WARN_PRINT("RoomList must be a Spatial");
}
}
// default to setting to null
_set_rooms(NULL);
}

View File

@ -41,6 +41,7 @@ class LRoomManager : public Spatial {
friend class LRoom;
friend class LRoomConverter;
friend class LHelper;
// godot ID of the camera (which should be registered as a DOB to allow moving between rooms)
@ -117,7 +118,7 @@ protected:
int m_iLoggingLevel;
private:
// this is where we do all the culling
void FrameUpdate();
bool FrameUpdate();
void FrameUpdate_Prepare();
void FrameUpdate_FinalizeRooms();
void FrameUpdate_AddShadowCasters();
@ -141,6 +142,7 @@ private:
void CreateDebug();
void DobChangeVisibility(Spatial * pDOB, const LRoom * pOld, const LRoom * pNew);
void ReleaseResources(bool bPrepareConvert);
void ShowAll(bool bShow);
// helper funcs
@ -165,6 +167,11 @@ public:
LVector<Vector3> m_DebugPlanes;
LVector<Vector3> m_DebugPortalLightPlanes;
// we are now referencing the rooms indirectly via a nodepath rather than directly being children
// of the LRoomManager node
NodePath m_path_RoomList;
ObjectID m_ID_RoomList;
private:
ObjectID m_ID_DebugPlanes;
ObjectID m_ID_DebugBounds;
@ -172,25 +179,61 @@ private:
Ref<SpatialMaterial> m_mat_Debug_Planes;
Ref<SpatialMaterial> m_mat_Debug_Bounds;
// unchecked
Spatial * m_pRoomList;
void ResolveRoomListPath();
public:
// makes sure m_pRoomList is up to date and valid
bool CheckRoomList() {return GetRoomList_Checked() != 0;}
Spatial * GetRoomList_Checked();
// unchecked, be sure to call checked version first which will set m_pRoomList
Spatial * GetRoomList() const {return m_pRoomList;}
public:
LRoomManager();
// PUBLIC INTERFACE TO GDSCRIPT
void set_rooms(const Object *p_rooms);
void _set_rooms(Object *p_rooms);
void set_rooms_path(const NodePath &p_path);
NodePath get_rooms_path() const;
void remove_rooms_path();
// convert empties and meshes to rooms and portals
void rooms_convert();
bool rooms_convert(bool bVerbose, bool bDeleteLights);
// free memory for current set of rooms, prepare for converting a new game level
void rooms_release();
// choose which camera you want to use to determine visibility.
// normally this will be your main camera, but you can choose another for debugging
void rooms_set_camera(Node * pCam);
bool rooms_set_camera(Node * pCam);
// get the Godot room that is associated with an LPortal room
// (can be used to find the name etc of a room ID returned by dob_update)
Node * rooms_get_room(int room_id);
// helpers to enable the client to manage switching on and off physics and AI
int rooms_get_num_rooms() const;
bool rooms_is_room_visible(int room_id) const;
Array rooms_get_visible_rooms() const;
// helper function to merge SOB meshes for producing lightmaps VIA external blender workflow
bool rooms_merge_sobs(Node * pMergeMeshInstance);
bool rooms_unmerge_sobs(Node * pMergeMeshInstance);
bool rooms_transfer_uv2s(Node * pMeshInstance_From, Node * pMeshInstance_To);
// one function to do all the uv mapping and lightmap creation in one
// (for godot lightmap workflow)
MeshInstance * rooms_convert_lightmap_internal(String szProxyFilename, String szLevelFilename);
// helper function for general use .. LPortal has the functionality, why not...
bool rooms_save_scene(Node * pNode, String szFilename);
// turn on and off culling for debugging
void rooms_set_active(bool bActive);
void rooms_set_debug_planes(bool bActive);

33
lscene_saver.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "lscene_saver.h"
#include "scene/main/node.h"
#include "scene/resources/packed_scene.h"
#include "core/io/resource_saver.h"
void LSceneSaver::SetOwnerRecursive(Node * pNode, Node * pOwner)
{
pNode->set_owner(pOwner);
for (int n=0; n<pNode->get_child_count(); n++)
{
SetOwnerRecursive(pNode->get_child(n), pOwner);
}
}
bool LSceneSaver::SaveScene(Node * pNode, String szFilename)
{
// godot needs owner to be set on nodes that are to be saved as part of a packed scene
SetOwnerRecursive(pNode, pNode);
//PackedScene ps;
// reference should self delete on exiting func .. check!
Ref<PackedScene> ps = memnew(PackedScene);
//Ref<PackedScene> ref_ps = &ps;
ps->pack(pNode);
ResourceSaver rs;
rs.save(szFilename, ps);
return true;
}

10
lscene_saver.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
class LSceneSaver
{
public:
bool SaveScene(Node * pNode, String szFilename);
private:
void SetOwnerRecursive(Node * pNode, Node * pOwner);
};