-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hx
207 lines (161 loc) · 4.9 KB
/
Main.hx
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
#if js
import js.lib.Int32Array;
import js.lib.Float32Array;
#end
import h3d.col.Point;
import hxd.Key;
import h3d.scene.Graphics;
import h3d.mat.MaterialSetup;
import h3d.scene.Mesh;
import h3d.mat.Texture;
#if js
@:expose
#end
class Main extends hxd.App
{
var library: hxd.fmt.hmd.Library;
var gDebugMesh: h3d.scene.Graphics;
var gDebugNav: h3d.scene.Graphics;
var interactive: h3d.scene.Interactive;
var lastPos = new h3d.col.Point(0,0,0);
var navMesh: recast.Native.NavMesh;
override function init()
{
// scene setup
new h3d.scene.fwd.DirLight(new h3d.Vector( 0.3, -0.4, -0.9), s3d);
new h3d.scene.fwd.DirLight(new h3d.Vector(1, 2, -4), s3d);
gDebugMesh = new Graphics(s3d);
gDebugNav = new Graphics(s3d);
gDebugNav.lineStyle(3,0x00FF00);
// Place our world mesh
library = hxd.Res.navtest.toHmd();
var obj = library.makeObject();
s3d.addChild(obj);
interactive = new h3d.scene.Interactive(obj.getCollider(),s3d);
interactive.onClick = function(e: hxd.Event)
{
gDebugNav.clear();
var newPos = new Point(e.relX, e.relY, e.relZ + 1 );
// Use recast to find a path from our old pos to the new one
var start = new recast.Native.Vec3(-lastPos.x, lastPos.z, lastPos.y);
var end = new recast.Native.Vec3(-newPos.x, newPos.z, newPos.y);
var path = navMesh.computePath( start, end );
start.delete();
end.delete();
if( path.getPointCount() == 0 )
{
gDebugNav.lineStyle(4,0xFF0000);
gDebugNav.drawLine(lastPos, newPos );
path.delete();
lastPos = newPos;
return;
}
gDebugNav.lineStyle(4,0x00FF00);
gDebugNav.moveTo(lastPos.x, lastPos.y, lastPos.z );
for( i in 0 ... path.getPointCount() )
{
var p = path.getPoint(i);
gDebugNav.lineTo( -p.x, p.z, p.y );
}
path.delete();
lastPos = newPos;
};
// Camera setup
s3d.camera.target.set(0, 0, 0);
s3d.camera.pos.set(120, 120, 40);
s3d.camera.zNear = 1;
s3d.camera.zFar = 100;
new h3d.scene.CameraController(s3d).loadFromCamera();
buildNavMesh();
}
function buildNavMesh()
{
// Ok yeah but...
var config = new recast.Native.RcConfig();
config.cs = 0.2;
config.ch = 0.2;
config.walkableSlopeAngle = 35;
config.walkableHeight = 1;
config.walkableClimb = 1;
config.walkableRadius = 1;
config.maxEdgeLen = 12;
config.maxSimplificationError = 1.3;
config.minRegionArea = 8;
config.mergeRegionArea = 20;
config.maxVertsPerPoly = 6;
config.detailSampleDist = 6;
config.detailSampleMaxError = 1;
navMesh = new recast.Native.NavMesh();
var data = library.header.geometries[0];
var pos = library.getBuffers(data, [new hxd.fmt.hmd.Data.GeometryFormat("position", DVec3)]);
#if js
var positions: Float32Array = new Float32Array( data.vertexCount * 3 );
var indexes: Int32Array = new Int32Array( data.indexCount );
for( i in 0 ... data.vertexCount )
{
positions[i*3 + 0] = pos.vertexes[i * 3 + 0];
positions[i*3 + 1] = pos.vertexes[i * 3 + 1];
positions[i*3 + 2] = pos.vertexes[i * 3 + 2];
}
for( i in 0 ... data.indexCount )
{
indexes[i] = pos.indexes[i];
}
// @todo: For now, the hase WebIDL lib does not appear to support directly binding arrays.
// So we're going to have to do it manually, and it's pretty gross. Apologies.
var indexCount = data.indexCount;
var positionCount = data.vertexCount;
var indexOffset = 0;
var positionOffset = 0;
js.Syntax.code("
positionOffset = recast._malloc(positionCount * 16);
recast.HEAPF32.set(positions,positionOffset / 4);
indexOffset = recast._malloc(indexCount * 4);
recast.HEAPU32.set(indexes,indexOffset / 4);");
navMesh.build(cast positionOffset, positionCount, cast indexOffset, indexCount, config);
#else
var positions: hl.BytesAccess<Single> = new hl.Bytes( data.vertexCount * 3 );
var indexes: hl.BytesAccess<Int> = new hl.Bytes( data.indexCount );
for( i in 0 ... data.vertexCount )
{
positions.set(i*3 + 0, pos.vertexes[i * 3 + 0]);
positions.set(i*3 + 1, pos.vertexes[i * 3 + 1]);
positions.set(i*3 + 2, pos.vertexes[i * 3 + 2]);
}
for( i in 0 ... data.indexCount )
{
indexes.set(i, pos.indexes[i]);
}
navMesh.build(cast positions, data.vertexCount, cast indexes, data.indexCount, config);
#end
// Draw bounds
var debugMesh = navMesh.getDebugNavMesh();
gDebugMesh.lineStyle(1, 0x002288);
for( i in 0 ... debugMesh.getTriangleCount() )
{
var triangle = debugMesh.getTriangle( i );
var p1 = triangle.getPoint(0);
var p2 = triangle.getPoint(1);
var p3 = triangle.getPoint(2);
// Re-oriente mesh for z-up
gDebugMesh.moveTo( -p1.x, p1.z, p1.y );
gDebugMesh.lineTo( -p2.x, p2.z, p2.y );
gDebugMesh.lineTo( -p3.x, p3.z, p3.y );
gDebugMesh.lineTo( -p1.x, p1.z, p1.y );
}
}
override function update(dt:Float) {
#if sys
if( Key.isPressed( Key.F5 ) )
hxd.System.exit();
#end
}
static function main() {
#if hl
hxd.Res.initLocal();
new Main();
#else
hxd.Res.initEmbed();
#end
}
}