Skip to content

Commit

Permalink
feat(samples): add shadows sample
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas7770 committed Jul 27, 2024
1 parent 10c74c0 commit 9cb63e7
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 1 deletion.
3 changes: 2 additions & 1 deletion engine/samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ make_sample(DIR "input" ASSETS)
make_sample(DIR "assets/bridge" ASSETS)
make_sample(DIR "assets/json" ASSETS)
make_sample(DIR "assets/saving" ASSETS)
make_sample(DIR "render" ASSETS)
make_sample(DIR "render/main" ASSETS)
make_sample(DIR "render/shadows" ASSETS)
make_sample(DIR "imgui")
make_sample(DIR "collisions")
make_sample(DIR "scene" ASSETS)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
108 changes: 108 additions & 0 deletions engine/samples/render/shadows/assets/main.cubos
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"entities": {
"render-target": {
"cubos::engine::RenderTargetDefaults": {}
},
"camera": {
"cubos::engine::PerspectiveCamera": {},
"cubos::engine::DrawsTo@render-target": {},
"cubos::engine::Position": {
"x": 0,
"y": 0,
"z": 0
}
},
"car": {
"cubos::engine::RenderVoxelGrid": {
"asset": "059c16e7-a439-44c7-9bdc-6e069dba0c75",
"offset": {
"x": -7.5,
"y": 0.0,
"z": -16.0
}
},
"cubos::engine::Position": {
"x": -3,
"y": -2.5,
"z": -14
},
"cubos::engine::Scale": 0.2,
"Spin": {}
},
"spot1": {
"cubos::engine::SpotLight": {
"color": {
"x": 1.0,
"y": 1.0,
"z": 1.0
},
"intensity": 10.0,
"range": 20.0,
"spotAngle": 90.0,
"innerSpotAngle": 45.0
},
"cubos::engine::Position": {
"x": -13.0,
"y": -2.0,
"z": -18.0
},
"cubos::engine::Rotation": {
"w": 0.7071068,
"x": 0.0,
"y": 0.7071068,
"z": 0.0
},
"cubos::engine::SpotShadowCaster": {}
},
"spot2": {
"cubos::engine::SpotLight": {
"color": {
"x": 1.0,
"y": 1.0,
"z": 1.0
},
"intensity": 20.0,
"range": 20.0,
"spotAngle": 90.0,
"innerSpotAngle": 45.0
},
"cubos::engine::Position": {
"x": 10.0,
"y": -2.0,
"z": -20.0
},
"cubos::engine::Rotation": {
"w": 0.8660254,
"x": 0.0,
"y": -0.5,
"z": 0.0
},
"cubos::engine::SpotShadowCaster": {}
},
"spot3": {
"cubos::engine::SpotLight": {
"color": {
"x": 1.0,
"y": 1.0,
"z": 1.0
},
"intensity": 10.0,
"range": 20.0,
"spotAngle": 90.0,
"innerSpotAngle": 45.0
},
"cubos::engine::Position": {
"x": 6.0,
"y": -2.0,
"z": -10.0
},
"cubos::engine::Rotation": {
"w": 0.258819,
"x": 0.0,
"y": -0.9659258,
"z": 0.0
},
"cubos::engine::SpotShadowCaster": {}
}
}
}
3 changes: 3 additions & 0 deletions engine/samples/render/shadows/assets/main.cubos.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "950f0d55-2fdf-4e70-a283-79a2bda10f99"
}
Binary file not shown.
3 changes: 3 additions & 0 deletions engine/samples/render/shadows/assets/voxels/car.grd.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "059c16e7-a439-44c7-9bdc-6e069dba0c75"
}
Binary file not shown.
Binary file added engine/samples/render/shadows/assets/voxels/main.pal
Binary file not shown.
3 changes: 3 additions & 0 deletions engine/samples/render/shadows/assets/voxels/main.pal.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "1aa5e234-28cb-4386-99b4-39386b0fc215"
}
90 changes: 90 additions & 0 deletions engine/samples/render/shadows/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <cubos/core/ecs/reflection.hpp>
#include <cubos/core/reflection/external/primitives.hpp>

#include <cubos/engine/assets/plugin.hpp>
#include <cubos/engine/render/defaults/plugin.hpp>
#include <cubos/engine/render/lights/environment.hpp>
#include <cubos/engine/render/voxels/grid.hpp>
#include <cubos/engine/render/voxels/palette.hpp>
#include <cubos/engine/scene/plugin.hpp>
#include <cubos/engine/settings/plugin.hpp>
#include <cubos/engine/transform/plugin.hpp>
#include <cubos/engine/voxels/grid.hpp>
#include <cubos/engine/voxels/plugin.hpp>
#include <cubos/engine/window/plugin.hpp>

using namespace cubos::engine;

static const Asset<Scene> SceneAsset = AnyAsset("950f0d55-2fdf-4e70-a283-79a2bda10f99");
static const Asset<VoxelPalette> PaletteAsset = AnyAsset("1aa5e234-28cb-4386-99b4-39386b0fc215");

struct Spin
{
CUBOS_REFLECT;
};

CUBOS_REFLECT_IMPL(Spin)
{
return cubos::core::ecs::TypeBuilder<Spin>("Spin").build();
}

int main()
{
Cubos cubos{};

cubos.component<Spin>();

/// [Adding the plugins]
cubos.plugin(settingsPlugin);
cubos.plugin(windowPlugin);
cubos.plugin(transformPlugin);
cubos.plugin(assetsPlugin);
cubos.plugin(voxelsPlugin);
cubos.plugin(renderDefaultsPlugin);
/// [Adding the plugins]

cubos.plugin(scenePlugin);

cubos.startupSystem("configure settings").tagged(settingsTag).call([](Settings& settings) {
settings.setString("assets.io.path", SAMPLE_ASSETS_FOLDER);
settings.setBool("window.vSync", false);
});

cubos.startupSystem("set the palette and environment and spawn the scene")
.tagged(assetsTag)
.call([](Commands commands, const Assets& assets, RenderPalette& palette, RenderEnvironment& environment) {
palette.asset = PaletteAsset;
environment.ambient = {0.01F, 0.01F, 0.01F};
environment.skyGradient[0] = {0.1F, 0.2F, 0.4F};
environment.skyGradient[1] = {0.6F, 0.6F, 0.8F};
commands.spawn(assets.read(SceneAsset)->blueprint);
});

cubos.startupSystem("create a voxel grid").tagged(assetsTag).call([](Commands commands, Assets& assets) {
// Create a 2x2x2 grid whose voxels alternate between the materials defined in the palette.
auto gridAsset = assets.create(VoxelGrid{{2, 2, 2}, {1, 2, 3, 1, 2, 3, 1, 2}});

// Spawn entities with the grid.
// Cube
commands.create()
.add(RenderVoxelGrid{gridAsset, {0.0F, 0.0F, 0.0F}})
.add(LocalToWorld{})
.add(Position{{3.0F, -3.0F, -17.0F}})
.add(Scale{1.5F});
// Floor
commands.create()
.add(RenderVoxelGrid{gridAsset, {0.0F, 0.0F, 0.0F}})
.add(LocalToWorld{})
.add(Position{{-20.0F, -43.0F, -50.0F}})
.add(Scale{20.0F});
});

cubos.system("spin car").call([](Query<const Spin&, Rotation&> query, DeltaTime& dt) {
for (auto [spin, rotation] : query)
{
rotation.quat = glm::cross(rotation.quat, glm::quat(glm::vec3{0.0F, dt.value(), 0.0F}));
}
});

cubos.run();
}

0 comments on commit 9cb63e7

Please sign in to comment.