forked from webaverse/physics-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objectize.cc
400 lines (359 loc) · 17 KB
/
objectize.cc
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
#include <emscripten.h>
// #include "geometry.h"
// #include "compose.h"
// #include "noise.h"
// #include "march.h"
// #include "occlusionCull/occlusionCull.h"
// #include "DualContouring/main.h"
// #include "AnimationSystem/AnimationSystem.h"
// #include "collide.h"
#include "physics.h"
// #include "convex.h"
// #include "earcut.h"
// #include <iostream>
// #include "cut.h"
#include <deque>
#include <map>
extern "C" {
// memory
EMSCRIPTEN_KEEPALIVE void *doMalloc(size_t size) {
return malloc(size);
}
EMSCRIPTEN_KEEPALIVE void doFree(void *ptr) {
free(ptr);
}
//
EMSCRIPTEN_KEEPALIVE void initialize() {
physicsBase = new PBase();
}
//
EMSCRIPTEN_KEEPALIVE PScene *makePhysics() {
return new PScene();
}
//
EMSCRIPTEN_KEEPALIVE unsigned int simulatePhysics(PScene *scene, unsigned int *ids, float *positions, float *quaternions, float *scales, unsigned int *bitfields, unsigned int numIds, float elapsedTime, float *velocities) {
return scene->simulate(ids, positions, quaternions, scales, bitfields, numIds, elapsedTime, velocities);
}
EMSCRIPTEN_KEEPALIVE float setTriggerPhysics(PScene *scene, unsigned int id) {
return scene->setTrigger(id);
}
EMSCRIPTEN_KEEPALIVE unsigned int getTriggerEventsPhysics(PScene *scene, unsigned int *scratchStack) {
return scene->getTriggerEvents(scratchStack);
}
EMSCRIPTEN_KEEPALIVE void raycastPhysics(PScene *scene, float *origin, float *direction, float maxDist, unsigned int *hit, float *position, float *normal, float *distance, unsigned int *objectId, unsigned int *faceIndex) {
scene->raycast(origin, direction, maxDist, *hit, position, normal, *distance, *objectId, *faceIndex);
}
EMSCRIPTEN_KEEPALIVE void raycastPhysicsArray(unsigned int rayCount, PScene *scene, float *origin, float *direction, float maxDist, unsigned int *hit, float *position, float *normal, float *distance, unsigned int *objectId, unsigned int *faceIndex) {
for (unsigned int i = 0; i < rayCount; i++) {
scene->raycast(origin, direction, maxDist, *hit, position, normal, *distance, *objectId, *faceIndex);
origin += 3;
direction += 3;
hit += 1;
position += 3;
normal += 3;
distance += 1;
objectId += 1;
faceIndex += 1;
}
}
EMSCRIPTEN_KEEPALIVE void sweepBox(
PScene *scene,
float *origin,
float *quaternion,
float *halfExtents,
float *direction,
float sweepDistance,
unsigned int maxHits,
unsigned int *numHits,
float *position,
float *normal,
float *distance,
unsigned int *objectId,
unsigned int *faceIndex
) {
scene->sweepBox(
origin,
quaternion,
halfExtents,
direction,
sweepDistance,
maxHits,
*numHits,
position,
normal,
distance,
objectId,
faceIndex
);
}
EMSCRIPTEN_KEEPALIVE void sweepConvexShape(
PScene *scene,
PxConvexMesh *convexMesh,
float *origin,
float *quaternion,
float *direction,
float sweepDistance,
unsigned int maxHits,
unsigned int *numHits,
float *position,
float *normal,
float *distance,
unsigned int *objectId,
unsigned int *faceIndex
) {
scene->sweepConvexShape(
convexMesh,
origin,
quaternion,
direction,
sweepDistance,
maxHits,
*numHits,
position,
normal,
distance,
objectId,
faceIndex
);
}
/* EMSCRIPTEN_KEEPALIVE float *getPathPhysics(PScene *scene, float *_start, float *_dest, bool _isWalk, float _hy, float _heightTolerance, unsigned int _maxIterdetect, unsigned int _maxIterStep, unsigned int _numIgnorePhysicsIds, unsigned int *_ignorePhysicsIds) {
return scene->getPath(_start, _dest, _isWalk, _hy, _heightTolerance, _maxIterdetect, _maxIterStep, _numIgnorePhysicsIds, _ignorePhysicsIds);
} */
EMSCRIPTEN_KEEPALIVE float *overlapBoxPhysics(PScene *scene, float hx, float hy, float hz, float *position, float *quaternion) {
return scene->overlapBox(hx, hy, hz, position, quaternion);
}
EMSCRIPTEN_KEEPALIVE float *overlapCapsulePhysics(PScene *scene, float radius, float halfHeight, float *position, float *quaternion) {
return scene->overlapCapsule(radius, halfHeight, position, quaternion);
}
EMSCRIPTEN_KEEPALIVE void collideBoxPhysics(PScene *scene, float hx, float hy, float hz, float *position, float *quaternion, unsigned int maxIter, unsigned int *hit, float *direction, unsigned int *grounded, unsigned int *id) {
scene->collideBox(hx, hy, hz, position, quaternion, maxIter, *hit, direction, *grounded, *id);
}
EMSCRIPTEN_KEEPALIVE void collideCapsulePhysics(PScene *scene, float radius, float halfHeight, float *position, float *quaternion, unsigned int maxIter, unsigned int *hit, float *direction, unsigned int *grounded, unsigned int *id) {
scene->collideCapsule(radius, halfHeight, position, quaternion, maxIter, *hit, direction, *grounded, *id);
}
EMSCRIPTEN_KEEPALIVE void getCollisionObjectPhysics(PScene *scene, float radius, float halfHeight, float *position, float *quaternion, float *direction, unsigned int *hit, unsigned int *id) {
scene->getCollisionObject(radius, halfHeight, position, quaternion, direction, *hit, *id);
}
EMSCRIPTEN_KEEPALIVE void addCapsuleGeometryPhysics(PScene *scene, float *position, float *quaternion, float radius, float halfHeight, unsigned int id, PxMaterial *material, unsigned int dynamic, unsigned int flags) {
scene->addCapsuleGeometry(position, quaternion, radius, halfHeight, id, material, dynamic, flags);
}
EMSCRIPTEN_KEEPALIVE void addPlaneGeometryPhysics(PScene *scene, float *position, float *quaternion, unsigned int id, PxMaterial *material, unsigned int dynamic) {
scene->addPlaneGeometry(position, quaternion, id, material, dynamic);
}
EMSCRIPTEN_KEEPALIVE void addBoxGeometryPhysics(PScene *scene, float *position, float *quaternion, float *size, unsigned int id, PxMaterial *material, unsigned int dynamic, int groupId) {
scene->addBoxGeometry(position, quaternion, size, id, material, dynamic, groupId);
}
//
EMSCRIPTEN_KEEPALIVE void cookGeometryPhysics(float *positions, unsigned int *indices, unsigned int numPositions, unsigned int numIndices, uint8_t **data, unsigned int *length, PxDefaultMemoryOutputStream **writeStream) {
physicsBase->cookGeometry(positions, indices, numPositions, numIndices, data, length, writeStream);
}
EMSCRIPTEN_KEEPALIVE void cookConvexGeometryPhysics(float *positions, unsigned int *indices, unsigned int numPositions, unsigned int numIndices, uint8_t **data, unsigned int *length, PxDefaultMemoryOutputStream **writeStream) {
physicsBase->cookConvexGeometry(positions, indices, numPositions, numIndices, data, length, writeStream);
}
EMSCRIPTEN_KEEPALIVE void cookHeightFieldGeometryPhysics(unsigned int numRows, unsigned int numColumns, int16_t *scratchStack, uint8_t **data, unsigned int *length, PxDefaultMemoryOutputStream **writeStream) {
physicsBase->cookHeightFieldGeometry(numRows, numColumns, scratchStack, data, length, writeStream);
}
//
EMSCRIPTEN_KEEPALIVE PxTriangleMesh *createShapePhysics(PScene *scene, uint8_t *data, unsigned int length, PxDefaultMemoryOutputStream *releaseWriteStream) {
return scene->createShape(data, length, releaseWriteStream);
}
EMSCRIPTEN_KEEPALIVE void destroyShapePhysics(PScene *scene, PxTriangleMesh *triangleMesh) {
scene->destroyShape(triangleMesh);
}
EMSCRIPTEN_KEEPALIVE PxConvexMesh *createConvexShapePhysics(PScene *scene, uint8_t *data, unsigned int length, PxDefaultMemoryOutputStream *releaseWriteStream) {
return scene->createConvexShape(data, length, releaseWriteStream);
}
EMSCRIPTEN_KEEPALIVE void destroyConvexShapePhysics(PScene *scene, PxConvexMesh *convexMesh) {
scene->destroyConvexShape(convexMesh);
}
EMSCRIPTEN_KEEPALIVE PxHeightField *createHeightFieldPhysics(PScene *scene, uint8_t *data, unsigned int length, PxDefaultMemoryOutputStream *releaseWriteStream) {
return scene->createHeightField(data, length, releaseWriteStream);
}
EMSCRIPTEN_KEEPALIVE PxMaterial *createMaterialPhysics(PScene *scene, float *mat) {
return scene->createMaterial(mat);
}
EMSCRIPTEN_KEEPALIVE void destroyMaterialPhysics(PScene *scene, PxMaterial *material) {
scene->destroyMaterial(material);
}
EMSCRIPTEN_KEEPALIVE void addGeometryPhysics(PScene *scene, PxTriangleMesh *triangleMesh, float *position, float *quaternion, float *scale, unsigned int id, PxMaterial *material, unsigned int external, PxTriangleMesh *releaseTriangleMesh) {
scene->addGeometry(triangleMesh, position, quaternion, scale, id, material, external, releaseTriangleMesh);
}
EMSCRIPTEN_KEEPALIVE void addConvexGeometryPhysics(PScene *scene, PxConvexMesh *convexMesh, float *position, float *quaternion, float *scale, unsigned int id, PxMaterial *material, unsigned int dynamic, unsigned int external, PxConvexMesh *releaseConvexMesh) {
scene->addConvexGeometry(convexMesh, position, quaternion, scale, id, material, dynamic, external, releaseConvexMesh);
}
EMSCRIPTEN_KEEPALIVE void addHeightFieldGeometryPhysics(PScene *scene, PxHeightField *convexMesh, float *position, float *quaternion, float *scale, float heightScale, float rowScale, float columnScale, unsigned int id, PxMaterial *material, unsigned int dynamic, unsigned int external, PxHeightField *releaseHeightField) {
scene->addHeightFieldGeometry(convexMesh, position, quaternion, scale, heightScale, rowScale, columnScale, id, material, dynamic, external, releaseHeightField);
}
EMSCRIPTEN_KEEPALIVE void setGeometryScalePhysics(PScene *scene, unsigned int id, float *scale, PxDefaultMemoryOutputStream *writeStream) {
scene->setGeometryScale(id, scale, writeStream);
}
EMSCRIPTEN_KEEPALIVE bool getGeometryPhysics(PScene *scene, unsigned int id, float *positions, unsigned int *numPositions, unsigned int *indices, unsigned int *numIndices, float *bounds) {
return scene->getGeometry(id, positions, *numPositions, indices, *numIndices, bounds);
}
EMSCRIPTEN_KEEPALIVE bool getBoundsPhysics(PScene *scene, unsigned int id, float *bounds) {
return scene->getBounds(id, bounds);
}
EMSCRIPTEN_KEEPALIVE void enableActorPhysics(PScene *scene, unsigned int id) {
scene->enableActor(id);
}
EMSCRIPTEN_KEEPALIVE void disableActorPhysics(PScene *scene, unsigned int id) {
scene->disableActor(id);
}
EMSCRIPTEN_KEEPALIVE void disableGeometryPhysics(PScene *scene, unsigned int id) {
scene->disableGeometry(id);
}
EMSCRIPTEN_KEEPALIVE void enableGeometryQueriesPhysics(PScene *scene, unsigned int id) {
scene->enableGeometryQueries(id);
}
EMSCRIPTEN_KEEPALIVE void disableGeometryQueriesPhysics(PScene *scene, unsigned int id) {
scene->disableGeometryQueries(id);
}
EMSCRIPTEN_KEEPALIVE void enableGeometryPhysics(PScene *scene, unsigned int id) {
scene->enableGeometry(id);
}
EMSCRIPTEN_KEEPALIVE void setMassAndInertiaPhysics(PScene *scene, unsigned int id, float mass, float *inertia) {
scene->setMassAndInertia(id, mass, inertia);
}
EMSCRIPTEN_KEEPALIVE void setGravityEnabledPhysics(PScene *scene, unsigned int id, bool enabled) {
scene->setGravityEnabled(id, enabled);
}
EMSCRIPTEN_KEEPALIVE void removeGeometryPhysics(PScene *scene, unsigned int id) {
scene->removeGeometry(id);
}
EMSCRIPTEN_KEEPALIVE void setTransformPhysics(PScene *scene, unsigned int id, float *position, float *quaternion, float *scale, bool autoWake) {
scene->setTransform(id, position, quaternion, scale, autoWake);
}
EMSCRIPTEN_KEEPALIVE void getGlobalPositionPhysics(PScene *scene, unsigned int id, float *position) {
scene->getGlobalPosition(id, position);
}
EMSCRIPTEN_KEEPALIVE void getLinearVelocityPhysics(PScene *scene, unsigned int id, float *velocity) {
scene->getLinearVelocity(id, velocity);
}
EMSCRIPTEN_KEEPALIVE void getAngularVelocityPhysics(PScene *scene, unsigned int id, float *velocity) {
scene->getAngularVelocity(id, velocity);
}
EMSCRIPTEN_KEEPALIVE void addForceAtPosPhysics(PScene *scene, unsigned int id, float *velocity, float *position, bool autoWake) {
scene->addForceAtPos(id, velocity, position, autoWake);
}
EMSCRIPTEN_KEEPALIVE void addForceAtLocalPosPhysics(PScene *scene, unsigned int id, float *velocity, float *position, bool autoWake) {
scene->addForceAtLocalPos(id, velocity, position, autoWake);
}
EMSCRIPTEN_KEEPALIVE void addLocalForceAtPosPhysics(PScene *scene, unsigned int id, float *velocity, float *position, bool autoWake) {
scene->addLocalForceAtPos(id, velocity, position, autoWake);
}
EMSCRIPTEN_KEEPALIVE void addLocalForceAtLocalPosPhysics(PScene *scene, unsigned int id, float *velocity, float *position, bool autoWake) {
scene->addLocalForceAtLocalPos(id, velocity, position, autoWake);
}
EMSCRIPTEN_KEEPALIVE void addForcePhysics(PScene *scene, unsigned int id, float *velocity, bool autoWake) {
scene->addForce(id, velocity, autoWake);
}
EMSCRIPTEN_KEEPALIVE void addTorquePhysics(PScene *scene, unsigned int id, float *velocity, bool autoWake) {
scene->addTorque(id, velocity, autoWake);
}
EMSCRIPTEN_KEEPALIVE void setVelocityPhysics(PScene *scene, unsigned int id, float *velocity, bool autoWake) {
scene->setVelocity(id, velocity, autoWake);
}
EMSCRIPTEN_KEEPALIVE void setAngularVelocityPhysics(PScene *scene, unsigned int id, float *velocity, bool autoWake) {
scene->setAngularVel(id, velocity, autoWake);
}
EMSCRIPTEN_KEEPALIVE void setLinearLockFlagsPhysics(PScene *scene, unsigned int id, bool x, bool y, bool z) {
scene->setLinearLockFlags(id, x, y, z);
}
EMSCRIPTEN_KEEPALIVE void setAngularLockFlagsPhysics(PScene *scene, unsigned int id, bool x, bool y, bool z) {
scene->setAngularLockFlags(id, x, y, z);
}
EMSCRIPTEN_KEEPALIVE PxController *createCharacterControllerPhysics(PScene *scene, float radius, float height, float contactOffset, float stepOffset, float *position, PxMaterial *material, unsigned int id) {
return scene->createCharacterController(radius, height, contactOffset, stepOffset, position, material, id);
}
EMSCRIPTEN_KEEPALIVE void destroyCharacterControllerPhysics(PScene *scene, PxController *characterController) {
scene->destroyCharacterController(characterController);
}
EMSCRIPTEN_KEEPALIVE unsigned int moveCharacterControllerPhysics(PScene *scene, PxController *characterController, float *displacement, float minDist, float elapsedTime, float *positionOut) {
return scene->moveCharacterController(characterController, displacement, minDist, elapsedTime, positionOut);
}
EMSCRIPTEN_KEEPALIVE void setCharacterControllerPositionPhysics(PScene *scene, PxController *characterController, float *position) {
return scene->setCharacterControllerPosition(characterController, position);
}
/* EMSCRIPTEN_KEEPALIVE float *doCut(
float *positions,
unsigned int numPositions,
float *normals,
unsigned int numNormals,
float *uvs,
unsigned int numUvs,
unsigned int *faces,
unsigned int numFaces,
float *planeNormal,
float planeDistance
) {
return cut(
positions,
numPositions,
normals,
numNormals,
uvs,
numUvs,
faces,
numFaces,
planeNormal,
planeDistance
);
} */
// EMSCRIPTEN_KEEPALIVE void doMarchingingCubes(
// int dims[3],
// float *potential,
// uint8_t *brush,
// float shift[3],
// float scale[3],
// float *positions,
// float *colors,
// unsigned int *faces,
// unsigned int *positionIndex,
// unsigned int *colorIndex,
// unsigned int *faceIndex)
// {
// marchingCubes(dims, potential, brush, shift, scale, positions, colors, faces, *positionIndex, *colorIndex, *faceIndex);
// }
/* EMSCRIPTEN_KEEPALIVE uint8_t *doMarchingCubes(int dims[3], float *potential, float shift[3], float scale[3]) {
return marchingCubes(dims, potential, shift, scale);
}
EMSCRIPTEN_KEEPALIVE OcclusionCulling *initOcclusionCulling()
{
return Culling::init();
}
EMSCRIPTEN_KEEPALIVE uint8_t *cullOcclusionCulling(OcclusionCulling *inst,
uint8_t *chunksBuffer,
int id,
int minX, int minY, int minZ,
int maxX, int maxY, int maxZ,
float cameraX, float cameraY, float cameraZ,
float cameraViewX, float cameraViewY, float cameraViewZ,
int numDraws)
{
return Culling::cull(inst,
chunksBuffer, id,
ivec3{minX, minY, minZ},
ivec3{maxX, maxY, maxZ},
vec3{cameraX, cameraY, cameraZ},
vec3{cameraViewX, cameraViewY, cameraViewZ},
numDraws);
} */
/* EMSCRIPTEN_KEEPALIVE void generateChunkDataDualContouring(float x, float y, float z){
return DualContouring::generateChunkData(x, y, z);
}
EMSCRIPTEN_KEEPALIVE void setChunkLodDualContouring(float x, float y, float z, int lod){
return DualContouring::setChunkLod(x, y, z, lod);
}
EMSCRIPTEN_KEEPALIVE void clearTemporaryChunkDataDualContouring(){
return DualContouring::clearTemporaryChunkData();
}
EMSCRIPTEN_KEEPALIVE void clearChunkRootDualContouring(float x, float y, float z){
return DualContouring::clearChunkRoot(x, y, z);
}
EMSCRIPTEN_KEEPALIVE uint8_t *createChunkMeshDualContouring(float x, float y, float z){
return DualContouring::createChunkMesh(x, y, z);
} */
/* EMSCRIPTEN_KEEPALIVE bool drawDamage(float x, float y, float z, float radius, float value) {
return DualContouring::drawDamage(x, y, z, radius, value);
} */
} // extern "C"