Skip to content

Commit

Permalink
Fixed distance calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ffrank913 committed Jan 16, 2025
1 parent aa54319 commit 03b9f1b
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/renderer/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
WebGLRenderTarget,
WebGLRenderer,
} from 'three';
import DIVEPerspectiveCamera from '../camera/PerspectiveCamera';
import {
BokehShader,
BokehDepthShader,
Expand All @@ -26,6 +25,7 @@ import {
PRODUCT_LAYER_MASK,
HELPER_LAYER_MASK,
} from '../constant/VisibilityLayerMask';
import DIVEPerspectiveCamera from '../camera/PerspectiveCamera';
import { DIVEScene } from '../scene/Scene';

type DIVEDOFSettings = {
Expand Down Expand Up @@ -395,6 +395,15 @@ export class DIVERenderer extends WebGLRenderer {
return (-zfar * znear) / (depth * (zfar - znear) - zfar);
}

private smoothstep(near: number, far: number, depth: number): number {
const x = this.saturate((depth - near) / (far - near));
return x * x * (3 - 2 * x);
}

private saturate(x: number): number {
return Math.max(0, Math.min(1, x));
}

private internal_dof_render(
scene: DIVEScene,
cam: DIVEPerspectiveCamera,
Expand All @@ -413,16 +422,10 @@ export class DIVERenderer extends WebGLRenderer {
const targetDistance =
intersects.length > 0 ? intersects[0].distance : 1000;

const sdistance = MathUtils.smoothstep(
cam.near,
cam.far,
targetDistance,
);
const sdistance = this.smoothstep(cam.near, cam.far, targetDistance);

const ldistance = this.linearize(cam, 1 - sdistance);

console.log(sdistance, ldistance);

dof.bokeh_uniforms['focalDepth'].value = ldistance;

this.clear();
Expand Down

0 comments on commit 03b9f1b

Please sign in to comment.