started working with shadow maps and I wanted soft shadows and got into blurring stuff which lead to this. Please look at and make suggestions thanks. I can't find anything to put into clean up. Haven't actually tested it either. Stole from ShadowRenderPass and ATI RenderMonkey IDE Depth of Field. Renders the scene 4 times first time to "sharp image" ,2nd time to blur "shape image",3rd time to blur the blur, and 4th time to combine shape and blur(the 2nd one). It suppose to work on pinhole and all the varibles are preset for testing purpose. In RenderMonkey after the first render it used a screen alligned quad and just put the texture from previous pass on the quad. How much of a proformance boost would that give me? I imagine that render 40verts then 4 ,4 ,4 would be better than 40 verts four times.
import java.net.URL;
import com.jme.image.Texture;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.renderer.TextureRenderer;
import com.jme.renderer.pass.*;
import com.jme.scene.Spatial;
import com.jme.scene.state.GLSLShaderObjectsState;
import com.jme.scene.state.RenderState;
import com.jme.system.DisplaySystem;
public class depth_of_field_pass extends Pass {
private DisplaySystem display;
private static float focus =(float) 0.48,
Ks = (float) 0.94,
sampleDist0 = (float) 0.0198,
distanceScale = (float) 0.0035,
range = (float) 4.6,
sampleDist1 = (float) 0.192,
Kd =(float) 0.96;
private Renderer renderer;
//blur scene 1
private GLSLShaderObjectsState blur0Shader;
private Texture blur0Tex;
//blur scene 2
private GLSLShaderObjectsState blur1Shader;
private Texture blur1Tex;
//Origanal scene
private GLSLShaderObjectsState RTShader;
private Texture RTTex;
private TextureRenderer TR;
private RenderState[] preStates;
private GLSLShaderObjectsState combineShader;
private static final long serialVersionUID = 1L;
protected void doRender(Renderer arg0) {
init();
setupShaders();
saveEnforcedStates();
Spatial.clearCurrentStates();
Spatial.enforceState(RTShader);
TR.setupTexture(RTTex);
TR.updateCamera();
renderScene(renderer);
Spatial.clearCurrentStates();
Spatial.enforceState(blur0Shader);
TR.setupTexture(blur0Tex);
TR.updateCamera();
renderScene(renderer);
Spatial.clearCurrentStates();
Spatial.enforceState(blur1Shader);
TR.setupTexture(blur1Tex);
TR.updateCamera();
renderScene(renderer);
TR.cleanup();
Spatial.clearCurrentStates();
Spatial.enforceState(combineShader);
renderScene(renderer);
replaceEnforcedStates();
}
protected void renderScene(Renderer r) {
for (int i = 0, sSize = spatials.size(); i < sSize; i++) {
Spatial s = (Spatial)spatials.get(i);
s.onDraw(r);
}
r.renderQueue();
}
protected void replaceEnforcedStates() {
for (int x = RenderState.RS_MAX_STATE; --x >= 0; ) {
Spatial.enforcedStateList[x] = preStates[x];
}
}
protected void saveEnforcedStates() {
for (int x = RenderState.RS_MAX_STATE; --x >= 0; ) {
preStates[x] = Spatial.enforcedStateList[x];
}
}
protected void init()
{
display = DisplaySystem.getDisplaySystem();
renderer = display.getRenderer();
RTShader = renderer.createGLSLShaderObjectsState();
TR = display.createTextureRenderer(512,512,false,true,false,false,TextureRenderer.RENDER_TEXTURE_2D,0);
TR.setBackgroundColor(ColorRGBA.black);
TR.getCamera().setLocation(renderer.getCamera().getLocation());
RTTex = new Texture();
RTTex.setWrap(Texture.WM_CLAMP_S_CLAMP_T);
blur0Tex = new Texture();
blur0Tex.setWrap(Texture.WM_CLAMP_S_CLAMP_T);
blur0Shader = renderer.createGLSLShaderObjectsState();
blur1Tex = new Texture();
blur1Tex.setWrap(Texture.WM_CLAMP_S_CLAMP_T);
blur1Shader = renderer.createGLSLShaderObjectsState();
combineShader = renderer.createGLSLShaderObjectsState();
}
protected void cleanup()
{
}
protected void setupShaders()
{
URL RTVert,RTFrag,blur0Vert,blur0Frag,blur1Vert,blur1Frag,combineVert,combineFrag;
try{
RTVert = new URL("Render.vert");
RTFrag = new URL("Render.frag");
blur0Vert = new URL("Blur0.vert");
blur0Frag = new URL("Blur0.frag");
blur1Vert = new URL("Blur1.vert");
blur1Frag = new URL("Blur1.frag");
combineVert = new URL("Combine.vert");
combineFrag = new URL("Combine.frag");
}
catch(Exception e)
{
System.out.println(e.printStackTrace());
}
RTShader.load(RTVert,RTFrag);
RTShader.setUniform("distanceScale",distanceScale);
blur0Shader.load(blur0Vert,blur0Frag);
blur0Shader.setUniform("RT",RTTex.getTextureId());
blur0Shader.setUniform("sampleDist0",sampleDist0);
blur1Shader.load(blur1Vert,blur1Frag);
blur1Shader.setUniform("RT",blur0Tex.getTextureId());
blur1Shader.setUniform("sampleDist1",sampleDist1);
combineShader.load(combineVert,combineFrag);
combineShader.setUniform("Range",range);
combineShader.setUniform("RT",RTTex.getTextureId());
combineShader.setUniform("blur1",blur1Tex.getTextureId());
combineShader.setUniform("focus",focus);
}
}
Render.vert shader code
uniform float distanceScale;
varying vec3 vNormal;
varying vec3 vViewVec;
void main(void)
{
gl_Position = ftransform();
// Eye-space lighting
vNormal = gl_NormalMatrix * gl_Normal;
// We multiply with distance scale in the vertex shader
// instead of the fragment shader to improve performance.
vViewVec = -vec3(distanceScale * gl_ModelViewMatrix * gl_Vertex);
}
Render Frag Shader code
uniform vec4 lightDir;
uniform float Ks;
uniform float Kd;
varying vec3 vNormal;
varying vec3 vViewVec;
void main(void)
{
// Basic lighting
float diffuse = dot(lightDir.xyz, vNormal);
float specular = pow(clamp(dot(reflect(-normalize(vViewVec), vNormal), lightDir.xyz),0.0, 1.0), 16.0);
vec3 light = Kd * diffuse + Ks * specular;
// We'll use the distance to decide how much blur we want
float dist = length(vViewVec);
gl_FragColor = vec4( light, dist);
}
Blur0 Vert Shader Code
varying vec2 vTexCoord;
void main(void)
{
// Clean up inaccuracies
vec2 Pos = sign(gl_Vertex.xy);
gl_Position = vec4(Pos.xy, 0, 1);
// Image-space
vTexCoord.x = 0.5 * (1.0 + Pos.x);
vTexCoord.y = 0.5 * (1.0 + Pos.y);
}
Blur0 Frag Shader Code
uniform float sampleDist0;
uniform sampler2D RT;
varying vec2 vTexCoord;
/*
const vec2 samples[12] = {
-0.326212, -0.405805,
-0.840144, -0.073580,
-0.695914, 0.457137,
-0.203345, 0.620716,
0.962340, -0.194983,
0.473434, -0.480026,
0.519456, 0.767022,
0.185461, -0.893124,
0.507431, 0.064425,
0.896420, 0.412458,
-0.321940, -0.932615,
-0.791559, -0.597705,
};
*/
void main(void)
{
vec2 samples00 = vec2(-0.326212, -0.405805);
vec2 samples01 = vec2(-0.840144, -0.073580);
vec2 samples02 = vec2(-0.695914, 0.457137);
vec2 samples03 = vec2(-0.203345, 0.620716);
vec2 samples04 = vec2( 0.962340, -0.194983);
vec2 samples05 = vec2( 0.473434, -0.480026);
vec2 samples06 = vec2( 0.519456, 0.767022);
vec2 samples07 = vec2( 0.185461, -0.893124);
vec2 samples08 = vec2( 0.507431, 0.064425);
vec2 samples09 = vec2( 0.896420, 0.412458);
vec2 samples10 = vec2(-0.321940, -0.932615);
vec2 samples11 = vec2(-0.791559, -0.597705);
vec2 newCoord;
vec4 sum = texture2D(RT, vTexCoord);
/*
for (int i = 0; i < 12; i++)
{
sum += tex2D(RT, texCoord + sampleDist0 * samples[i]);
}
*/
newCoord = vTexCoord + sampleDist0 * samples00;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist0 * samples01;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist0 * samples02;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist0 * samples03;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist0 * samples04;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist0 * samples05;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist0 * samples06;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist0 * samples07;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist0 * samples08;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist0 * samples09;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist0 * samples10;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist0 * samples11;
sum += texture2D(RT, newCoord);
sum /= 13.0;
gl_FragColor = vec4( sum);
}
Blur1 Vert Shader Code
varying vec2 vTexCoord;
void main(void)
{
// Clean up inaccuracies
vec2 Pos = sign(gl_Vertex.xy);
gl_Position = vec4(Pos.xy, 0, 1);
// Image-space
vTexCoord.x = 0.5 * (1.0 + Pos.x);
vTexCoord.y = 0.5 * (1.0 + Pos.y);
}
Blur1 Frag Shader Code
uniform float sampleDist1;
uniform sampler2D RT;
varying vec2 vTexCoord;
/*
const vec2 samples[12] = {
-0.326212, -0.405805,
-0.840144, -0.073580,
-0.695914, 0.457137,
-0.203345, 0.620716,
0.962340, -0.194983,
0.473434, -0.480026,
0.519456, 0.767022,
0.185461, -0.893124,
0.507431, 0.064425,
0.896420, 0.412458,
-0.321940, -0.932615,
-0.791559, -0.597705,
};
*/
void main(void)
{
vec2 samples00 = vec2(-0.326212, -0.405805);
vec2 samples01 = vec2(-0.840144, -0.073580);
vec2 samples02 = vec2(-0.695914, 0.457137);
vec2 samples03 = vec2(-0.203345, 0.620716);
vec2 samples04 = vec2( 0.962340, -0.194983);
vec2 samples05 = vec2( 0.473434, -0.480026);
vec2 samples06 = vec2( 0.519456, 0.767022);
vec2 samples07 = vec2( 0.185461, -0.893124);
vec2 samples08 = vec2( 0.507431, 0.064425);
vec2 samples09 = vec2( 0.896420, 0.412458);
vec2 samples10 = vec2(-0.321940, -0.932615);
vec2 samples11 = vec2(-0.791559, -0.597705);
vec2 newCoord;
vec4 sum = texture2D(RT, vTexCoord);
/*
for (int i = 0; i < 12; i++)
{
sum += tex2D(RT, texCoord + sampleDist1 * samples[i]);
}
*/
newCoord = vTexCoord + sampleDist1 * samples00;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist1 * samples01;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist1 * samples02;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist1 * samples03;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist1 * samples04;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist1 * samples05;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist1 * samples06;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist1 * samples07;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist1 * samples08;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist1 * samples09;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist1 * samples10;
sum += texture2D(RT, newCoord);
newCoord = vTexCoord + sampleDist1 * samples11;
sum += texture2D(RT, newCoord);
sum /= 13.0;
gl_FragColor = vec4( sum);
}
Combine Vert Shader Code
varying vec2 vTexCoord;
void main(void)
{
// Clean up inaccuracies
vec2 Pos = sign(gl_Vertex.xy);
gl_Position = vec4(Pos.xy, 0, 1);
// Image-space
vTexCoord.x = 0.5 * (1.0 + Pos.x);
vTexCoord.y = 0.5 * (1.0 + Pos.y);
}
Combine Frag Shader Code
uniform float range;
uniform sampler2D RT;
uniform sampler2D Blur1;
uniform float focus;
varying vec2 vTexCoord;
void main(void)
{
vec4 sharp = texture2D(RT, vTexCoord);
vec4 blur = texture2D(Blur1, vTexCoord);
gl_FragColor = mix(sharp, blur, clamp(range * abs(focus - sharp.a), 0.0, 1.0));
}