-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Added SoftBloomFilter #2229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Added SoftBloomFilter #2229
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6ed6466
added pbr bloom filter
codex128 902acd2
add javadoc and license
codex128 8d262b1
documented and tweaked test
codex128 e604840
added exception
codex128 c5014f1
various formatting fixes
codex128 4b31f76
fixed javadoc typo
codex128 66dba26
fixed bug on applying glow factor
codex128 60491c5
fix javadoc typo
codex128 f563bb3
fixed formatting issues
codex128 b0f1cdc
switched texture min/mag filters
codex128 b91d9c6
rename filter
codex128 7cf0ea9
rename filter
codex128 eb2efbf
improved test and capped number of passes
codex128 23cf554
reformat test
codex128 ab8121f
serialize bilinear filtering
codex128 2b45ec0
delete unrelated files
codex128 5ad4d7c
increase size limit to 2
codex128 11559c1
renamed shaders
codex128 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
262 changes: 262 additions & 0 deletions
262
jme3-effects/src/main/java/com/jme3/post/filters/PBRBloomFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,262 @@ | ||
| /* | ||
| * Copyright (c) 2024 jMonkeyEngine | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * * Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.jme3.post.filters; | ||
|
|
||
| import com.jme3.asset.AssetManager; | ||
| import com.jme3.material.Material; | ||
| import com.jme3.math.FastMath; | ||
| import com.jme3.math.Vector2f; | ||
| import com.jme3.post.Filter; | ||
| import com.jme3.renderer.RenderManager; | ||
| import com.jme3.renderer.Renderer; | ||
| import com.jme3.renderer.ViewPort; | ||
| import com.jme3.texture.Image; | ||
| import java.util.LinkedList; | ||
|
|
||
| /** | ||
| * Adds a glow effect to the scene. | ||
| * <p> | ||
| * Compared to {@link BloomFilter}, this filter produces much higher quality | ||
| * results that feel much more natural. | ||
| * <p> | ||
| * This implementation, unlike BloomFilter, has no brightness threshold, | ||
| * meaning all aspects of the scene glow, although only very bright areas will | ||
| * noticably produce glow. For this reason, this filter should <em>only</em> be used | ||
| * if HDR is also being utilized, otherwise BloomFilter should be preferred. | ||
| * <p> | ||
| * This filter uses the PBR bloom algorithm presented in | ||
| * <a href="https://learnopengl.com/Guest-Articles/2022/Phys.-Based-Bloom">this article</a>. | ||
| * | ||
| * @author codex | ||
| */ | ||
| public class PBRBloomFilter extends Filter { | ||
|
|
||
| private AssetManager assetManager; | ||
| private RenderManager renderManager; | ||
| private ViewPort viewPort; | ||
| private int width, height; | ||
codex128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private Pass[] downsamplingPasses; | ||
codex128 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private Pass[] upsamplingPasses; | ||
| private final Image.Format format = Image.Format.RGBA16F; | ||
| private boolean initialized = false; | ||
| private int numSamplingPasses = 5; | ||
| private float glowFactor = 0.05f; | ||
|
|
||
| /** | ||
| * Creates filter with default settings. | ||
| */ | ||
| public PBRBloomFilter() {} | ||
codex128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Creates filter with the given number of sampling passes. | ||
| * | ||
| * @param numSamplingPasses | ||
| * @see #setNumSamplingPasses(int) | ||
| */ | ||
| public PBRBloomFilter(int numSamplingPasses) { | ||
| this.numSamplingPasses = numSamplingPasses; | ||
codex128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Creates filter with given glow factor. | ||
| * | ||
| * @param glowFactor | ||
| * @see #setGlowFactor(float) | ||
| */ | ||
| public PBRBloomFilter(float glowFactor) { | ||
codex128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.glowFactor = glowFactor; | ||
| } | ||
|
|
||
| /** | ||
| * Creates filter with the given number of sampling passes and the given glow factor. | ||
| * | ||
| * @param numSamplingPasses | ||
| * @param glowFactor | ||
| * @see #setNumSamplingPasses(int) | ||
| * @see #setGlowFactor(float) | ||
| */ | ||
| public PBRBloomFilter(int numSamplingPasses, float glowFactor) { | ||
codex128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.numSamplingPasses = numSamplingPasses; | ||
| this.glowFactor = glowFactor; | ||
| } | ||
|
|
||
| @Override | ||
| protected void initFilter(AssetManager am, RenderManager rm, ViewPort vp, int width, int height) { | ||
codex128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| assetManager = am; | ||
| renderManager = rm; | ||
| viewPort = vp; | ||
| postRenderPasses = new LinkedList<>(); | ||
| Renderer renderer = renderManager.getRenderer(); | ||
| int w = this.width = width; | ||
| int h = this.height = height; | ||
|
|
||
| downsamplingPasses = new Pass[numSamplingPasses]; | ||
| upsamplingPasses = new Pass[numSamplingPasses]; | ||
|
|
||
| // downsampling passes | ||
| Material downsampleMat = new Material(assetManager, "Common/MatDefs/Post/Downsample.j3md"); | ||
| Vector2f initTexelSize = new Vector2f(1f/w, 1f/h); | ||
| w /= 2; h /= 2; | ||
| Pass initialPass = new Pass() { | ||
| @Override | ||
| public boolean requiresSceneAsTexture() { | ||
| return true; | ||
| } | ||
| @Override | ||
| public void beforeRender() { | ||
| downsampleMat.setVector2("TexelSize", initTexelSize); | ||
| } | ||
| }; | ||
| initialPass.init(renderer, w, h, format, Image.Format.Depth, 1, downsampleMat); | ||
| postRenderPasses.add(initialPass); | ||
| downsamplingPasses[0] = initialPass; | ||
| for (int i = 1; i < downsamplingPasses.length; i++) { | ||
| Vector2f texelSize = new Vector2f(1f/w, 1f/h); | ||
| w /= 2; h /= 2; | ||
| Pass prev = downsamplingPasses[i-1]; | ||
| Pass pass = new Pass() { | ||
| @Override | ||
| public void beforeRender() { | ||
| downsampleMat.setTexture("Texture", prev.getRenderedTexture()); | ||
| downsampleMat.setVector2("TexelSize", texelSize); | ||
| } | ||
| }; | ||
| pass.init(renderer, w, h, format, Image.Format.Depth, 1, downsampleMat); | ||
| postRenderPasses.add(pass); | ||
| downsamplingPasses[i] = pass; | ||
| } | ||
|
|
||
| // upsampling passes | ||
| Material upsampleMat = new Material(assetManager, "Common/MatDefs/Post/Upsample.j3md"); | ||
| for (int i = 0; i < upsamplingPasses.length; i++) { | ||
| Vector2f texelSize = new Vector2f(1f/w, 1f/h); | ||
| w *= 2; h *= 2; | ||
| Pass prev; | ||
| if (i == 0) { | ||
| prev = downsamplingPasses[downsamplingPasses.length-1]; | ||
| } else { | ||
| prev = upsamplingPasses[i-1]; | ||
| } | ||
| Pass pass = new Pass() { | ||
| @Override | ||
| public void beforeRender() { | ||
| upsampleMat.setTexture("Texture", prev.getRenderedTexture()); | ||
| upsampleMat.setVector2("TexelSize", texelSize); | ||
| } | ||
| }; | ||
| pass.init(renderer, w, h, format, Image.Format.Depth, 1, upsampleMat); | ||
| postRenderPasses.add(pass); | ||
| upsamplingPasses[i] = pass; | ||
| } | ||
|
|
||
| material = new Material(assetManager, "Common/MatDefs/Post/PBRBloomFinal.j3md"); | ||
| material.setTexture("GlowMap", upsamplingPasses[upsamplingPasses.length-1].getRenderedTexture()); | ||
|
|
||
| initialized = true; | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| protected Material getMaterial() { | ||
| return material; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the number of sampling passes in each step. | ||
| * <p> | ||
| * Higher values produce more glow with higher resolution, at the cost | ||
| * of more passes. Lower values produce less glow with lower resolution. | ||
| * <p> | ||
| * The total number of passes is {@code 2n+1}: n passes for downsampling | ||
| * (13 texture reads per pass per fragment), n passes for upsampling and blur | ||
| * (9 texture reads per pass per fragment), and 1 pass for blending (2 texture reads | ||
| * per fragment). Though, it should be noted that for each downsampling pass the | ||
| * number of fragments decreases by 75%, and for each upsampling pass, the number | ||
| * of fragments quadruples (which restores the number of fragments to the original | ||
| * resolution). | ||
| * <p> | ||
| * Settings this after the filter has been initialized forces reinitialization. | ||
| * <p> | ||
| * default=5 | ||
| * | ||
| * @param n number of passes per donwsampling/upsampling step | ||
| */ | ||
| public void setNumSamplingPasses(int n) { | ||
codex128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (n <= 0) { | ||
| throw new IllegalArgumentException("Expected number of sampling passes to be greater than zero (found: "+n+")."); | ||
codex128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| numSamplingPasses = n; | ||
| if (initialized) { | ||
| initFilter(assetManager, renderManager, viewPort, width, height); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets the factor at which the glow result texture is merged with | ||
| * the scene texture. | ||
| * <p> | ||
| * Low values favor the scene texture more, while high values make | ||
| * glow more noticable. This value is clamped between 0 and 1. | ||
codex128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * <p> | ||
| * default=0.05f | ||
| * | ||
| * @param factor | ||
| */ | ||
| public void setGlowFactor(float factor) { | ||
| this.glowFactor = FastMath.clamp(factor, 0, 1); | ||
| if (material != null) { | ||
| material.setFloat("GlowFactor", glowFactor); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the number of downsampling/upsampling passes per step. | ||
| * | ||
| * @return glow factor | ||
codex128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @see #setNumSamplingPasses(int) | ||
| */ | ||
| public int getNumSamplingPasses() { | ||
| return numSamplingPasses; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the glow factor. | ||
| * | ||
| * @return glow factor | ||
| * @see #setGlowFactor(float) | ||
| */ | ||
| public float getGlowFactor() { | ||
| return glowFactor; | ||
| } | ||
|
|
||
codex128 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
60 changes: 60 additions & 0 deletions
60
jme3-effects/src/main/resources/Common/MatDefs/Post/Downsample.frag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
|
|
||
| #import "Common/ShaderLib/GLSLCompat.glsllib" | ||
| #import "Common/ShaderLib/MultiSample.glsllib" | ||
|
|
||
| uniform COLORTEXTURE m_Texture; | ||
| uniform vec2 m_TexelSize; | ||
| varying vec2 texCoord; | ||
|
|
||
| void main() { | ||
|
|
||
| // downsampling code: https://learnopengl.com/Guest-Articles/2022/Phys.-Based-Bloom | ||
|
|
||
| float x = m_TexelSize.x; | ||
| float y = m_TexelSize.y; | ||
|
|
||
| // Take 13 samples around current texel | ||
| // a - b - c | ||
| // - j - k - | ||
| // d - e - f | ||
| // - l - m - | ||
| // g - h - i | ||
| // === ('e' is the current texel) === | ||
| vec3 a = getColor(m_Texture, vec2(texCoord.x - 2*x, texCoord.y + 2*y)).rgb; | ||
| vec3 b = getColor(m_Texture, vec2(texCoord.x, texCoord.y + 2*y)).rgb; | ||
| vec3 c = getColor(m_Texture, vec2(texCoord.x + 2*x, texCoord.y + 2*y)).rgb; | ||
|
|
||
| vec3 d = getColor(m_Texture, vec2(texCoord.x - 2*x, texCoord.y)).rgb; | ||
| vec3 e = getColor(m_Texture, vec2(texCoord.x, texCoord.y)).rgb; | ||
| vec3 f = getColor(m_Texture, vec2(texCoord.x + 2*x, texCoord.y)).rgb; | ||
|
|
||
| vec3 g = getColor(m_Texture, vec2(texCoord.x - 2*x, texCoord.y - 2*y)).rgb; | ||
| vec3 h = getColor(m_Texture, vec2(texCoord.x, texCoord.y - 2*y)).rgb; | ||
| vec3 i = getColor(m_Texture, vec2(texCoord.x + 2*x, texCoord.y - 2*y)).rgb; | ||
|
|
||
| vec3 j = getColor(m_Texture, vec2(texCoord.x - x, texCoord.y + y)).rgb; | ||
| vec3 k = getColor(m_Texture, vec2(texCoord.x + x, texCoord.y + y)).rgb; | ||
| vec3 l = getColor(m_Texture, vec2(texCoord.x - x, texCoord.y - y)).rgb; | ||
| vec3 m = getColor(m_Texture, vec2(texCoord.x + x, texCoord.y - y)).rgb; | ||
|
|
||
| // Apply weighted distribution: | ||
| // 0.5 + 0.125 + 0.125 + 0.125 + 0.125 = 1 | ||
| // a,b,d,e * 0.125 | ||
| // b,c,e,f * 0.125 | ||
| // d,e,g,h * 0.125 | ||
| // e,f,h,i * 0.125 | ||
| // j,k,l,m * 0.5 | ||
| // This shows 5 square areas that are being sampled. But some of them overlap, | ||
| // so to have an energy preserving downsample we need to make some adjustments. | ||
| // The weights are the distributed, so that the sum of j,k,l,m (e.g.) | ||
| // contribute 0.5 to the final color output. The code below is written | ||
| // to effectively yield this sum. We get: | ||
| // 0.125*5 + 0.03125*4 + 0.0625*4 = 1 | ||
| vec3 downsample = e*0.125; | ||
| downsample += (a+c+g+i)*0.03125; | ||
| downsample += (b+d+f+h)*0.0625; | ||
| downsample += (j+k+l+m)*0.125; | ||
|
|
||
| gl_FragColor = vec4(downsample, 1.0); | ||
|
|
||
| } |
22 changes: 22 additions & 0 deletions
22
jme3-effects/src/main/resources/Common/MatDefs/Post/Downsample.j3md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| MaterialDef Downsample { | ||
|
|
||
| MaterialParameters { | ||
| Texture2D Texture | ||
| Vector2 TexelSize | ||
| Int BoundDrawBuffer | ||
| Int NumSamples | ||
| } | ||
|
|
||
| Technique { | ||
| VertexShader GLSL300 GLSL150 GLSL100: Common/MatDefs/Post/Post.vert | ||
| FragmentShader GLSL300 GLSL150 GLSL100: Common/MatDefs/Post/Downsample.frag | ||
|
|
||
| WorldParameters { | ||
| } | ||
|
|
||
| Defines { | ||
| BOUND_DRAW_BUFFER: BoundDrawBuffer | ||
| RESOLVE_MS : NumSamples | ||
| } | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
jme3-effects/src/main/resources/Common/MatDefs/Post/PBRBloomFinal.frag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
|
|
||
| #import "Common/ShaderLib/GLSLCompat.glsllib" | ||
| #import "Common/ShaderLib/MultiSample.glsllib" | ||
|
|
||
| uniform COLORTEXTURE m_Texture; | ||
| uniform sampler2D m_GlowMap; | ||
| uniform float m_GlowFactor; | ||
| varying vec2 texCoord; | ||
|
|
||
| void main() { | ||
|
|
||
| gl_FragColor = mix(getColor(m_Texture, texCoord), texture2D(m_GlowMap, texCoord), m_GlowFactor); | ||
|
|
||
| } | ||
|
|
23 changes: 23 additions & 0 deletions
23
jme3-effects/src/main/resources/Common/MatDefs/Post/PBRBloomFinal.j3md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| MaterialDef PBRBloomFinal { | ||
|
|
||
| MaterialParameters { | ||
| Texture2D Texture | ||
| Texture2D GlowMap | ||
| Float GlowFactor : 0.05 | ||
| Int BoundDrawBuffer | ||
| Int NumSamples | ||
| } | ||
|
|
||
| Technique { | ||
| VertexShader GLSL300 GLSL150 GLSL100: Common/MatDefs/Post/Post.vert | ||
| FragmentShader GLSL300 GLSL150 GLSL100: Common/MatDefs/Post/PBRBloomFinal.frag | ||
|
|
||
| WorldParameters { | ||
| } | ||
|
|
||
| Defines { | ||
| BOUND_DRAW_BUFFER: BoundDrawBuffer | ||
| RESOLVE_MS : NumSamples | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.