-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.cpp
1312 lines (1047 loc) · 37.4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <math.h>
#include <memory.h>
#include <string>
#include <random>
#include <Recast.h>
#include <InputGeom.h>
#include <DetourNavMesh.h>
#include <DetourCommon.h>
#include <DetourNavMeshQuery.h>
#include <DetourNavMeshBuilder.h>
#include <DetourCrowd.h>
#include <RecastDebugDraw.h>
#include <DetourDebugDraw.h>
#include <emscripten.h>
#include <SampleInterfaces.h>
static const int MAX_POLYS = 256;
////////////////////////////
// CUSTOM
//////////////
rcMeshLoaderObj* meshLoader;
////////////////////////////
// FROM SAMPLE
//////////////
InputGeom* m_geom;
dtNavMesh* m_navMesh;
dtNavMeshQuery* m_navQuery;
dtCrowd* m_crowd;
unsigned char m_navMeshDrawFlags;
bool m_keepInterResults = true;
float m_totalBuildTimeMs;
//// DEFAULTS
float m_agentHeight = 2.0f; // , 5.0f, 0.1f);
float m_agentRadius = 0.5f; // , 5.0f, 0.1f);
float m_cellSize = m_agentRadius / 2;
float m_cellHeight = m_cellSize / 2;
float m_agentMaxClimb = 4.0f; // , 5.0f, 0.1f);
float m_agentMaxSlope = 30.0f; // , 90.0f, 1.0f);
float m_regionMinSize = 1.0f; // , 150.0f, 1.0f);
float m_regionMergeSize = 1.0f; // , 150.0f, 1.0f);
bool m_monotonePartitioning = 0;
float m_edgeMaxLen = 50.0f; // , 50.0f, 1.0f);
float m_edgeMaxError = 1.0f; // , 3.0f, 0.1f);
float m_vertsPerPoly = 3.0f; // , 12.0f, 1.0f);
float m_detailSampleDist = 0.0f; // , 16.0f, 1.0f);
float m_detailSampleMaxError = 8.0f; // , 16.0f, 1.0f);
unsigned char* m_triareas;
rcHeightfield* m_solid;
rcCompactHeightfield* m_chf;
rcContourSet* m_cset;
rcPolyMesh* m_pmesh;
rcConfig m_cfg;
rcPolyMeshDetail* m_dmesh;
/// Tool types.
enum SampleToolType
{
TOOL_NONE = 0,
TOOL_TILE_EDIT,
TOOL_TILE_HIGHLIGHT,
TOOL_TEMP_OBSTACLE,
TOOL_NAVMESH_TESTER,
TOOL_NAVMESH_PRUNE,
TOOL_OFFMESH_CONNECTION,
TOOL_CONVEX_VOLUME,
TOOL_CROWD,
MAX_TOOLS
};
/// These are just sample areas to use consistent values across the samples.
/// The use should specify these base on his needs.
enum SamplePolyAreas
{
SAMPLE_POLYAREA_GROUND,
SAMPLE_POLYAREA_WATER,
SAMPLE_POLYAREA_ROAD,
SAMPLE_POLYAREA_DOOR,
SAMPLE_POLYAREA_GRASS,
SAMPLE_POLYAREA_JUMP,
};
enum SamplePolyFlags
{
SAMPLE_POLYFLAGS_WALK = 0x01, // Ability to walk (ground, grass, road)
SAMPLE_POLYFLAGS_SWIM = 0x02, // Ability to swim (water).
SAMPLE_POLYFLAGS_DOOR = 0x04, // Ability to move through doors.
SAMPLE_POLYFLAGS_JUMP = 0x08, // Ability to jump.
SAMPLE_POLYFLAGS_DISABLED = 0x10, // Disabled polygon
SAMPLE_POLYFLAGS_ALL = 0xffff // All abilities.
};
enum DrawMode
{
DRAWMODE_NAVMESH,
DRAWMODE_NAVMESH_TRANS,
DRAWMODE_NAVMESH_BVTREE,
DRAWMODE_NAVMESH_NODES,
DRAWMODE_NAVMESH_INVIS,
DRAWMODE_MESH,
DRAWMODE_VOXELS,
DRAWMODE_VOXELS_WALKABLE,
DRAWMODE_COMPACT,
DRAWMODE_COMPACT_DISTANCE,
DRAWMODE_COMPACT_REGIONS,
DRAWMODE_REGION_CONNECTIONS,
DRAWMODE_RAW_CONTOURS,
DRAWMODE_BOTH_CONTOURS,
DRAWMODE_CONTOURS,
DRAWMODE_POLYMESH,
DRAWMODE_POLYMESH_DETAIL,
MAX_DRAWMODE
};
DrawMode m_drawMode;
BuildContext* m_ctx;
DebugDrawGL* dd;
////////////////////////////
extern "C" {
extern char* getThreeJSMeshes();
}
/////////////////////////////////
void debugConfig()
{
printf("config \n");
printf(" m_cellSize=%f \n", m_cellSize);
printf(" m_cellHeight=%f \n", m_cellHeight);
//// DEFAULTS
printf(" m_agentHeight=%f \n", m_agentHeight);
printf(" m_agentRadius=%f \n", m_agentRadius);
printf(" m_agentMaxClimb=%f \n", m_agentMaxClimb);
printf(" m_agentMaxSlope=%f \n", m_agentMaxSlope);
printf(" m_monotonePartitioning=%u\n", m_monotonePartitioning);
printf(" m_regionMinSize=%f \n", m_regionMinSize);
printf(" m_regionMergeSize=%f \n", m_regionMergeSize);
printf(" m_edgeMaxLen=%f \n", m_edgeMaxLen);
printf(" m_edgeMaxError=%f \n", m_edgeMaxError);
printf(" m_vertsPerPoly=%f \n", m_vertsPerPoly);
printf(" m_detailSampleDist=%f \n", m_detailSampleDist);
printf(" m_detailSampleMaxError=%f \n", m_detailSampleMaxError);
}
void debugDrawNavMesh(unsigned char flags) {
duDebugDrawNavMesh(dd, *m_navMesh, flags);
}
void debugDrawNavMeshPortals() {
duDebugDrawNavMeshPortals(dd, *m_navMesh);
}
void debugDrawRegionConnections() {
duDebugDrawRegionConnections(dd, *m_cset, 0.5f);
}
void debugDrawRawContours() {
duDebugDrawRawContours(dd, *m_cset, 0.5f);
}
void debugDrawContours() {
duDebugDrawContours(dd, *m_cset, 0.5f);
}
void debugDrawHeightfieldSolid() {
duDebugDrawHeightfieldSolid(dd, *m_solid);
}
void debugDrawHeightfieldWalkable() {
duDebugDrawHeightfieldWalkable(dd, *m_solid);
}
////////////////////////////
void emscripten_log(char* string, bool escape = true)
{
char buff[1024];
sprintf(buff, (escape ? "console.log('%s');" : "console.log(%s);"), string);
emscripten_run_script(buff);
// free(buff);
}
void emscripten_debugger()
{
emscripten_run_script("debugger");
}
////////////////////////////
void cleanup()
{
printf("cleanup \n");
delete [] m_triareas;
m_triareas = 0;
rcFreeHeightField(m_solid);
m_solid = 0;
rcFreeCompactHeightfield(m_chf);
m_chf = 0;
rcFreeContourSet(m_cset);
m_cset = 0;
rcFreePolyMesh(m_pmesh);
m_pmesh = 0;
rcFreePolyMeshDetail(m_dmesh);
m_dmesh = 0;
dtFreeNavMesh(m_navMesh);
m_navMesh = 0;
//dtNavMeshQuery(m_navQuery);
m_navQuery = 0;
}
void bareGeomInit(){
m_geom = new InputGeom;
meshLoader = new rcMeshLoaderObj;
}
void bareGeomAddVertex(float x, float y, float z, int cap){
meshLoader->addVertex(x, y, z, cap);
}
void bareGeomAddTriangle(int a, int b, int c, int cap){
meshLoader->addTriangle(a, b, c, cap);
}
void bareGeomValidate(){
m_geom->loadFromMeshLoader(m_ctx, meshLoader);
}
void getNavMeshVertices(std::string callback){
const int nvp = m_pmesh->nvp;
const float cs = m_pmesh->cs;
const float ch = m_pmesh->ch;
const float* orig = m_pmesh->bmin;
char buff[512];
sprintf(buff, "nvp=%u, cs=%f, ch=%f, orig={%f, %f, %f}", nvp, cs, ch, orig[0], orig[1], orig[2]);
emscripten_log(buff);
emscripten_run_script("__tmp_recastjs_data = [];");
for (int i = 0; i < m_pmesh->npolys; ++i)
{
if (m_pmesh->areas[i] == SAMPLE_POLYAREA_GROUND)
{
const unsigned short* p = &m_pmesh->polys[i*nvp*2];
unsigned short vi[3];
for (int j = 2; j < nvp; ++j)
{
if (p[j] == RC_MESH_NULL_IDX) break;
vi[0] = p[0];
vi[1] = p[j-1];
vi[2] = p[j];
for (int k = 0; k < 3; ++k)
{
const unsigned short* v = &m_pmesh->verts[vi[k]*3];
const float x = orig[0] + v[0]*cs;
const float y = orig[1] + (v[1]+1)*ch;
const float z = orig[2] + v[2]*cs;
sprintf(buff, "__tmp_recastjs_data.push({x:%f, y:%f, z:%f});", x, y, z);
emscripten_run_script(buff);
}
}
}
}
sprintf(buff, "%s(__tmp_recastjs_data);", callback.c_str());
emscripten_run_script(buff);
// free(buff);
}
void getNavMeshTiles(std::string callback){
}
float randomf()
{
return (static_cast <float> (rand()) / static_cast <float> (RAND_MAX));
}
void getRandomPoint(std::string callback)
{
char buff[512];
dtQueryFilter filter;
filter.setIncludeFlags(3);
filter.setExcludeFlags(0);
dtPolyRef ref = 0;
float randomPt[3];
dtStatus findStatus = m_navQuery->findRandomPoint(&filter, randomf, &ref, randomPt);
if (dtStatusFailed(findStatus)) {
printf("Cannot find a random point: %u\n", findStatus);
sprintf(buff, "%s(null);", callback.c_str());
emscripten_run_script(buff);
} else {
sprintf(buff, "%s({x:%f, y:%f, z:%f});", callback.c_str(), randomPt[0], randomPt[1], randomPt[2]);
emscripten_run_script(buff);
}
// free(buff);
}
void getNavHeightfieldRegions(std::string callback)
{
if (! m_chf) {
printf("CompactHeightfield (m_chf) is not available \n");
return;
}
const float cs = m_chf->cs;
const float ch = m_chf->ch;
int height = m_chf->height;
int width = m_chf->width;
char buff[512];
sprintf(buff, "cs=%f, ch=%f, height=%u, width=%u", cs, ch, height, width);
emscripten_log(buff);
emscripten_run_script("__tmp_recastjs_data = [];");
for (int y = 0; y < m_chf->height; ++y)
{
printf("on y=%u \n", y);
for (int x = 0; x < m_chf->width; ++x)
{
const float fx = m_chf->bmin[0] + x*cs;
const float fz = m_chf->bmin[2] + y*cs;
const rcCompactCell& c = m_chf->cells[x+y*m_chf->width];
for (unsigned i = c.index, ni = c.index+c.count; i < ni; ++i)
{
const rcCompactSpan& s = m_chf->spans[i];
const float fy = m_chf->bmin[1] + (s.y)*ch;
unsigned int color;
if (s.reg)
color = duIntToCol(s.reg, 192);
else
color = duRGBA(0,0,0,64);
sprintf(buff, "__tmp_recastjs_data.push({x:%f, y:%f, z:%f});", fx, fy, fz); emscripten_run_script(buff);
sprintf(buff, "__tmp_recastjs_data.push({x:%f, y:%f, z:%f});", fx, fy, fz+cs); emscripten_run_script(buff);
sprintf(buff, "__tmp_recastjs_data.push({x:%f, y:%f, z:%f});", fx+cs, fy, fz+cs);emscripten_run_script(buff);
sprintf(buff, "__tmp_recastjs_data.push({x:%f, y:%f, z:%f});", fx+cs, fy, fz); emscripten_run_script(buff);
}
}
}
sprintf(buff, "%s(__tmp_recastjs_data);", callback.c_str());
emscripten_run_script(buff);
// free(buff);
}
void findNearestPoly(float cx, float cy, float cz,
float ex, float ey, float ez,
/*const dtQueryFilter* filter,
dtPolyRef* nearestRef, float* nearestPt*/
std::string callback)
{
emscripten_run_script("__tmp_recastjs_data = [];");
char buff[512];
const float p[3] = {cx,cy,cz};
const float ext[3] = {ex,ey,ez};
float nearestPt[3];
dtQueryFilter filter;
filter.setIncludeFlags(3);
filter.setExcludeFlags(0);
dtPolyRef ref = 0;
dtStatus findStatus = m_navQuery->findNearestPoly(p, ext, &filter, &ref, 0);
if (dtStatusFailed(findStatus)) {
printf("Cannot find nearestPoly: %u\n", findStatus);
} else {
const dtMeshTile* tile;
const dtPoly* poly;
findStatus = m_navMesh->getTileAndPolyByRef(ref, &tile, &poly);
if (dtStatusFailed(findStatus)) {
printf("Cannot get tile and poly by ref #%u : %u \n", ref, findStatus);
} else {
// TODO: put poly and tile in __tmp_recastjs_data
for (int i = 0; i < tile->header->vertCount; i++) {
float* v = &tile->verts[i];
sprintf(buff, "__tmp_recastjs_data.push({x:%f, y:%f, z:%f});", v[0], v[1], v[2]);
emscripten_run_script(buff);
}
}
}
sprintf(buff, "%s(__tmp_recastjs_data);", callback.c_str());
emscripten_run_script(buff);
// free(buff);
}
void findNearestPoint(float cx, float cy, float cz,
float ex, float ey, float ez,
/*const dtQueryFilter* filter,
dtPolyRef* nearestRef, float* nearestPt*/
std::string callback)
{
emscripten_run_script("__tmp_recastjs_data = [];");
char buff[512];
const float p[3] = {cx,cy,cz};
const float ext[3] = {ex,ey,ez};
float nearestPt[3];
dtQueryFilter filter;
filter.setIncludeFlags(3);
filter.setExcludeFlags(0);
dtPolyRef ref = 0;
float nearestPos[3];
dtStatus findStatus = m_navQuery->findNearestPoly(p, ext, &filter, &ref, nearestPos);
if (dtStatusFailed(findStatus)) {
sprintf(buff, "%s(null);", callback.c_str());
} else {
sprintf(buff, "%s({ x:%f, y:%f, z:%f });", callback.c_str(), nearestPos[0], nearestPos[1], nearestPos[2]);
}
emscripten_run_script(buff);
}
void setPolyUnwalkable(float posX, float posY, float posZ, float extendX, float extendY, float extendZ, unsigned short flags)
{
dtQueryFilter filter;
filter.setIncludeFlags(3);
filter.setExcludeFlags(0);
const float ext[3] = {extendX, extendY, extendZ};
float startPos[3] = { posX, posY, posZ };
float nearestPos[3];
dtPolyRef ref = 0;
dtStatus findStatus;
findStatus = m_navQuery->findNearestPoly(startPos, ext, &filter, &ref, nearestPos);
if (dtStatusFailed(findStatus)) {
printf("Cannot find a poly near: %f, %f, %f \n", posX, posY, posZ);
} else {
printf("Set poly %u as unwalkable \n", ref);
m_navMesh->setPolyFlags(ref, flags);
}
}
void findPath(float startPosX, float startPosY, float startPosZ,
float endPosX, float endPosY, float endPosZ, int maxPath,
std::string callback)
{
emscripten_run_script("__tmp_recastjs_data = [];");
char buff[512];
float startPos[3] = { startPosX, startPosY, startPosZ };
float endPos[3] = { endPosX, endPosY, endPosZ };
const float ext[3] = {2,4,2};
dtStatus findStatus;
dtPolyRef path[maxPath+1];
int pathCount;
dtQueryFilter filter;
filter.setIncludeFlags(3);
filter.setExcludeFlags(0);
// Change costs.
// filter.setAreaCost(SAMPLE_POLYAREA_GROUND, 1.0f);
// filter.setAreaCost(SAMPLE_POLYAREA_WATER, 10.0f);
// filter.setAreaCost(SAMPLE_POLYAREA_ROAD, 1.0f);
// filter.setAreaCost(SAMPLE_POLYAREA_DOOR, 1.0f);
// filter.setAreaCost(SAMPLE_POLYAREA_GRASS, 2.0f);
// filter.setAreaCost(SAMPLE_POLYAREA_JUMP, 1.5f);
float nearestStartPos[3];
dtPolyRef startRef = 0;
m_navQuery->findNearestPoly(startPos, ext, &filter, &startRef, nearestStartPos);
float nearestEndPos[3];
dtPolyRef endRef = 0;
m_navQuery->findNearestPoly(endPos, ext, &filter, &endRef, nearestEndPos);
printf("Use %u , %u as start / end polyRefs \n", startRef, endRef);
findStatus = m_navQuery->findPath(startRef, endRef, nearestStartPos, nearestEndPos, &filter, path, &pathCount, maxPath);
if (dtStatusFailed(findStatus)) {
printf("Cannot find a path: %u\n", findStatus);
} else {
printf("Found a %u polysteps path \n", pathCount);
float straightPath[maxPath*3];
unsigned char straightPathFlags[maxPath];
dtPolyRef straightPathRefs[maxPath];
int straightPathCount = 0;
int maxStraightPath = maxPath;
int options = 0;
findStatus = m_navQuery->findStraightPath(nearestStartPos, nearestEndPos, path, pathCount, straightPath,
straightPathFlags, straightPathRefs, &straightPathCount, maxStraightPath, options);
if (dtStatusFailed(findStatus)) {
printf("Cannot find a straight path: %u\n", findStatus);
} else {
printf("Found a %u steps path \n", straightPathCount);
for (int i = 0; i < straightPathCount; ++i) {
const float* v = &straightPath[i*3];
// why ?
if (!(fabs(v[0]) < 0.0000001f && fabs(v[1]) < 0.0000001f && fabs(v[2]) < 0.0000001f)) {
sprintf(buff, "__tmp_recastjs_data.push({x:%f, y:%f, z:%f});", v[0], v[1], v[2]);
emscripten_run_script(buff);
} else {
sprintf(buff, "ignore %f, %f, %f", v[0], v[1], v[2]);
emscripten_log(buff);
}
}
}
}
sprintf(buff, "%s(__tmp_recastjs_data);", callback.c_str());
emscripten_run_script(buff);
// free(buff);
}
void set_cellSize(float val){ m_cellSize = val; }
void set_cellHeight(float val){ m_cellHeight = val; }
void set_agentHeight(float val){ m_agentHeight = val; }
void set_agentRadius(float val){ m_agentRadius = val; }
void set_agentMaxClimb(float val){ m_agentMaxClimb = val; }
void set_agentMaxSlope(float val){ m_agentMaxSlope = val; }
void set_regionMinSize(float val){ m_regionMinSize = val; }
void set_regionMergeSize(float val){ m_regionMergeSize = val; }
void set_edgeMaxLen(float val){ m_edgeMaxLen = val; }
void set_edgeMaxError(float val){ m_edgeMaxError = val; }
void set_vertsPerPoly(float val){ m_vertsPerPoly = val; }
void set_detailSampleDist(float val){ m_detailSampleDist = val; }
void set_detailSampleMaxError(float val){ m_detailSampleMaxError = val; }
void set_monotonePartitioning(int val){ m_monotonePartitioning = !!val; }
/////////////////////////////
bool init()
{
return getThreeJSMeshes();
}
bool initWithFile(std::string filename)
{
printf("loading from file");
m_geom = new InputGeom;
if (!m_geom || !m_geom->loadMesh(m_ctx, filename.c_str()))
{
printf("cannot load OBJ file \n");
return false;
}
return true;
}
bool initWithFileContent(std::string contents)
{
printf("loading from contents \n");
// printf(contents.c_str());
m_geom = new InputGeom;
if (!m_geom || !m_geom->loadMeshFromContents(m_ctx, contents.c_str()))
{
printf("cannot load OBJ contents \n");
return false;
}
return true;
}
bool initCrowd(const int maxAgents, const float maxAgentRadius)
{
m_crowd->init(maxAgents, maxAgentRadius, m_navMesh);
return true;
}
struct agentUserData {
int idx;
};
void updateCrowdAgentParameters(const int idx, float posX, float posY, float posZ, float radius, float height,
float maxAcceleration, float maxSpeed, unsigned char updateFlags, float separationWeight)
{
dtCrowdAgentParams ap;
memset(&ap, 0, sizeof(ap));
ap.radius = radius;
ap.height = height;
ap.maxAcceleration = maxAcceleration;
ap.maxSpeed = maxSpeed;
ap.collisionQueryRange = ap.radius * 12.0f;
ap.pathOptimizationRange = ap.radius * 300.0f;
ap.updateFlags = 0;
// if (m_toolParams.m_anticipateTurns)
// ap.updateFlags |= DT_CROWD_ANTICIPATE_TURNS;
// if (m_toolParams.m_optimizeVis)
// ap.updateFlags |= DT_CROWD_OPTIMIZE_VIS;
// if (m_toolParams.m_optimizeTopo)
// ap.updateFlags |= DT_CROWD_OPTIMIZE_TOPO;
// if (m_toolParams.m_obstacleAvoidance)
// ap.updateFlags |= DT_CROWD_OBSTACLE_AVOIDANCE;
// if (m_toolParams.m_separation)
// ap.updateFlags |= DT_CROWD_SEPARATION;
ap.obstacleAvoidanceType = 3.0;
ap.separationWeight = separationWeight;
ap.userData = (void *)idx;
float pos[3] = { posX, posY, posZ };
m_crowd->updateAgentParameters(idx, &ap);
}
int addCrowdAgent(float posX, float posY, float posZ, float radius, float height,
float maxAcceleration, float maxSpeed, unsigned char updateFlags, float separationWeight)
{
dtCrowdAgentParams ap;
memset(&ap, 0, sizeof(ap));
ap.radius = radius;
ap.height = height;
ap.maxAcceleration = maxAcceleration;
ap.maxSpeed = maxSpeed;
ap.collisionQueryRange = ap.radius * 12.0f;
ap.pathOptimizationRange = ap.radius * 300.0f;
ap.updateFlags = 0;
// if (m_toolParams.m_anticipateTurns)
// ap.updateFlags |= DT_CROWD_ANTICIPATE_TURNS;
// if (m_toolParams.m_optimizeVis)
// ap.updateFlags |= DT_CROWD_OPTIMIZE_VIS;
// if (m_toolParams.m_optimizeTopo)
// ap.updateFlags |= DT_CROWD_OPTIMIZE_TOPO;
// if (m_toolParams.m_obstacleAvoidance)
// ap.updateFlags |= DT_CROWD_OBSTACLE_AVOIDANCE;
// if (m_toolParams.m_separation)
// ap.updateFlags |= DT_CROWD_SEPARATION;
ap.obstacleAvoidanceType = 3.0;
ap.separationWeight = separationWeight;
float pos[3] = { posX, posY, posZ };
int idx = m_crowd->addAgent(pos, &ap);
// agentUserData data;
// memset(&data, 0, sizeof(data));
// data.idx = 99;
// ap.userData = (void *) &data;
ap.userData = (void *)idx; /* FIXME: doesnt fucking work ://// */
/* So we do this ?? */
updateCrowdAgentParameters(idx, posX, posY, posZ, radius, height, maxAcceleration, maxSpeed, updateFlags, separationWeight);
// char buff[512];
// const dtCrowdAgent* ag = m_crowd->getAgent(idx);
// const float* p = ag->npos;
// const float r = ag->params.radius;
// sprintf(buff, "debug('new agent', { idx:%d });", idx);
// emscripten_run_script(buff);
return idx;
}
void removeCrowdAgent(int idx)
{
m_crowd->removeAgent(idx);
}
void requestMoveVelocity(int agentIdx, float velX, float velY, float velZ)
{
float vel[3] = { velX, velY, velZ };
m_crowd->requestMoveVelocity(agentIdx, vel);
}
bool crowdRequestMoveTarget(int agentIdx, float posX, float posY, float posZ)
{
char buff[512];
float pos[3] = { posX, posY, posZ };
const float ext[3] = {2,4,2};
dtPolyRef m_targetRef = 0;
float m_targetPos[3];
dtQueryFilter filter;
filter.setIncludeFlags(3);
filter.setExcludeFlags(0);
dtStatus findStatus = m_navQuery->findNearestPoly(pos, ext, &filter, &m_targetRef, m_targetPos);
if (dtStatusFailed(findStatus)) {
// emscripten_run_script("debug('Cannot find a poly near specified position');");
return false;
} else {
// emscripten_run_script("debug('MoveTarget adjusted');");
}
m_crowd->requestMoveTarget(agentIdx, m_targetRef, m_targetPos);
return true;
}
bool crowdUpdate(float dt)
{
char buff[512];
dtCrowdAgentDebugInfo m_agentDebug;
// memset(&m_agentDebug, 0, sizeof(m_agentDebug));
m_crowd->update(dt, &m_agentDebug);
return true;
}
bool crowdGetActiveAgents(std::string callback)
{
int maxAgents = 100;
dtCrowdAgent** agents = (dtCrowdAgent**)dtAlloc(sizeof(dtCrowdAgent*)*maxAgents, DT_ALLOC_PERM);
int nagents = m_crowd->getActiveAgents(agents, maxAgents);
char buff[1024 * nagents];
/*
struct dtCrowdAgent
{
/// 1 if the agent is active, or 0 if the agent is in an unused slot in the agent pool.
unsigned char active;
/// The type of mesh polygon the agent is traversing. (See: #CrowdAgentState)
unsigned char state;
/// The path corridor the agent is using.
dtPathCorridor corridor;
/// The local boundary data for the agent.
dtLocalBoundary boundary;
/// Time since the agent's path corridor was optimized.
float topologyOptTime;
/// The known neighbors of the agent.
dtCrowdNeighbour neis[DT_CROWDAGENT_MAX_NEIGHBOURS];
/// The number of neighbors.
int nneis;
/// The desired speed.
float desiredSpeed;
float npos[3]; ///< The current agent position. [(x, y, z)]
float disp[3];
float dvel[3]; ///< The desired velocity of the agent. [(x, y, z)]
float nvel[3];
float vel[3]; ///< The actual velocity of the agent. [(x, y, z)]
/// The agent's configuration parameters.
dtCrowdAgentParams params;
/// The local path corridor corners for the agent. (Staight path.) [(x, y, z) * #ncorners]
float cornerVerts[DT_CROWDAGENT_MAX_CORNERS*3];
/// The local path corridor corner flags. (See: #dtStraightPathFlags) [(flags) * #ncorners]
unsigned char cornerFlags[DT_CROWDAGENT_MAX_CORNERS];
/// The reference id of the polygon being entered at the corner. [(polyRef) * #ncorners]
dtPolyRef cornerPolys[DT_CROWDAGENT_MAX_CORNERS];
/// The number of corners.
int ncorners;
unsigned char targetState; ///< State of the movement request.
dtPolyRef targetRef; ///< Target polyref of the movement request.
float targetPos[3]; ///< Target position of the movement request (or velocity in case of DT_CROWDAGENT_TARGET_VELOCITY).
dtPathQueueRef targetPathqRef; ///< Path finder ref.
bool targetReplan; ///< Flag indicating that the current path is being replanned.
float targetReplanTime; /// <Time since the agent's target was replanned.
};
*/
sprintf(buff, "__tmp_recastjs_crowd_data = []; var object;");
for (int i = 0; i < nagents; i++) {
dtCrowdAgent* ag = agents[i];
const float* p = ag->npos;
const float* v = ag->vel;
const float r = ag->params.radius;
int idx = (int) ag->params.userData;
// agentUserData* data = (agentUserData*) ag->params.userData;
// sprintf(buff, "debug({ position:{x:%f,y:%f,z:%f}, radius:%f, active:%d, state:%d });", p[0], p[1], p[2], r, ag->active, ag->state);
// emscripten_run_script(buff);
// sprintf(buff, "%s \n { \n var object = /* agentPool.get(); */ { position:{}, velocity:{} }; \n object.idx=%d; object.position.x=%f; object.position.y=%f; object.position.z=%f; object.velocity.x=%f; object.velocity.y=%f; object.velocity.z=%f; object.radius=%f; object.active=%d; object.state=%d; object.neighbors=%d; \n __tmp_recastjs_crowd_data.push(object);",
// buff, idx, p[0], p[1], p[2], v[0], v[1], v[2], r, ag->active, ag->state, ag->nneis);
sprintf(buff, "%s \n object = agentPool.get(%d, %f, %f, %f, %f, %f, %f, %f, %d, %d, %d); \n __tmp_recastjs_crowd_data.push(object);",
buff, idx, p[0], p[1], p[2], v[0], v[1], v[2], r, ag->active, ag->state, ag->nneis);
}
sprintf(buff, "\n %s \n\n %s(__tmp_recastjs_crowd_data);", buff, callback.c_str());
for (int i = 0; i < nagents; i++) {
sprintf(buff, "%s \n agentPool.add(__tmp_recastjs_crowd_data[%d]); \n", buff, i);
}
emscripten_run_script(buff);
// free(buff);
return true;
}
bool build()
{
dd = new DebugDrawGL;
if (!m_geom || !m_geom->getMesh())
{
printf("buildNavigation: Input mesh is not specified.");
m_ctx->log(RC_LOG_ERROR, "buildNavigation: Input mesh is not specified.");
return false;
}
cleanup();
const float* bmin = m_geom->getMeshBoundsMin();
const float* bmax = m_geom->getMeshBoundsMax();
const float* verts = m_geom->getMesh()->getVerts();
const int nverts = m_geom->getMesh()->getVertCount();
const int* tris = m_geom->getMesh()->getTris();
const int ntris = m_geom->getMesh()->getTriCount();
//
// Step 1. Initialize build config.
//
// Init build configuration from GUI
memset(&m_cfg, 0, sizeof(m_cfg));
m_cfg.cs = m_cellSize;
m_cfg.ch = m_cellHeight;
m_cfg.walkableSlopeAngle = m_agentMaxSlope;
m_cfg.walkableHeight = (int)ceilf(m_agentHeight / m_cfg.ch);
m_cfg.walkableClimb = (int)floorf(m_agentMaxClimb / m_cfg.ch);
m_cfg.walkableRadius = (int)ceilf(m_agentRadius / m_cfg.cs);
m_cfg.maxEdgeLen = (int)(m_edgeMaxLen / m_cellSize);
m_cfg.maxSimplificationError = m_edgeMaxError;
m_cfg.minRegionArea = (int)rcSqr(m_regionMinSize); // Note: area = size*size
m_cfg.mergeRegionArea = (int)rcSqr(m_regionMergeSize); // Note: area = size*size
m_cfg.maxVertsPerPoly = (int)m_vertsPerPoly;
m_cfg.detailSampleDist = m_detailSampleDist < 0.9f ? 0 : m_cellSize * m_detailSampleDist;
m_cfg.detailSampleMaxError = m_cellHeight * m_detailSampleMaxError;
// Set the area where the navigation will be build.
// Here the bounds of the input mesh are used, but the
// area could be specified by an user defined box, etc.
rcVcopy(m_cfg.bmin, bmin);
rcVcopy(m_cfg.bmax, bmax);
rcCalcGridSize(m_cfg.bmin, m_cfg.bmax, m_cfg.cs, &m_cfg.width, &m_cfg.height);
// Reset build times gathering.
//m_ctx->resetTimers();
printf("resetTimers \n");
// Start the build process.
//m_ctx->startTimer(RC_TIMER_TOTAL);
m_ctx->log(RC_LOG_PROGRESS, "Building navigation:");
m_ctx->log(RC_LOG_PROGRESS, " - %d x %d cells", m_cfg.width, m_cfg.height);
m_ctx->log(RC_LOG_PROGRESS, " - %.1fK verts, %.1fK tris", nverts/1000.0f, ntris/1000.0f);
printf("Building navigation \n");
//
// Step 2. Rasterize input polygon soup.
//
// Allocate voxel heightfield where we rasterize our input data to.
m_solid = rcAllocHeightfield();
if (!m_solid)
{
printf("buildNavigation: Out of memory 'solid'. \n");
m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'solid'.");
return false;
}
if (!rcCreateHeightfield(m_ctx, *m_solid, m_cfg.width, m_cfg.height, m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch))
{
printf("buildNavigation: Could not create solid heightfield. \n");
m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not create solid heightfield.");
return false;
}
printf("Heightfield polygon soup \n");
// Allocate array that can hold triangle area types.
// If you have multiple meshes you need to process, allocate
// and array which can hold the max number of triangles you need to process.
m_triareas = new unsigned char[ntris];
if (!m_triareas)
{
m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'm_triareas' (%d).", ntris);
return false;
}
// Find triangles which are walkable based on their slope and rasterize them.
// If your input data is multiple meshes, you can transform them here, calculate
// the are type for each of the meshes and rasterize them.
memset(m_triareas, 0, ntris*sizeof(unsigned char));
rcMarkWalkableTriangles(m_ctx, m_cfg.walkableSlopeAngle, verts, nverts, tris, ntris, m_triareas);
printf("%u Walkable Triangles\n", sizeof(m_triareas)/sizeof(unsigned char));
rcRasterizeTriangles(m_ctx, verts, nverts, tris, m_triareas, ntris, *m_solid, m_cfg.walkableClimb);
if (!m_keepInterResults)
{
delete [] m_triareas;
m_triareas = 0;
}
//
// Step 3. Filter walkables surfaces.
//
// Once all geoemtry is rasterized, we do initial pass of filtering to
// remove unwanted overhangs caused by the conservative rasterization
// as well as filter spans where the character cannot possibly stand.
rcFilterLowHangingWalkableObstacles(m_ctx, m_cfg.walkableClimb, *m_solid);
rcFilterLedgeSpans(m_ctx, m_cfg.walkableHeight, m_cfg.walkableClimb, *m_solid);
rcFilterWalkableLowHeightSpans(m_ctx, m_cfg.walkableHeight, *m_solid);
printf("filters \n");
//
// Step 4. Partition walkable surface to simple regions.
//
// Compact the heightfield so that it is faster to handle from now on.
// This will result more cache coherent data as well as the neighbours
// between walkable cells will be calculated.
m_chf = rcAllocCompactHeightfield();
if (!m_chf)
{
m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'chf'.");
return false;
}
if (!rcBuildCompactHeightfield(m_ctx, m_cfg.walkableHeight, m_cfg.walkableClimb, *m_solid, *m_chf))
{
m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build compact data.");
return false;
}
if (!m_keepInterResults)
{