Skip to content

Commit

Permalink
feat(shader): add logarithmic depth (#346)
Browse files Browse the repository at this point in the history
* feat(shader): add logarithmic depth
  • Loading branch information
Codeboy-cn authored Dec 8, 2023
1 parent b4edb46 commit 24afa9d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
78 changes: 78 additions & 0 deletions samples/render/Sample_HighPrecision.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Engine3D, Scene3D, AtmosphericComponent, View3D, CameraUtil, HoverCameraController, Object3D, MeshRenderer, SphereGeometry, UnLitMaterial, BoxGeometry, SkyRenderer, Color } from "@orillusion/core";
import { GUIHelp } from "@orillusion/debug/GUIHelp";
import { GUIUtil } from "@samples/utils/GUIUtil";

export class Sample_HighPrecision {

async run() {
Engine3D.setting.render.useLogDepth = true;

await Engine3D.init();

let scene = new Scene3D();

GUIHelp.init();

let camera = CameraUtil.createCamera3DObject(scene);
camera.perspective(60, Engine3D.aspect, 1.0, 6000 * 10000.0);
camera.object3D.z = 3;

let cameraController = camera.object3D.addComponent(HoverCameraController);
cameraController.setCamera(20, -45, 1500 * 10000.0);
cameraController.minDistance = 601 * 10000.0;
cameraController.maxDistance = 6000 * 10000.0;

this.initScene(scene);

let view = new View3D();
view.scene = scene;
view.camera = camera;
Engine3D.startRenderView(view);
}

async initScene(scene: Scene3D) {
{
let obj = new Object3D();
obj.rotationX = -90;
let mr = obj.addComponent(MeshRenderer);
mr.geometry = new SphereGeometry(600 * 10000.0, 128, 128);
let mat = new UnLitMaterial();
mat.baseMap = await Engine3D.res.loadTexture('textures/earth/8k_earth_daymap.jpg');
mr.material = mat;
scene.addChild(obj);
}

{
let obj = new Object3D();
obj.x = 600 * 10000.0;
let mr = obj.addComponent(MeshRenderer);
mr.geometry = new BoxGeometry(600 * 10000.0, 100 * 10000.0, 100 * 10000.0);
let mat = new UnLitMaterial();
mat.baseColor = new Color(1, 0, 0)
mr.material = mat;
scene.addChild(obj);
}

{
let obj = new Object3D();
obj.y = 600 * 10000.0;
let mr = obj.addComponent(MeshRenderer);
mr.geometry = new BoxGeometry(100 * 10000.0, 600 * 10000.0, 100 * 10000.0);
let mat = new UnLitMaterial();
mat.baseColor = new Color(0, 1, 0)
mr.material = mat;
scene.addChild(obj);
}

{
let obj = new Object3D();
obj.z = 600 * 10000.0;
let mr = obj.addComponent(MeshRenderer);
mr.geometry = new BoxGeometry(100 * 10000.0, 100 * 10000.0, 600 * 10000.0);
let mat = new UnLitMaterial();
mat.baseColor = new Color(0, 0, 1)
mr.material = mat;
scene.addChild(obj);
}
}
}
2 changes: 1 addition & 1 deletion src/assets/shader/core/base/Common_frag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export let Common_frag: string = /*wgsl*/ `
#if USE_OUTDEPTH
#if USE_LOGDEPTH
ORI_FragmentOutput.out_depth = log2Depth(ORI_VertexVarying.fragCoord.z,globalUniform.near,globalUniform.far) ;
ORI_FragmentOutput.out_depth = log2DepthFixPersp(ORI_VertexVarying.fragPosition.w, globalUniform.near, globalUniform.far);
#else
ORI_FragmentOutput.out_depth = ORI_ShadingInput.FragDepth ;
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/assets/shader/core/struct/VertexAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ export let VertexAttributes: string = /*wgsl*/ `
var viewPosition = ORI_MATRIX_V * worldPos;
var clipPosition = ORI_MATRIX_P * viewPosition ;
#if USE_LOGDEPTH
clipPosition.z = log2Depth(clipPosition.w, globalUniform.near, globalUniform.far);
#endif
ORI_CameraWorldDir = normalize(ORI_CAMERAMATRIX[3].xyz - worldPos.xyz) ;
ORI_VertexOut.index = f32(vertex.index) ;
Expand Down
16 changes: 12 additions & 4 deletions src/assets/shader/math/MathShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,20 @@ fn dir_to_faceId(pt:vec3<f32>) -> i32 {
return log2(depth + 1.0) * 2.0 / (log(far + 1.0) / 0.6931471805599453) * 0.5;
}
fn log2Depth(depth : f32, near:f32, far:f32) -> f32 {
let pt = pow((far / near),depth);
return near * pt / (far / near);
fn log2Depth(depth:f32, near:f32, far:f32) -> f32 {
let Fcoef:f32 = 2.0 / log2(far + 1.0);
var result:f32 = (log2(max(1e-6, 1.0 + depth)) * Fcoef - 1.0);
result = (1.0 + result) / 2.0;
return result * depth;
}
fn log2DepthFixPersp(depth:f32, near:f32, far:f32) -> f32 {
let flogz:f32 = 1.0 + depth;
let Fcoef_half:f32 = (2.0 / log2(far + 1.0)) * 0.5;
var result:f32 = log2(flogz) * Fcoef_half;
result = (1.0 + result) / 2.0;
return result;
}
fn QuaternionToMatrix(q: vec4<f32>) -> mat4x4<f32> {
Expand Down

0 comments on commit 24afa9d

Please sign in to comment.