Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix heightfield #13

Merged
merged 2 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions dist/cannon-es-debugger.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function cannonDebugger(scene, bodies, {
const _meshes = [];

const _material = new three.MeshBasicMaterial({
color: color != null ? color : 0x00ff00,
color: color ?? 0x00ff00,
wireframe: true
});

Expand Down Expand Up @@ -84,28 +84,21 @@ function cannonDebugger(scene, bodies, {

function createHeightfieldGeometry(shape) {
const geometry = new three.BufferGeometry();
const positions = [];
const v0 = _tempVec0;
const v1 = _tempVec1;
const v2 = _tempVec2;
const s = shape.elementSize || 1; // assumes square heightfield, else i*x, j*y

const positions = shape.data.flatMap((row, i) => row.flatMap((z, j) => [i * s, j * s, z]));
const indices = [];

for (let xi = 0; xi < shape.data.length - 1; xi++) {
for (let yi = 0; yi < shape.data[xi].length - 1; yi++) {
for (let k = 0; k < 2; k++) {
shape.getConvexTrianglePillar(xi, yi, k === 0);
v0.copy(shape.pillarConvex.vertices[0]);
v1.copy(shape.pillarConvex.vertices[1]);
v2.copy(shape.pillarConvex.vertices[2]);
v0.vadd(shape.pillarOffset, v0);
v1.vadd(shape.pillarOffset, v1);
v2.vadd(shape.pillarOffset, v2);
positions.push(v0.x, v0.y, v0.z);
positions.push(v1.x, v1.y, v1.z);
positions.push(v2.x, v2.y, v2.z);
}
const stride = shape.data[xi].length;
const index = xi * stride + yi;
indices.push(index + 1, index + stride, index + stride + 1);
indices.push(index + stride, index + 1, index);
}
}

geometry.setIndex(indices);
geometry.setAttribute('position', new three.Float32BufferAttribute(positions, 3));
geometry.computeBoundingSphere();
geometry.computeVertexNormals();
Expand Down
27 changes: 10 additions & 17 deletions dist/cannon-es-debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function cannonDebugger(scene, bodies, {
const _meshes = [];

const _material = new MeshBasicMaterial({
color: color != null ? color : 0x00ff00,
color: color ?? 0x00ff00,
wireframe: true
});

Expand Down Expand Up @@ -82,28 +82,21 @@ function cannonDebugger(scene, bodies, {

function createHeightfieldGeometry(shape) {
const geometry = new BufferGeometry();
const positions = [];
const v0 = _tempVec0;
const v1 = _tempVec1;
const v2 = _tempVec2;
const s = shape.elementSize || 1; // assumes square heightfield, else i*x, j*y

const positions = shape.data.flatMap((row, i) => row.flatMap((z, j) => [i * s, j * s, z]));
const indices = [];

for (let xi = 0; xi < shape.data.length - 1; xi++) {
for (let yi = 0; yi < shape.data[xi].length - 1; yi++) {
for (let k = 0; k < 2; k++) {
shape.getConvexTrianglePillar(xi, yi, k === 0);
v0.copy(shape.pillarConvex.vertices[0]);
v1.copy(shape.pillarConvex.vertices[1]);
v2.copy(shape.pillarConvex.vertices[2]);
v0.vadd(shape.pillarOffset, v0);
v1.vadd(shape.pillarOffset, v1);
v2.vadd(shape.pillarOffset, v2);
positions.push(v0.x, v0.y, v0.z);
positions.push(v1.x, v1.y, v1.z);
positions.push(v2.x, v2.y, v2.z);
}
const stride = shape.data[xi].length;
const index = xi * stride + yi;
indices.push(index + 1, index + stride, index + stride + 1);
indices.push(index + stride, index + 1, index);
}
}

geometry.setIndex(indices);
geometry.setAttribute('position', new Float32BufferAttribute(positions, 3));
geometry.computeBoundingSphere();
geometry.computeVertexNormals();
Expand Down
24 changes: 8 additions & 16 deletions src/cannon-es-debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,20 @@ export default function cannonDebugger(

function createHeightfieldGeometry(shape: Heightfield): BufferGeometry {
const geometry = new BufferGeometry()
const positions = []
const v0 = _tempVec0
const v1 = _tempVec1
const v2 = _tempVec2
const s = shape.elementSize || 1 // assumes square heightfield, else i*x, j*y
const positions = shape.data.flatMap((row, i) => row.flatMap((z, j) => [i * s, j * s, z]))
const indices = []

for (let xi = 0; xi < shape.data.length - 1; xi++) {
for (let yi = 0; yi < shape.data[xi].length - 1; yi++) {
for (let k = 0; k < 2; k++) {
shape.getConvexTrianglePillar(xi, yi, k === 0)
v0.copy(shape.pillarConvex.vertices[0])
v1.copy(shape.pillarConvex.vertices[1])
v2.copy(shape.pillarConvex.vertices[2])
v0.vadd(shape.pillarOffset, v0)
v1.vadd(shape.pillarOffset, v1)
v2.vadd(shape.pillarOffset, v2)
positions.push(v0.x, v0.y, v0.z)
positions.push(v1.x, v1.y, v1.z)
positions.push(v2.x, v2.y, v2.z)
}
const stride = shape.data[xi].length
const index = xi * stride + yi
indices.push(index + 1, index + stride, index + stride + 1)
indices.push(index + stride, index + 1, index)
}
}

geometry.setIndex(indices)
geometry.setAttribute('position', new Float32BufferAttribute(positions, 3))
geometry.computeBoundingSphere()
geometry.computeVertexNormals()
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"outDir": "dist",
"resolveJsonModule": true,
"noImplicitThis": false,
"lib": [
"es2019",
"dom"
],
"baseUrl": "./src"
},
"include": ["./src"],
Expand Down