Skip to content

Commit

Permalink
Added a decal component.
Browse files Browse the repository at this point in the history
Decal component isn't fully implemented. Needs to handle resource loading.
Added checks if context id is valid before handling them in various graphics contexts. This removes lookups, and reduces risk if we don't have to do validity checks first outside of the context.
  • Loading branch information
fLindahl committed Nov 17, 2024
1 parent d75bc81 commit 1183971
Show file tree
Hide file tree
Showing 18 changed files with 273 additions and 104 deletions.
5 changes: 4 additions & 1 deletion code/addons/graphicsfeature/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ fips_dir(managers)
)
fips_dir(components)
nebula_idl_compile(
graphicsfeature.json
camera.json
decal.json
lighting.json
model.json
)
nebula_end_module()

45 changes: 45 additions & 0 deletions code/addons/graphicsfeature/components/camera.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"namespace": "GraphicsFeature",
"enums": {
"ProjectionMode": {
"Perspective": 0,
"Orthographic": 1
}
},
"components": {
"Camera": {
"viewHandle": {
"type": "uint",
"default": -1,
"hideInInspector": true
},
"localTransform": {
"type": "mat4",
"hideInInspector": true
},
"fieldOfView": {
"type": "float",
"default": 70.0
},
"aspectRatio": {
"type": "float"
},
"zNear": {
"type": "float",
"default": 0.01
},
"zFar": {
"type": "float",
"default": 1000.0
},
"orthographicWidth": {
"type": "float",
"default": 1.0
},
"projectionMode": {
"type": "GraphicsFeature::ProjectionMode",
"default": 0
}
}
}
}
16 changes: 16 additions & 0 deletions code/addons/graphicsfeature/components/decal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"namespace": "GraphicsFeature",
"components": {
"Decal": {
"graphicsEntityId": {
"type": "uint",
"default": -1,
"hideInInspector": true
},
"texture": {
"type": "resource",
"description": "The texture that should be projected"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
"namespace": "GraphicsFeature",
"enums": {
"ProjectionMode": {
"Perspective": 0,
"Orthographic": 1
},
"AreaLightShape": {
"Disk": 0,
"Rectangle": 1,
Expand Down Expand Up @@ -85,60 +81,6 @@
"twoSided": "bool",
"castShadows": "bool",
"renderMesh": "bool"
},
"Model": {
"resource": {
"type": "resource",
"default": "mdl:system/placeholder.n3"
},
"graphicsEntityId": {
"type": "uint",
"default": -1,
"hideInInspector": true
},
"raytracing": "bool",
"anim": {
"type": "resource",
"default": ""
},
"skeleton": {
"type": "resource",
"default": ""
}
},
"Camera": {
"viewHandle": {
"type": "uint",
"default": -1,
"hideInInspector": true
},
"localTransform": {
"type": "mat4",
"hideInInspector": true
},
"fieldOfView": {
"type": "float",
"default": 70.0
},
"aspectRatio": {
"type": "float"
},
"zNear": {
"type": "float",
"default": 0.01
},
"zFar": {
"type": "float",
"default": 1000.0
},
"orthographicWidth": {
"type": "float",
"default": 1.0
},
"projectionMode": {
"type": "GraphicsFeature::ProjectionMode",
"default": 0
}
}
}
}
25 changes: 25 additions & 0 deletions code/addons/graphicsfeature/components/model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"namespace": "GraphicsFeature",
"components": {
"Model": {
"resource": {
"type": "resource",
"default": "mdl:system/placeholder.n3"
},
"graphicsEntityId": {
"type": "uint",
"default": -1,
"hideInInspector": true
},
"raytracing": "bool",
"anim": {
"type": "resource",
"default": ""
},
"skeleton": {
"type": "resource",
"default": ""
}
}
}
}
6 changes: 5 additions & 1 deletion code/addons/graphicsfeature/graphicsfeatureunit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
#include "flat/graphicsfeature/terrainschema.h"
#include "flat/graphicsfeature/vegetationschema.h"
#include "nflatbuffer/flatbufferinterface.h"
#include "components/graphicsfeature.h"
#include "components/camera.h"
#include "components/decal.h"
#include "components/lighting.h"
#include "components/model.h"

#include "scripting/deargui.h"

Expand Down Expand Up @@ -87,6 +90,7 @@ GraphicsFeatureUnit::OnAttach()
this->RegisterComponentType<SpotLight>({.decay = true, .OnInit = &GraphicsManager::InitSpotLight });
this->RegisterComponentType<AreaLight>({.decay = true, .OnInit = &GraphicsManager::InitAreaLight });
this->RegisterComponentType<Model>({.decay = true, .OnInit = &GraphicsManager::InitModel });
this->RegisterComponentType<Decal>({.decay = true, .OnInit = &GraphicsManager::InitDecal});
this->RegisterComponentType<Camera>();
Scripting::RegisterDearguiModule();
}
Expand Down
2 changes: 1 addition & 1 deletion code/addons/graphicsfeature/managers/cameramanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "ids/idgenerationpool.h"
#include "graphics/graphicsentity.h"
#include "math/vec3.h"
#include "graphicsfeature/components/graphicsfeature.h"
#include "graphicsfeature/components/camera.h"

namespace Graphics
{
Expand Down
116 changes: 97 additions & 19 deletions code/addons/graphicsfeature/managers/graphicsmanager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "raytracing/raytracingcontext.h"
#include "characters/charactercontext.h"
#include "game/gameserver.h"
#include "graphicsfeature/components/graphicsfeature.h"
#include "basegamefeature/components/basegamefeature.h"
#include "basegamefeature/components/position.h"
#include "basegamefeature/components/orientation.h"
Expand All @@ -20,6 +19,7 @@
#include "io/jsonwriter.h"
#include "lighting/lightcontext.h"
#include "game/componentinspection.h"
#include "decals/decalcontext.h"

namespace GraphicsFeature
{
Expand Down Expand Up @@ -130,6 +130,22 @@ DeregisterLight(Graphics::GraphicsEntityId gfxId)
Graphics::DestroyEntity(gfxId);
}

//------------------------------------------------------------------------------
/**
*/
void
DeregisterDecal(Graphics::GraphicsEntityId gfxId)
{
if (gfxId == Graphics::InvalidGraphicsEntityId)
return;

if (Decals::DecalContext::IsEntityRegistered(gfxId))
{
Decals::DecalContext::DeregisterEntity(gfxId);
}
Graphics::DestroyEntity(gfxId);
}

//------------------------------------------------------------------------------
/**
*/
Expand Down Expand Up @@ -169,6 +185,14 @@ GraphicsManager::OnDecay()
AreaLight* light = areaLightData + i;
DeregisterLight(light->graphicsEntityId);
}

Game::ComponentDecayBuffer const decalDecayBuffer = world->GetDecayBuffer(Game::GetComponentId<Decal>());
Decal* decalData = (Decal*)decalDecayBuffer.buffer;
for (int i = 0; i < decalDecayBuffer.size; i++)
{
Decal* decal = decalData + i;
DeregisterDecal(decal->graphicsEntityId);
}
}

//------------------------------------------------------------------------------
Expand All @@ -188,11 +212,8 @@ GraphicsManager::InitUpdateModelTransformProcessor()
Game::Scale const& scale,
GraphicsFeature::Model const& model)
{
if (model.graphicsEntityId != -1)
{
Math::mat4 worldTransform = Math::trs(pos, orient, scale);
Models::ModelContext::SetTransform(model.graphicsEntityId, worldTransform);
}
Math::mat4 worldTransform = Math::trs(pos, orient, scale);
Models::ModelContext::SetTransform(model.graphicsEntityId, worldTransform);
}
)
.Build();
Expand All @@ -212,8 +233,7 @@ GraphicsManager::InitUpdateLightTransformProcessor()
.Func(
[](Game::World* world, Game::Position const& pos, GraphicsFeature::PointLight const& light)
{
if (Lighting::LightContext::IsEntityRegistered(light.graphicsEntityId))
Lighting::LightContext::SetPosition(light.graphicsEntityId, pos);
Lighting::LightContext::SetPosition(light.graphicsEntityId, pos);
}
)
.Build();
Expand All @@ -227,11 +247,8 @@ GraphicsManager::InitUpdateLightTransformProcessor()
Game::Orientation const& rot,
GraphicsFeature::SpotLight const& light)
{
if (Lighting::LightContext::IsEntityRegistered(light.graphicsEntityId))
{
Lighting::LightContext::SetPosition(light.graphicsEntityId, pos);
Lighting::LightContext::SetRotation(light.graphicsEntityId, rot);
}
Lighting::LightContext::SetPosition(light.graphicsEntityId, pos);
Lighting::LightContext::SetRotation(light.graphicsEntityId, rot);
}
)
.Build();
Expand All @@ -246,12 +263,34 @@ GraphicsManager::InitUpdateLightTransformProcessor()
Game::Scale const& scale,
GraphicsFeature::AreaLight const& light)
{
if (Lighting::LightContext::IsEntityRegistered(light.graphicsEntityId))
{
Lighting::LightContext::SetPosition(light.graphicsEntityId, pos);
Lighting::LightContext::SetRotation(light.graphicsEntityId, rot);
Lighting::LightContext::SetScale(light.graphicsEntityId, scale);
}
Lighting::LightContext::SetPosition(light.graphicsEntityId, pos);
Lighting::LightContext::SetRotation(light.graphicsEntityId, rot);
Lighting::LightContext::SetScale(light.graphicsEntityId, scale);
}
)
.Build();
}

//------------------------------------------------------------------------------
/**
*/
void
GraphicsManager::InitUpdateDecalTransformProcessor()
{
Game::World* world = Game::GetWorld(WORLD_DEFAULT);

Game::ProcessorBuilder(world, "GraphicsManager.UpdateDecalTransform"_atm)
.On("OnEndFrame")
.Excluding<Game::Static>()
.Func(
[](Game::World* world,
Game::Position const& pos,
Game::Orientation const& rot,
Game::Scale const& scale,
GraphicsFeature::Decal const& decal)
{
Math::mat4 transform = Math::trs(pos, rot, scale);
Decals::DecalContext::SetTransform(decal.graphicsEntityId, transform);
}
)
.Build();
Expand All @@ -266,6 +305,7 @@ GraphicsManager::OnActivate()
Manager::OnActivate();
this->InitUpdateModelTransformProcessor();
this->InitUpdateLightTransformProcessor();
this->InitUpdateDecalTransformProcessor();
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -350,6 +390,27 @@ GraphicsManager::InitAreaLight(Game::World* world, Game::Entity entity, AreaLigh
Lighting::LightContext::SetScale(light->graphicsEntityId, scale);
}

//------------------------------------------------------------------------------
/**
*/
void
GraphicsManager::InitDecal(Game::World* world, Game::Entity entity, Decal* decal)
{
//decal->graphicsEntityId = Graphics::CreateEntity().id;
//Game::Position pos = world->GetComponent<Game::Position>(entity);
//Game::Orientation rot = world->GetComponent<Game::Orientation>(entity);
//Game::Scale scale = world->GetComponent<Game::Scale>(entity);
//Decals::DecalContext::RegisterEntity(decal->graphicsEntityId);
//Decals::DecalContext::SetupDecalPBR(
// decal->graphicsEntityId,
// transform,
// decal->resource, // TODO: load resource
//
//);
//Math::mat4 transform = Math::trs(pos, rot, scale);
//Decals::DecalContext::SetTransform(decal->graphicsEntityId, transform);
}

//------------------------------------------------------------------------------
/**
*/
Expand Down Expand Up @@ -440,6 +501,23 @@ GraphicsManager::OnCleanup(Game::World* world)
}
}

Game::DestroyFilter(filter);
}
{ // Decal cleanup
Game::Filter filter = Game::FilterBuilder().Including<Decal>().Build();
Game::Dataset data = world->Query(filter);

for (int v = 0; v < data.numViews; v++)
{
Game::Dataset::View const& view = data.views[v];
Decal const* const componentData = (Decal*)view.buffers[0];

for (IndexT i = 0; i < view.numInstances; ++i)
{
DeregisterDecal((componentData + i)->graphicsEntityId);
}
}

Game::DestroyFilter(filter);
}
}
Expand Down
Loading

0 comments on commit 1183971

Please sign in to comment.