Shader debugging: scaling the world

In the linearizeDepth function in area_effect.frag, I was multiplying by 2, but the scaling of the world was off. The number I found that works best is e - 0.99, and I have no idea why.
This commit is contained in:
JozsefA 2021-04-15 12:46:54 -07:00
parent 7988fb69a7
commit f12d9452b4
3 changed files with 44 additions and 16 deletions

View file

@ -100,8 +100,8 @@ public class EffectsHandler {
program.bindDepthTexture(mainBuffer.getDepthAttachment());
GameRenderer gameRenderer = Minecraft.getInstance().gameRenderer;
Matrix4f projection = gameRenderer.getBasicProjectionMatrix(gameRenderer.getActiveRenderInfo(), AnimationTickHolder.getPartialTicks(), true);
//Matrix4f projection = Backend.projectionMatrix.copy();
//Matrix4f projection = gameRenderer.getBasicProjectionMatrix(gameRenderer.getActiveRenderInfo(), AnimationTickHolder.getPartialTicks(), true);
Matrix4f projection = Backend.projectionMatrix.copy();
//projection.a23 = projection.a32 = 0;
projection.a33 = 1;
projection.invert();
@ -117,6 +117,9 @@ public class EffectsHandler {
// Vector3d pos3 = new Vector3d(906, 84, -207);
Vector3d cameraPos = gameRenderer.getActiveRenderInfo().getProjectedView();
program.setTestParam((float) (Math.E - 0.99));
program.setCameraPos(cameraPos.inverse());
for (int i = 0; i < 16; i++) {
double angle = (Math.PI * AnimationTickHolder.getRenderTime() / 40) + i * Math.PI / 4;

View file

@ -39,6 +39,9 @@ public class SphereFilterProgram extends GlProgram {
protected final int uNearPlane;
protected final int uFarPlane;
protected final int uCameraPos;
protected final int testParam;
// protected final int uSphereCenter;
// protected final int uSphereRadius;
// protected final int uSphereFeather;
@ -62,6 +65,8 @@ public class SphereFilterProgram extends GlProgram {
uInverseView = getUniformLocation("uInverseView");
uNearPlane = getUniformLocation("uNearPlane");
uFarPlane = getUniformLocation("uFarPlane");
uCameraPos = getUniformLocation("uCameraPos");
testParam = getUniformLocation("testParam");
//
// uSphereCenter = getUniformLocation("uSphereCenter");
// uSphereRadius = getUniformLocation("uSphereRadius");
@ -82,6 +87,14 @@ public class SphereFilterProgram extends GlProgram {
GL20.glUniform1f(uFarPlane, farPlane);
}
public void setTestParam(float farPlane) {
GL20.glUniform1f(testParam, farPlane);
}
public void setCameraPos(Vector3d pos) {
GL20.glUniform3f(uCameraPos, (float) pos.x, (float) pos.y, (float) pos.z);
}
public void clear() {
filters.clear();
}

View file

@ -12,6 +12,12 @@ uniform sampler2D uDepth;
uniform sampler2D uColor;
uniform float uNearPlane = 0.15;
uniform float uFarPlane = 1.;
uniform vec3 uCameraPos;
uniform float testParam = 2.0;
uniform mat4 uInverseProjection;
uniform mat4 uInverseView;
struct SphereFilter {
vec4 sphere;// <vec3 position, float radius>
@ -26,8 +32,9 @@ layout (std140) uniform Filters {
};
float linearizeDepth(float d, float zNear, float zFar) {
float z_n = 2.0 * d - 1.0;
return 2.0 * zNear * zFar / (zFar + zNear - z_n * (zFar - zNear));
float clipZ = 2.0 * d - 1.0;
float linearized = zNear * zFar / (zFar + zNear - clipZ * (zFar - zNear));
return testParam * linearized;
}
vec4 filterColor(mat4 colorOp, vec4 frag) {
@ -41,7 +48,7 @@ float getDepth() {
float depth = texture2D(uDepth, ScreenCoord).r;
depth = linearizeDepth(depth, uNearPlane, uFarPlane);
//depth = ( - uNearPlane) / (uFarPlane - uNearPlane);
//depth = (depth - uNearPlane) / (uFarPlane - uNearPlane);
//depth = depth / uFarPlane;
return depth;
@ -49,19 +56,24 @@ float getDepth() {
void main() {
float depth = getDepth();
vec3 worldPos = WorldDir * depth;
vec3 worldPos = WorldDir * depth - uCameraPos;
vec4 accum = texture2D(uColor, ScreenCoord);
vec4 diffuse = texture2D(uColor, ScreenCoord);
//
// for (int i = 0; i < uCount; i++) {
// SphereFilter s = uSpheres[i];
//
// float distance = distance(s.sphere.xyz, worldPos);
// float strength = 1 - smoothstep(s.sphere.w - s.feather, s.sphere.w + s.feather, distance);
//
// accum = mix(accum, filterColor(s.colorOp, accum), strength);
// }
//
// Color = accum;
for (int i = 0; i < uCount; i++) {
SphereFilter s = uSpheres[i];
vec3 fractionalCoords = fract(worldPos);
float distance = distance(s.sphere.xyz, worldPos);
float strength = 1 - smoothstep(s.sphere.w - s.feather, s.sphere.w + s.feather, distance);
vec3 isBonudary = step(15./16., fractionalCoords);
accum = mix(accum, filterColor(s.colorOp, accum), strength);
}
Color = accum;
//Color = vec4(vec3(distance / uFarPlane), 1.);
Color = vec4(mix(diffuse.rgb, fractionalCoords, isBonudary), 1.);
}