Hi
I am trying to convert the GradientFogPass from @RiccardoBlb JME pipeline repo to a JME Filter that I can add it to FilterPostProcessor.
I am a noob at filters and shaders. Can someone please take a glance and see if I am doing it right?
Original files:
Pipeline pass:
MatDef:
Frag Shader:
Vertex Shader:
Converted by me:
I have used the default JME fog filter as my reference.
Filter:
public class GradientFogFilter extends Filter {
private final Vector2f frustumNearFar = new Vector2f();
@Override
protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) {
/*
Camera cam = renderManager.getCurrentCamera();
frustumNearFar.set(cam.getFrustumNear(),cam.getFrustumFar());
Seems camera is null at this point!?
*/
material = new Material(manager, "MatDefs/Post/GradientFog.j3md");
Texture texture = manager.loadTexture("Textures/defaultGradient.bmp");
material.setTexture("FogGradient", texture);
material.setVector2("FrustumNearFar", frustumNearFar);
}
@Override
protected Material getMaterial() {
return material;
}
@Override
protected boolean isRequiresDepthTexture() {
return true;
}
public void setFrustumNearFar(Vector2f frustumNearFar) {
this.frustumNearFar.set(frustumNearFar);
}
public Vector2f getFrustumNearFar() {
return frustumNearFar;
}
}
MatDef:
MaterialDef GradientFog {
MaterialParameters {
Int NumSamples
Int NumSamplesDepth
Texture2D Texture
Texture2D DepthTexture
Texture2D FogGradient
Vector2 FrustumNearFar
}
Technique {
VertexShader GLSL150: Common/MatDefs/Post/Post.vert
FragmentShader GLSL150: Shaders/Post/GradientFog.frag
WorldParameters {
}
Defines {
RESOLVE_MS : NumSamples
RESOLVE_DEPTH_MS : NumSamplesDepth
}
}
}
Frag Shader:
#import "Common/ShaderLib/GLSLCompat.glsllib"
#import "Common/ShaderLib/MultiSample.glsllib"
#extension GL_ARB_explicit_attrib_location : enable
uniform COLORTEXTURE m_Texture;
uniform DEPTHTEXTURE m_DepthTexture;
varying vec2 texCoord;
uniform vec2 m_FrustumNearFar;
uniform sampler2D m_FogGradient;
float linearizeDepth(in float depth){
float f = m_FrustumNearFar.y;
float n = m_FrustumNearFar.x;
float d= depth * 2.-1.;
return (2. * n * f) / (f + n - d * (f - n));
}
float linearize01Depth(in float depth){
float d = linearizeDepth(depth);
float f = m_FrustumNearFar.y;
float n = m_FrustumNearFar.x;
return (d-n) / (f-n);
}
vec4 sampleWithFog(in sampler2D sceneTx, in sampler2D depthTx, in sampler2D gradientTx){
float depth = linearize01Depth(texture(depthTx, texCoord).r);
vec4 fogGradient = texture(gradientTx, vec2(depth, 0));
vec4 color = texture(sceneTx, texCoord);
color.rgb = mix(color.rgb, fogGradient.rgb, fogGradient.a);
return color;
}
void main(){
gl_FragColor = sampleWithFog(m_Texture, m_DepthTexture, m_FogGradient);
}
Vertex Shader:
#import "Common/ShaderLib/GLSLCompat.glsllib"
attribute vec4 inPosition;
attribute vec2 inTexCoord;
varying vec2 texCoord;
void main() {
vec2 pos = inPosition.xy * 2.0 - 1.0;
gl_Position = vec4(pos, 0.0, 1.0);
texCoord = inTexCoord;
}
I seem to be able to compile and run the shader without issue but not sure if the rendering result is correct or not. (I do not have the original repo forked to be able to run the original one to compare the results)
I am mostly considered about these lines and why there supposed to be multiple scene and depth textures instead of just one scene texture and depth texture:
Thanks in advance