Skip to content

Commit 9bf440d

Browse files
ClipPlane: fix z-fighting artifacts (#539)
* ClipPlane: fix z-fighting artifacts * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 1b8462c commit 9bf440d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

packages/base/src/3dview/mainview.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,25 @@ export class MainView extends React.Component<IProps, IStates> {
359359

360360
// Update the clipping plane whenever the transform UI move
361361
this._transformControls.addEventListener('change', () => {
362-
const normal = new THREE.Vector3(0, 0, 1);
362+
let normal = new THREE.Vector3(0, 0, 1);
363+
normal = normal.applyEuler(this._clippingPlaneMeshControl.rotation);
364+
365+
// This is to prevent z-fighting
366+
// We can't use the WebGL polygonOffset because of the logarithmic depth buffer
367+
// Long term, when using the new WebGPURenderer, we could update the formula of the
368+
// logarithmic depth computation to emulate the polygonOffset in the shaders directly
369+
// refLength divided by 1000 looks like it's working fine to emulate a polygonOffset for now
370+
const translation = this._refLength ? 0.001 * this._refLength : 0;
363371

364372
this._clippingPlane.setFromNormalAndCoplanarPoint(
365-
normal.applyEuler(this._clippingPlaneMeshControl.rotation),
373+
normal,
366374
this._clippingPlaneMeshControl.position
367375
);
376+
this._clippingPlane.translate(
377+
normal.multiply(
378+
new THREE.Vector3(translation, translation, translation)
379+
)
380+
);
368381
});
369382
this._transformControls.attach(this._clippingPlaneMeshControl);
370383
this._scene.add(this._transformControls);

0 commit comments

Comments
 (0)