Skip to content

Commit b4e4250

Browse files
authored
Add threshold option to TileFlattening (#1084)
1 parent bbbc036 commit b4e4250

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

src/plugins/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,10 +767,10 @@ Returns whether the given object has been passed in as a shape.
767767
### addShape
768768

769769
```js
770-
addShape( shape: Object3D, direction: Vector3 ): void
770+
addShape( shape: Object3D, direction: Vector3 = ( 0, - 1, 0 ), threshold: number = Infinity ): void
771771
```
772772

773-
Adds the given object as a shape to flatten to in addition to the direction to flatten.
773+
Adds the given object as a shape to flatten to in addition to the direction to flatten. The `threshold` field is the distance threshold under which vertices will be flattened. `Infinity` will always flatten while `0` will never flatten.
774774

775775
### updateShape
776776

src/plugins/three/TileFlatteningPlugin.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Mesh } from 'three';
33
export class TileFatteningPlugin {
44

55
hasShape( mesh: Mesh ): boolean;
6-
addShape( mesh: Mesh, direction: Vector3 ): void;
6+
addShape( mesh: Mesh, direction: Vector3, threshold: number ): void;
77
updateShape( mesh: Mesh ): void;
88
deleteShape( mesh ): boolean;
99
clearShapes(): void;

src/plugins/three/TileFlatteningPlugin.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const _matrix = /* @__PURE__ */ new Matrix4();
1212
const _invMatrix = /* @__PURE__ */ new Matrix4();
1313
const _raycaster = /* @__PURE__ */ new Raycaster();
1414
const _doubleSidedMaterial = /* @__PURE__ */ new MeshBasicMaterial( { side: DoubleSide } );
15+
const RAYCAST_DISTANCE = 1e5;
1516

1617
function calculateSphere( object, target ) {
1718

@@ -143,6 +144,7 @@ export class TileFlatteningPlugin {
143144
shape,
144145
direction,
145146
sphere,
147+
threshold,
146148
} ) => {
147149

148150
// TODO: if we save the sphere of the original mesh we can check the height to limit the tiles checked
@@ -189,11 +191,11 @@ export class TileFlatteningPlugin {
189191
ray.origin
190192
.fromBufferAttribute( position, i )
191193
.applyMatrix4( _matrix )
192-
.addScaledVector( direction, 1e5 );
193-
_raycaster.far = 1e5;
194+
.addScaledVector( direction, RAYCAST_DISTANCE );
195+
_raycaster.far = RAYCAST_DISTANCE;
194196

195197
const hit = _raycaster.intersectObject( shape )[ 0 ];
196-
if ( hit ) {
198+
if ( hit && RAYCAST_DISTANCE - hit.distance < threshold ) {
197199

198200
hit.point.applyMatrix4( _invMatrix );
199201
position.setXYZ( i, ...hit.point );
@@ -226,7 +228,7 @@ export class TileFlatteningPlugin {
226228

227229
}
228230

229-
addShape( mesh, direction = new Vector3( 0, - 1, 0 ) ) {
231+
addShape( mesh, direction = new Vector3( 0, - 1, 0 ), threshold = Infinity ) {
230232

231233
if ( this.hasShape( mesh ) ) {
232234

@@ -255,6 +257,7 @@ export class TileFlatteningPlugin {
255257
shape: shape,
256258
direction: direction.clone(),
257259
sphere: sphere,
260+
threshold: threshold,
258261
} );
259262

260263
}

0 commit comments

Comments
 (0)