2020-05-05 01:51:13 +02:00
|
|
|
#include "Scene.h"
|
2020-09-19 14:36:50 +02:00
|
|
|
#include "Graphics/Graphics.h"
|
2022-11-30 10:19:45 +01:00
|
|
|
#include "Graphics/Mesh.h"
|
2022-11-15 12:19:11 +01:00
|
|
|
#include "Component/StaticMesh.h"
|
|
|
|
|
#include "Component/Transform.h"
|
2023-02-01 22:13:04 +01:00
|
|
|
#include "Asset/AssetRegistry.h"
|
|
|
|
|
#include "Asset/TextureAsset.h"
|
2023-02-13 14:56:13 +01:00
|
|
|
#include "Asset/MaterialAsset.h"
|
2020-05-05 01:51:13 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
2021-05-06 17:02:10 +02:00
|
|
|
inline float frand()
|
|
|
|
|
{
|
|
|
|
|
return (float)rand()/RAND_MAX;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-19 14:36:50 +02:00
|
|
|
Scene::Scene(Gfx::PGraphics graphics)
|
|
|
|
|
: graphics(graphics)
|
2023-01-21 18:43:21 +01:00
|
|
|
, physics(registry)
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Scene::~Scene()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 18:43:21 +01:00
|
|
|
void Scene::update(float deltaTime)
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
2023-01-21 18:43:21 +01:00
|
|
|
physics.update(deltaTime);
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-13 14:56:13 +01:00
|
|
|
Array<MeshBatch> Scene::getStaticMeshes()
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
2023-02-13 14:56:13 +01:00
|
|
|
Array<MeshBatch> result;
|
2022-11-15 12:19:11 +01:00
|
|
|
auto view = registry.view<Component::StaticMesh, Component::Transform>();
|
2023-01-21 18:43:21 +01:00
|
|
|
uint32 sceneDataIndex = 0;
|
|
|
|
|
sceneData.clear();
|
2022-11-15 12:19:11 +01:00
|
|
|
for(auto&& [entity, mesh, transform] : view.each())
|
|
|
|
|
{
|
2023-01-21 18:43:21 +01:00
|
|
|
sceneData.add(PrimitiveSceneData {
|
2022-11-15 12:19:11 +01:00
|
|
|
.localToWorld = transform.toMatrix(),
|
|
|
|
|
.worldToLocal = glm::inverse(transform.toMatrix()),
|
2023-01-21 18:43:21 +01:00
|
|
|
.actorLocation = Vector4(transform.getPosition(), 1.0f)
|
|
|
|
|
});
|
2022-11-30 10:19:45 +01:00
|
|
|
for(auto& m : mesh.mesh->meshes)
|
|
|
|
|
{
|
|
|
|
|
auto& batch = result.add();
|
2023-02-13 14:56:13 +01:00
|
|
|
batch.material = m->referencedMaterial->getMaterial();
|
2022-11-30 10:19:45 +01:00
|
|
|
batch.isBackfaceCullingDisabled = false;
|
|
|
|
|
batch.isCastingShadow = true;
|
|
|
|
|
batch.topology = Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
|
|
|
|
batch.useReverseCulling = false;
|
|
|
|
|
batch.useWireframe = false;
|
|
|
|
|
batch.vertexInput = m->vertexInput;
|
|
|
|
|
MeshBatchElement batchElement;
|
|
|
|
|
batchElement.baseVertexIndex = 0;
|
|
|
|
|
batchElement.firstIndex = 0;
|
|
|
|
|
batchElement.indexBuffer = m->indexBuffer;
|
|
|
|
|
batchElement.indirectArgsBuffer = nullptr;
|
|
|
|
|
batchElement.isInstanced = false;
|
|
|
|
|
batchElement.numInstances = 1;
|
2023-01-21 18:43:21 +01:00
|
|
|
batchElement.sceneDataIndex = sceneDataIndex;
|
2022-11-30 10:19:45 +01:00
|
|
|
batch.elements.add(batchElement);
|
|
|
|
|
}
|
2023-01-21 18:43:21 +01:00
|
|
|
sceneDataIndex++;
|
2022-11-15 12:19:11 +01:00
|
|
|
}
|
2023-01-21 18:43:21 +01:00
|
|
|
if(sceneDataBuffer == nullptr || sceneDataBuffer->getNumElements() != sceneData.size())
|
|
|
|
|
{
|
|
|
|
|
StructuredBufferCreateInfo createInfo = StructuredBufferCreateInfo {
|
|
|
|
|
.resourceData = {
|
|
|
|
|
.size = sceneData.size() * sizeof(PrimitiveSceneData),
|
|
|
|
|
.data = nullptr,
|
|
|
|
|
},
|
|
|
|
|
.stride = (uint32)sceneData.size(),
|
|
|
|
|
};
|
|
|
|
|
sceneDataBuffer = graphics->createStructuredBuffer(createInfo);
|
|
|
|
|
}
|
|
|
|
|
sceneDataBuffer->updateContents(BulkResourceData {
|
|
|
|
|
.size = sceneData.size() * sizeof(PrimitiveSceneData),
|
|
|
|
|
.data = (uint8*)sceneData.data(),
|
|
|
|
|
});
|
2022-11-15 12:19:11 +01:00
|
|
|
return result;
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
LightEnv Scene::getLightBuffer() const
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
2022-11-15 12:19:11 +01:00
|
|
|
LightEnv result;
|
2023-01-21 18:43:21 +01:00
|
|
|
result.directionalLights[0].color = Vector4(0.4, 0.3, 0.5, 1.0);
|
|
|
|
|
result.directionalLights[0].direction = Vector4(0.5, 0.5, 0, 0);
|
|
|
|
|
result.directionalLights[0].intensity = Vector4(1.0, 0.9, 0.7, 0.5);\
|
2022-11-15 12:19:11 +01:00
|
|
|
result.numDirectionalLights = 1;
|
|
|
|
|
result.numPointLights = 0;
|
|
|
|
|
return result;
|
2023-02-01 22:13:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Component::Skybox Scene::getSkybox()
|
|
|
|
|
{
|
|
|
|
|
return Seele::Component::Skybox {
|
|
|
|
|
.day = AssetRegistry::findTexture("FS000_Day_01")->getTexture().cast<Gfx::TextureCube>(),
|
|
|
|
|
.night = AssetRegistry::findTexture("FS000_Night_01")->getTexture().cast<Gfx::TextureCube>(),
|
|
|
|
|
.fogColor = Vector(0.2, 0.1, 0.6),
|
|
|
|
|
.blendFactor = 0,
|
|
|
|
|
};
|
2022-11-15 12:19:11 +01:00
|
|
|
}
|