forked from LeenkxTeam/LNXSDK
Converted rendering engine to reverse-z
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
#ifndef _GBUFFER_GLSL_
|
||||
#define _GBUFFER_GLSL_
|
||||
#include "std/math.glsl"
|
||||
|
||||
vec2 octahedronWrap(const vec2 v) {
|
||||
return (1.0 - abs(v.yx)) * (vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0));
|
||||
@ -13,20 +14,17 @@ vec3 getNor(const vec2 enc) {
|
||||
return n;
|
||||
}
|
||||
|
||||
vec3 getPosView(const vec3 viewRay, const float depth, const vec2 cameraProj) {
|
||||
float linearDepth = cameraProj.y / (cameraProj.x - depth);
|
||||
return viewRay * linearDepth;
|
||||
vec3 getPosView(const vec3 viewRay, float depth, vec2 cameraProj) {
|
||||
return viewRay * linearize(depth, cameraProj);
|
||||
}
|
||||
|
||||
vec3 getPos(const vec3 eye, const vec3 eyeLook, const vec3 viewRay, const float depth, const vec2 cameraProj) {
|
||||
float linearDepth = cameraProj.y / ((depth * 0.5 + 0.5) - cameraProj.x);
|
||||
float viewZDist = dot(eyeLook, viewRay);
|
||||
vec3 wposition = eye + viewRay * (linearDepth / viewZDist);
|
||||
return wposition;
|
||||
vec3 getPos(const vec3 eye, mat3 invV, const vec3 viewRay, float depth, vec2 cameraProj) {
|
||||
vec3 pVS = viewRay * linearize(depth, cameraProj);
|
||||
return eye + invV * pVS; // invV == inverse of view-rotation
|
||||
}
|
||||
|
||||
vec3 getPosNoEye(const vec3 eyeLook, const vec3 viewRay, const float depth, const vec2 cameraProj) {
|
||||
float linearDepth = cameraProj.y / ((depth * 0.5 + 0.5) - cameraProj.x);
|
||||
float linearDepth = linearize(depth, cameraProj);
|
||||
float viewZDist = dot(eyeLook, viewRay);
|
||||
vec3 wposition = viewRay * (linearDepth / viewZDist);
|
||||
return wposition;
|
||||
@ -44,28 +42,31 @@ vec3 getPos2(const mat4 invVP, const float depth, const vec2 coord) {
|
||||
return pos.xyz;
|
||||
}
|
||||
|
||||
#if defined(HLSL) || defined(METAL)
|
||||
vec3 getPosView2(const mat4 invP, const float depth, vec2 coord) {
|
||||
coord.y = 1.0 - coord.y;
|
||||
#else
|
||||
vec3 getPosView2(const mat4 invP, const float depth, const vec2 coord) {
|
||||
#endif
|
||||
//#if defined(HLSL) || defined(METAL)
|
||||
//vec3 getPosView2(const mat4 invP, const float depth, vec2 coord) {
|
||||
// coord.y = 1.0 - coord.y;
|
||||
//#else
|
||||
vec3 getPosView2(mat4 invP, float depth, vec2 coord) {
|
||||
vec4 clip = vec4(coord * 2.0 - 1.0, depth, 1.0);
|
||||
vec4 view = invP * clip;
|
||||
return view.xyz / view.w;
|
||||
//#endif
|
||||
vec4 pos = vec4(coord * 2.0 - 1.0, depth, 1.0);
|
||||
pos = invP * pos;
|
||||
pos.xyz /= pos.w;
|
||||
return pos.xyz;
|
||||
}
|
||||
|
||||
#if defined(HLSL) || defined(METAL)
|
||||
vec3 getPos2NoEye(const vec3 eye, const mat4 invVP, const float depth, vec2 coord) {
|
||||
coord.y = 1.0 - coord.y;
|
||||
#else
|
||||
// Reconstruct view-space position from inverse View×Proj
|
||||
vec3 getPos2NoEye(const vec3 eye, const mat4 invVP, const float depth, const vec2 coord) {
|
||||
vec2 uv = coord;
|
||||
#if defined(HLSL) || defined(METAL)
|
||||
uv.y = 1.0 - uv.y;
|
||||
#endif
|
||||
vec4 pos = vec4(coord * 2.0 - 1.0, depth, 1.0);
|
||||
pos = invVP * pos;
|
||||
pos.xyz /= pos.w;
|
||||
return pos.xyz - eye;
|
||||
vec4 clip = vec4(uv * 2.0 - 1.0, depth, 1.0);
|
||||
vec4 world = invVP * clip;
|
||||
world.xyz /= world.w;
|
||||
return world.xyz - eye;
|
||||
}
|
||||
|
||||
// Updated to support separate roughness/metalness storage
|
||||
|
@ -34,8 +34,8 @@ vec2 rand2(const vec2 coord) {
|
||||
}
|
||||
|
||||
float linearize(float depth, vec2 cameraProj) {
|
||||
depth = depth * 2.0 - 1.0;
|
||||
return cameraProj.y / (cameraProj.x - depth);
|
||||
return cameraProj.y / (cameraProj.x - max(depth, 1e-6));
|
||||
//return cameraProj.y / (cameraProj.x - depth);
|
||||
}
|
||||
|
||||
float attenuate(const float dist) {
|
||||
|
Reference in New Issue
Block a user