-
Notifications
You must be signed in to change notification settings - Fork 28
/
helloworld.html
182 lines (157 loc) · 8.26 KB
/
helloworld.html
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
<!DOCTYPE html>
<head>
<title>PhysX Test</title>
<style>
html, body, canvas {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/3.4.2/gl-matrix-min.js" integrity="sha512-eV9ExyTa3b+YHr99IBTYpwk4wbgDMDlfW8uTxhywO8dWb810fGUSKDgHhEv1fAqmJT4jyYnt1iWWMW4FRxeQOQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="physx-js-webidl.js"></script>
<script>
PhysX().then(function(PhysX) {
var version = PhysX.PHYSICS_VERSION;
console.log('PhysX loaded! Version: ' + ((version >> 24) & 0xff) + '.' + ((version >> 16) & 0xff) + '.' + ((version >> 8) & 0xff));
var allocator = new PhysX.PxDefaultAllocator();
var errorCb = new PhysX.PxDefaultErrorCallback();
var foundation = PhysX.CreateFoundation(version, allocator, errorCb);
console.log('Created PxFoundation');
var tolerances = new PhysX.PxTolerancesScale();
var physics = PhysX.CreatePhysics(version, foundation, tolerances);
console.log('Created PxPhysics');
// create scene
var tmpVec = new PhysX.PxVec3(0, -9.81, 0);
var sceneDesc = new PhysX.PxSceneDesc(tolerances);
sceneDesc.set_gravity(tmpVec);
sceneDesc.set_cpuDispatcher(PhysX.DefaultCpuDispatcherCreate(0));
sceneDesc.set_filterShader(PhysX.DefaultFilterShader());
var scene = physics.createScene(sceneDesc);
console.log('Created scene');
// create a default material
var material = physics.createMaterial(0.5, 0.5, 0.5);
// create default simulation shape flags
var shapeFlags = new PhysX.PxShapeFlags(PhysX.PxShapeFlagEnum.eSCENE_QUERY_SHAPE | PhysX.PxShapeFlagEnum.eSIMULATION_SHAPE | PhysX.PxShapeFlagEnum.eVISUALIZATION);
// create a few temporary objects used during setup
var tmpPose = new PhysX.PxTransform(PhysX.PxIDENTITYEnum.PxIdentity);
var tmpFilterData = new PhysX.PxFilterData(1, 1, 0, 0);
// create a large static box with size 20x1x20 as ground
var groundGeometry = new PhysX.PxBoxGeometry(10, 0.5, 10); // PxBoxGeometry uses half-sizes
var groundShape = physics.createShape(groundGeometry, material, true, shapeFlags);
var ground = physics.createRigidStatic(tmpPose);
groundShape.setSimulationFilterData(tmpFilterData);
ground.attachShape(groundShape);
scene.addActor(ground);
// create a few dynamic boxes with size 1x1x1, which will fall on the ground
var boxGeometry = new PhysX.PxBoxGeometry(0.5, 0.5, 0.5); // PxBoxGeometry uses half-sizes
var lastBox = null;
for (var y = 0; y < 10; y++) {
tmpVec.set_x(0); tmpVec.set_y(y*2 + 5); tmpVec.set_z(0);
tmpPose.set_p(tmpVec);
var boxShape = physics.createShape(boxGeometry, material, true, shapeFlags);
var box = physics.createRigidDynamic(tmpPose);
boxShape.setSimulationFilterData(tmpFilterData);
box.attachShape(boxShape);
scene.addActor(box);
lastBox = box;
}
// clean up temp objects
PhysX.destroy(groundGeometry);
PhysX.destroy(boxGeometry);
PhysX.destroy(tmpFilterData);
PhysX.destroy(tmpPose);
PhysX.destroy(tmpVec);
PhysX.destroy(shapeFlags);
PhysX.destroy(sceneDesc);
PhysX.destroy(tolerances);
console.log('Created scene objects');
// setup debug drawing stuff
const { mat4, vec4, vec3 } = glMatrix;
const viewMatrix = mat4.create();
const projectionMatrix = mat4.create();
const viewProjectionMatrix = mat4.create();
const tmpVec4 = vec4.create();
const canvas = document.querySelector('canvas');
const context = canvas.getContext('2d');
setupDebugDrawer(PhysX, scene);
// simulate forever!
simulationLoop();
function setupDebugDrawer() {
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
// compute projection matrix
mat4.lookAt(viewMatrix, [12, 15, 20], [0, 0, 0], [0, 1, 0])
mat4.perspective(projectionMatrix, 45 * (Math.PI / 180), canvas.width / canvas.height, 0.01, 75);
mat4.multiply(viewProjectionMatrix, projectionMatrix, viewMatrix);
// setup debug drawer
const context = canvas.getContext('2d');
scene.setVisualizationParameter(PhysX.eSCALE, 1);
scene.setVisualizationParameter(PhysX.eWORLD_AXES, 1);
scene.setVisualizationParameter(PhysX.eACTOR_AXES, 1);
scene.setVisualizationParameter(PhysX.eCOLLISION_SHAPES, 1);
}
function simulationLoop() {
let lastFrame = 0;
requestAnimationFrame(function loop(hrTime) {
var timeStep = Math.min(0.03, (hrTime - lastFrame) / 1000);
scene.simulate(timeStep);
scene.fetchResults(true);
// use debug drawer interface to draw boxes on a canvas.
// in a real world application you would query the box poses and update your graphics boxes accordingly
debugDraw(scene);
var lastBoxPos = lastBox.getGlobalPose().get_p();
console.log('Last box position: ' + lastBoxPos.get_x() + ", " + lastBoxPos.get_y() + ", " + lastBoxPos.get_z());
lastFrame = hrTime;
requestAnimationFrame(loop);
});
}
function project(x, y, z) {
const result = vec4.transformMat4(tmpVec4, [x, y, z, 1], viewProjectionMatrix);
const clipX = (result[0] / result[3]);
const clipY = (result[1] / result[3]);
return [(canvas.width / 2) * (1 + clipX), (canvas.height / 2) * (1 - clipY)];
}
function drawLine(from, to, color) {
const [r, g, b] = color;
context.beginPath();
context.strokeStyle = `rgb(${255 * r}, ${255 * g}, ${255 * b})`;
context.moveTo(...from);
context.lineTo(...to);
context.stroke();
}
function debugDraw() {
canvas.width = canvas.width; // clears the canvas
const rb = scene.getRenderBuffer();
for(let i = 0; i < rb.getNbLines(); i++) {
const line = PhysX.NativeArrayHelpers.prototype.getDebugLineAt(rb.getLines(), i);
const from = project(line.pos0.get_x(), line.pos0.get_y(), line.pos0.get_z());
const to = project(line.pos1.get_x(), line.pos1.get_y(), line.pos1.get_z());
drawLine(from, to, colors[line.get_color0()]);
}
}
const colors = {
[PhysX.PxDebugColorEnum.eARGB_BLACK]: [ 0, 0, 0],
[PhysX.PxDebugColorEnum.eARGB_RED]: [ 1, 0, 0],
[PhysX.PxDebugColorEnum.eARGB_GREEN]: [ 0, 1, 0],
[PhysX.PxDebugColorEnum.eARGB_BLUE]: [ 0, 0, 1],
[PhysX.PxDebugColorEnum.eARGB_YELLOW]: [ 1, 1, 0],
[PhysX.PxDebugColorEnum.eARGB_MAGENTA]: [ 1, 0, 1],
[PhysX.PxDebugColorEnum.eARGB_CYAN]: [ 0, 1, 1],
[PhysX.PxDebugColorEnum.eARGB_WHITE]: [ 1, 1, 1],
[PhysX.PxDebugColorEnum.eARGB_GREY]: [0.5, 0.5, 0.5],
[PhysX.PxDebugColorEnum.eARGB_DARKRED]: [0.5, 0, 0],
[PhysX.PxDebugColorEnum.eARGB_DARKGREEN]: [ 0, 0.5, 0],
[PhysX.PxDebugColorEnum.eARGB_DARKBLUE]: [ 0, 0, 0.5],
};
});
</script>
</body>
</html>