Skip to content

Commit

Permalink
ClipPlane: fix z-fighting artifacts (#539)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
martinRenou and pre-commit-ci[bot] authored Oct 25, 2024
1 parent 1b8462c commit 9bf440d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/base/src/3dview/mainview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,25 @@ export class MainView extends React.Component<IProps, IStates> {

// Update the clipping plane whenever the transform UI move
this._transformControls.addEventListener('change', () => {
const normal = new THREE.Vector3(0, 0, 1);
let normal = new THREE.Vector3(0, 0, 1);
normal = normal.applyEuler(this._clippingPlaneMeshControl.rotation);

// This is to prevent z-fighting
// We can't use the WebGL polygonOffset because of the logarithmic depth buffer
// Long term, when using the new WebGPURenderer, we could update the formula of the
// logarithmic depth computation to emulate the polygonOffset in the shaders directly
// refLength divided by 1000 looks like it's working fine to emulate a polygonOffset for now
const translation = this._refLength ? 0.001 * this._refLength : 0;

this._clippingPlane.setFromNormalAndCoplanarPoint(
normal.applyEuler(this._clippingPlaneMeshControl.rotation),
normal,
this._clippingPlaneMeshControl.position
);
this._clippingPlane.translate(
normal.multiply(
new THREE.Vector3(translation, translation, translation)
)
);
});
this._transformControls.attach(this._clippingPlaneMeshControl);
this._scene.add(this._transformControls);
Expand Down

0 comments on commit 9bf440d

Please sign in to comment.