Skip to content

Commit 18c036d

Browse files
committed
separated lod selection; improved scene picking
1 parent 30fed8f commit 18c036d

File tree

11 files changed

+346
-151
lines changed

11 files changed

+346
-151
lines changed

sources/asset-processor/model.cpp

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <cage-core/flatSet.h>
77
#include <cage-core/hashString.h>
88
#include <cage-core/mesh.h>
9+
#include <cage-core/meshAlgorithms.h>
910
#include <cage-core/meshImport.h>
1011
#include <cage-core/skeletalAnimation.h>
1112
#include <cage-engine/shaderConventions.h>
@@ -239,6 +240,47 @@ namespace
239240
if (dsm.textureNames[CAGE_SHADER_TEXTURE_NORMAL] != 0 && none(flags & ModelDataFlags::Normals))
240241
CAGE_THROW_ERROR(Exception, "model uses normal map texture but has no normals");
241242
}
243+
244+
Holder<PointerRange<const char>> generateAndSerializeCollider(const MeshImportPart &part, const MeshImportResult &result)
245+
{
246+
if (part.mesh->type() != MeshTypeEnum::Triangles)
247+
return {};
248+
if (part.mesh->indicesCount() == 0)
249+
return {}; // indexed mesh only
250+
251+
CAGE_LOG(SeverityEnum::Info, "assetProcessor", "building collider");
252+
253+
Holder<Mesh> mesh = newMesh();
254+
const auto &append = [&](const Mesh *tmp)
255+
{
256+
PointerRange<const Vec3> poss = tmp->positions();
257+
PointerRange<const uint32> inds = tmp->indices();
258+
for (uint32 i = 0; i < inds.size(); i += 3)
259+
mesh->addTriangle(Triangle(poss[inds[i]], poss[inds[i + 1]], poss[inds[i + 2]]));
260+
};
261+
append(+part.mesh);
262+
263+
if (!part.mesh->boneIndices().empty())
264+
{
265+
for (const MeshImportAnimation &ani : result.animations)
266+
{
267+
for (Real t = 0; t <= 1; t += 0.05) // sample the animation at 20 positions
268+
{
269+
Holder<Mesh> tmp = part.mesh->copy();
270+
animateMesh(+result.skeleton, +ani.animation, t, +tmp);
271+
append(+tmp);
272+
}
273+
}
274+
}
275+
276+
Holder<Mesh> convex = meshConvexHull(+mesh, {});
277+
278+
Holder<Collider> collider = newCollider();
279+
collider->importMesh(+convex);
280+
collider->optimize();
281+
collider->rebuild();
282+
return collider->exportBuffer();
283+
}
242284
}
243285

244286
void processModel()
@@ -336,17 +378,8 @@ void processModel()
336378
Holder<PointerRange<const char>> serMesh = part.mesh->exportBuffer();
337379
dsm.meshSize = serMesh.size();
338380

339-
Holder<PointerRange<const char>> serCol;
340-
if (part.mesh->type() == MeshTypeEnum::Triangles)
341-
{
342-
CAGE_LOG(SeverityEnum::Info, "assetProcessor", "building collider");
343-
Holder<Collider> collider = newCollider();
344-
collider->importMesh(+part.mesh);
345-
collider->optimize();
346-
collider->rebuild();
347-
serCol = collider->exportBuffer();
348-
dsm.colliderSize = serCol.size();
349-
}
381+
Holder<PointerRange<const char>> serCol = generateAndSerializeCollider(part, result);
382+
dsm.colliderSize = serCol.size();
350383

351384
CAGE_LOG(SeverityEnum::Info, "assetProcessor", "serializing");
352385
AssetHeader h = processor->initializeAssetHeader();
@@ -360,8 +393,7 @@ void processModel()
360393
ser << dsm;
361394
ser << mat;
362395
ser.write(serMesh);
363-
if (serMesh)
364-
ser.write(serCol);
396+
ser.write(serCol);
365397
h.originalSize = buffer.size();
366398
Holder<PointerRange<char>> compressed = memoryCompress(buffer);
367399
h.compressedSize = compressed.size();

sources/include/cage-core/collider.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ namespace cage
7070

7171
CAGE_CORE_API bool collisionDetection(CollisionDetectionConfig &params);
7272

73+
CAGE_CORE_API Real distance(Vec3 shape, const Collider *collider, Transform t);
7374
CAGE_CORE_API Real distance(Line shape, const Collider *collider, Transform t);
7475
CAGE_CORE_API Real distance(Triangle shape, const Collider *collider, Transform t);
7576
CAGE_CORE_API Real distance(Plane shape, const Collider *collider, Transform t);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef guard_lodSelection_h_dsthgiztr8
2+
#define guard_lodSelection_h_dsthgiztr8
3+
4+
#include <vector>
5+
6+
#include <cage-engine/core.h>
7+
8+
namespace cage
9+
{
10+
struct CameraComponent;
11+
class RenderObject;
12+
class Model;
13+
class AssetsOnDemand;
14+
15+
struct CAGE_ENGINE_API LodSelection
16+
{
17+
Vec3 center = Vec3::Nan(); // center of camera
18+
Real screenSize = -1; // vertical size of screen in pixels, one meter in front of the camera
19+
bool orthographic = false;
20+
21+
LodSelection() = default;
22+
LodSelection(Vec3 center, const CameraComponent &cam, sint32 screenHeightPx);
23+
24+
uint32 selectLod(const Vec3 position, const RenderObject *object) const;
25+
void selectModels(std::vector<Holder<Model>> &outModels, const Vec3 position, const RenderObject *object, AssetsOnDemand *assets) const;
26+
void selectModels(std::vector<Holder<Model>> &outModels, const Vec3 position, const RenderObject *object, const AssetsManager *assets) const;
27+
};
28+
}
29+
30+
#endif // guard_lodSelection_h_dsthgiztr8

sources/include/cage-engine/renderPipeline.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
#ifndef guard_renderPipeline_h_4hg1s8596drfh4
22
#define guard_renderPipeline_h_4hg1s8596drfh4
33

4+
#include <cage-engine/lodSelection.h>
45
#include <cage-engine/provisionalHandles.h>
56
#include <cage-engine/scene.h>
67
#include <cage-engine/sceneScreenSpaceEffects.h>
78

89
namespace cage
910
{
1011
class EntityManager;
11-
class ShaderProgram;
1212
class RenderQueue;
1313
class ProvisionalGraphics;
1414
class AssetsOnDemand;
1515

16-
struct CAGE_ENGINE_API LodSelection
17-
{
18-
Vec3 center; // center of camera
19-
Real screenSize = 0; // vertical size of screen in pixels, one meter in front of the camera
20-
bool orthographic = false;
21-
};
22-
2316
struct CAGE_ENGINE_API RenderPipelineConfig
2417
{
2518
String name;

sources/include/cage-engine/scenePicking.h

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,19 @@
33

44
#include <cage-core/entities.h>
55
#include <cage-core/geometry.h>
6-
#include <cage-engine/scene.h>
6+
#include <cage-engine/lodSelection.h>
77

88
namespace cage
99
{
10-
class Mesh;
11-
1210
struct CAGE_ENGINE_API PickableComponent
1311
{};
1412

15-
/*
16-
enum class ScenePickingPrecisionEnum
17-
{
18-
None = 0,
19-
BoundingBoxesOnly,
20-
BoundingMeshesOnly,
21-
Models,
22-
ModelsWithAlphaCut,
23-
ModelsWithAnimations,
24-
ModelsWithAnimationsAndAlphaCut,
25-
};
26-
*/
27-
2813
struct CAGE_ENGINE_API ScenePickingConfig
2914
{
3015
Line picker;
16+
LodSelection lodSelection;
3117
const AssetsManager *assets = nullptr;
3218
const EntityManager *entities = nullptr;
33-
//ScenePickingPrecisionEnum precision = ScenePickingPrecisionEnum::Models;
34-
35-
// used for lod selection
36-
const Entity *camera = nullptr;
37-
uint32 screenHeight = 0;
3819
};
3920

4021
struct CAGE_ENGINE_API ScenePickingResult

sources/libcore/geometry/collider.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <vector>
33

44
#include <cage-core/collider.h>
5-
//#include <cage-core/flatSet.h>
65
#include <cage-core/geometry.h>
76
#include <cage-core/memoryBuffer.h>
87
#include <cage-core/mesh.h>
@@ -430,6 +429,7 @@ namespace cage
430429

431430
CollisionDetector(const ColliderImpl *ao, const ColliderImpl *bo, Transform am, Transform bm, Holder<PointerRange<CollisionPair>> &outputBuffer) : ats(ao->tris.data(), numeric_cast<uint32>(ao->tris.size()), am), bts(bo->tris.data(), numeric_cast<uint32>(bo->tris.size()), bm), abs(ao->boxes.data(), numeric_cast<uint32>(ao->boxes.size()), am), bbs(bo->boxes.data(), numeric_cast<uint32>(bo->boxes.size()), bm), ao(ao), bo(bo), outputBuffer(outputBuffer) {}
432431

432+
private:
433433
void process(uint32 a, uint32 b)
434434
{
435435
CAGE_ASSERT(a < ao->nodes.size());
@@ -483,6 +483,7 @@ namespace cage
483483
}
484484
}
485485

486+
public:
486487
void process()
487488
{
488489
process(0, 0);
@@ -528,6 +529,7 @@ namespace cage
528529

529530
IntersectionDetector(const ColliderImpl *collider, Transform m) : col(collider), m(m) { CAGE_ASSERT(!collider->dirty); }
530531

532+
private:
531533
template<class T>
532534
Real distance(const T &l, uint32 nodeIdx)
533535
{
@@ -561,12 +563,16 @@ namespace cage
561563
}
562564
}
563565

566+
public:
564567
template<class T>
565568
Real distance(const T &shape)
566569
{
567-
return distance(shape * inverse(m), 0);
570+
CAGE_THROW_CRITICAL(Exception, "distance to collider is untested and broken");
571+
// the result is in the original space of the collider and must be converted back to the space of the shape
572+
return distance(shape * inverse(m), 0) * m.scale;
568573
}
569574

575+
private:
570576
template<class T>
571577
bool intersects(const T &l, uint32 nodeIdx)
572578
{
@@ -590,12 +596,14 @@ namespace cage
590596
}
591597
}
592598

599+
public:
593600
template<class T>
594601
bool intersects(const T &shape)
595602
{
596603
return intersects(shape * inverse(m), 0);
597604
}
598605

606+
private:
599607
bool intersection(Line l, uint32 nodeIdx, Vec3 &point, uint32 &triangleIndex)
600608
{
601609
const Aabb b = col->boxes[nodeIdx];
@@ -664,14 +672,15 @@ namespace cage
664672
}
665673
}
666674

675+
public:
667676
bool intersection(Line l, Vec3 &point, uint32 &triangleIndex)
668677
{
678+
// p is in the original space of the collider and must be converted back to the space of the shape
669679
Vec3 pt;
670680
uint32 ti = 0;
671681
if (intersection(l * inverse(m), 0, pt, ti))
672682
{
673-
const Vec4 r4 = Vec4(pt, 1) * Mat4(m);
674-
point = Vec3(r4) / r4[3];
683+
point = pt * m;
675684
triangleIndex = ti;
676685
return true;
677686
}
@@ -789,6 +798,12 @@ namespace cage
789798
}
790799
}
791800

801+
Real distance(Vec3 shape, const Collider *collider, Transform t)
802+
{
803+
IntersectionDetector d((const ColliderImpl *)collider, t);
804+
return d.distance(shape);
805+
}
806+
792807
Real distance(Line shape, const Collider *collider, Transform t)
793808
{
794809
IntersectionDetector d((const ColliderImpl *)collider, t);
@@ -892,7 +907,6 @@ namespace cage
892907

893908
Vec3 intersection(Line shape, const Collider *collider, Transform t, uint32 &triangleIndex)
894909
{
895-
// todo
896910
IntersectionDetector d((const ColliderImpl *)collider, t);
897911
Vec3 p;
898912
if (d.intersection(shape, p, triangleIndex))

0 commit comments

Comments
 (0)