Skip to content

Commit 753e9dd

Browse files
authored
Merge pull request #153 from dli7319/depthmesh
DepthMesh: Add ignore edge pixels option.
2 parents 2969e12 + f79c3d3 commit 753e9dd

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/depth/DepthMesh.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,29 @@ export class DepthMesh extends MeshScript {
5555
private depthTextures?: DepthTextures
5656
) {
5757
const options = depthOptions.depthMesh;
58-
const geometry = new THREE.PlaneGeometry(1, 1, 159, 159);
58+
const depthResolution = options.depthFullResolution;
59+
const ignoreEdgePixels = options.ignoreEdgePixels;
60+
const activeRes = Math.max(2, depthResolution - 2 * ignoreEdgePixels);
61+
const geometry = new THREE.PlaneGeometry(
62+
1,
63+
1,
64+
activeRes - 1,
65+
activeRes - 1
66+
);
67+
68+
const minU = ignoreEdgePixels / (depthResolution - 1);
69+
const maxU =
70+
(depthResolution - 1 - ignoreEdgePixels) / (depthResolution - 1);
71+
const minV = ignoreEdgePixels / (depthResolution - 1);
72+
const maxV =
73+
(depthResolution - 1 - ignoreEdgePixels) / (depthResolution - 1);
74+
75+
const uvs = geometry.attributes.uv.array;
76+
for (let i = 0; i < uvs.length; i += 2) {
77+
uvs[i] = minU + uvs[i] * (maxU - minU);
78+
uvs[i + 1] = minV + uvs[i + 1] * (maxV - minV);
79+
}
80+
5981
let material: THREE.Material;
6082
let uniforms;
6183
if (options.useDepthTexture || options.showDebugTexture) {
@@ -101,6 +123,11 @@ export class DepthMesh extends MeshScript {
101123
// Create a downsampled geometry for raycasts and physics.
102124
if (options.useDownsampledGeometry) {
103125
this.downsampledGeometry = new THREE.PlaneGeometry(1, 1, 39, 39);
126+
const dsUvs = this.downsampledGeometry.attributes.uv.array;
127+
for (let i = 0; i < dsUvs.length; i += 2) {
128+
dsUvs[i] = minU + dsUvs[i] * (maxU - minU);
129+
dsUvs[i + 1] = minV + dsUvs[i + 1] * (maxV - minV);
130+
}
104131
this.downsampledMesh = new THREE.Mesh(this.downsampledGeometry, material);
105132
this.downsampledMesh.visible = false;
106133
this.add(this.downsampledMesh);
@@ -290,8 +317,8 @@ export class DepthMesh extends MeshScript {
290317
const v = geometry.attributes.uv.array[2 * i + 1];
291318

292319
// Grabs the nearest for now.
293-
const depthX = Math.round(clamp(u * width, 0, width - 1));
294-
const depthY = Math.round(clamp((1.0 - v) * height, 0, height - 1));
320+
const depthX = Math.round(clamp(u * (width - 1), 0, width - 1));
321+
const depthY = Math.round(clamp((1.0 - v) * (height - 1), 0, height - 1));
295322
const rawDepth = depthArray[depthY * width + depthX];
296323
let depth = depthData.rawValueToMeters * rawDepth;
297324

src/depth/DepthOptions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export class DepthMeshOptions {
1818
// Whether to always update the full resolution geometry.
1919
updateFullResolutionGeometry = false;
2020
colliderUpdateFps = 5;
21+
depthFullResolution = 160;
22+
ignoreEdgePixels = 3;
2123
}
2224

2325
export class DepthOptions {

0 commit comments

Comments
 (0)