|
| 1 | +#include <algorithm> |
| 2 | + |
| 3 | +#include <cage-core/assetsManager.h> |
| 4 | +#include <cage-core/entitiesVisitor.h> |
| 5 | +#include <cage-core/pointerRangeHolder.h> |
| 6 | +#include <cage-engine/model.h> |
| 7 | +#include <cage-engine/renderObject.h> |
| 8 | +#include <cage-engine/scenePicking.h> |
| 9 | + |
| 10 | +namespace cage |
| 11 | +{ |
| 12 | + namespace |
| 13 | + { |
| 14 | + struct Boxes |
| 15 | + { |
| 16 | + const AssetsManager *assets = nullptr; |
| 17 | + |
| 18 | + Aabb model(uint32 name) const |
| 19 | + { |
| 20 | + Holder<Model> m = assets->get<AssetSchemeIndexModel, Model>(name); |
| 21 | + if (!m) |
| 22 | + return Aabb(); |
| 23 | + return m->boundingBox; |
| 24 | + } |
| 25 | + |
| 26 | + Aabb object(const RenderObject *o) const |
| 27 | + { |
| 28 | + if (!o) |
| 29 | + return Aabb(); |
| 30 | + Aabb res; |
| 31 | + for (uint32 i = 0; i < o->lodsCount(); i++) // not sure which LOD is loaded, so we need to check all |
| 32 | + for (uint32 it : o->models(i)) |
| 33 | + res += model(it); |
| 34 | + return res; |
| 35 | + } |
| 36 | + |
| 37 | + Aabb asset(uint32 name) const |
| 38 | + { |
| 39 | + if (name == 0) |
| 40 | + return Aabb(); |
| 41 | + { |
| 42 | + Holder<Model> m = assets->get<AssetSchemeIndexModel, Model>(name); |
| 43 | + if (m) |
| 44 | + return m->boundingBox; |
| 45 | + } |
| 46 | + { |
| 47 | + Holder<RenderObject> o = assets->get<AssetSchemeIndexRenderObject, RenderObject>(name); |
| 48 | + if (o) |
| 49 | + return object(+o); |
| 50 | + } |
| 51 | + return Aabb(); |
| 52 | + } |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + Holder<PointerRange<ScenePickingResult>> scenePicking(const ScenePickingConfig &config) |
| 57 | + { |
| 58 | + CAGE_ASSERT(config.picker.valid()); |
| 59 | + CAGE_ASSERT(config.assets); |
| 60 | + CAGE_ASSERT(config.entities); |
| 61 | + CAGE_ASSERT(!config.camera || config.camera->manager() == config.entities); |
| 62 | + CAGE_ASSERT(!config.camera || config.screenHeight > 0); |
| 63 | + |
| 64 | + PointerRangeHolder<ScenePickingResult> results; |
| 65 | + |
| 66 | + entitiesVisitor( |
| 67 | + [&](Entity *e, const TransformComponent &tr, const ModelComponent &md, const PickableComponent &) |
| 68 | + { |
| 69 | + // todo respect different precisions |
| 70 | + // todo animations and alpha cut |
| 71 | + const Aabb box = Boxes{ config.assets }.asset(md.model) * tr; |
| 72 | + const Line ln = intersection(box, config.picker); |
| 73 | + if (!ln.valid()) |
| 74 | + return; |
| 75 | + ScenePickingResult r; |
| 76 | + r.entity = e; |
| 77 | + r.point = ln.a(); |
| 78 | + results.push_back(r); |
| 79 | + }, |
| 80 | + config.entities, false); |
| 81 | + |
| 82 | + // todo entities with icons instead of models |
| 83 | + |
| 84 | + if (!results.empty()) |
| 85 | + std::sort(results.begin(), results.end(), [&](const ScenePickingResult &a, const ScenePickingResult &b) -> bool { return distanceSquared(a.point, config.picker.a()) < distanceSquared(b.point, config.picker.b()); }); |
| 86 | + return results; |
| 87 | + } |
| 88 | +} |
0 commit comments