Skip to content

Commit

Permalink
SSAOPass bug fix #55 and added some example
Browse files Browse the repository at this point in the history
  • Loading branch information
hjoykim committed Nov 18, 2024
1 parent 7e9e6c4 commit 9b12190
Show file tree
Hide file tree
Showing 39 changed files with 1,099 additions and 159 deletions.
2 changes: 1 addition & 1 deletion THREE.OpenGL/Postprocessing/BloomPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public BloomPass(float? strength = null, int? kernelSize = null, float? sigma =

this.NeedsSwap = false;

this.fullScreenQuad = new Pass.FullScreenQuad();
this.fullScreenQuad = new FullScreenQuad();
}
public override void Render(GLRenderer renderer, GLRenderTarget writeBuffer, GLRenderTarget readBuffer, float? deltaTime = null, bool? maskActive = null)
{
Expand Down
2 changes: 1 addition & 1 deletion THREE.OpenGL/Postprocessing/BokehPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public BokehPass(Scene scene, Camera camera, Hashtable parameter) : base()
this.uniforms = bokehUniforms;
this.NeedsSwap = false;

this.fullScreenQuad = new Pass.FullScreenQuad(this.materialBokeh);
this.fullScreenQuad = new FullScreenQuad(this.materialBokeh);

this.oldClearColor = new Color();

Expand Down
2 changes: 1 addition & 1 deletion THREE.OpenGL/Postprocessing/FilmPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public FilmPass(float? noiseIntensity, float? scanlinesIntensity, float? scanlin
if (scanlinesIntensity != null) (this.uniforms["sIntensity"] as GLUniform)["value"] = scanlinesIntensity.Value;
if (scanlinesCount != null) (this.uniforms["sCount"] as GLUniform)["value"] = scanlinesCount.Value;

this.fullScreenQuad = new Pass.FullScreenQuad(this.material);
this.fullScreenQuad = new FullScreenQuad(this.material);
}

public override void Render(GLRenderer renderer, GLRenderTarget writeBuffer, GLRenderTarget readBuffer, float? deltaTime = null, bool? maskActive = null)
Expand Down
2 changes: 1 addition & 1 deletion THREE.OpenGL/Postprocessing/GlitchPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public GlitchPass(float? dt_size = null) : base()
FragmentShader = shader.FragmentShader
};

this.fullScreenQuad = new Pass.FullScreenQuad(this.material);
this.fullScreenQuad = new FullScreenQuad(this.material);

this.goWild = false;
this.curF = 0;
Expand Down
2 changes: 1 addition & 1 deletion THREE.OpenGL/Postprocessing/HalftonePass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public HalftonePass(float? width = null, float? height = null, Hashtable paramet
}
}
}
this.fullScreenQuad = new Pass.FullScreenQuad(this.material);
this.fullScreenQuad = new FullScreenQuad(this.material);
}
public override void Render(GLRenderer renderer, GLRenderTarget writeBuffer, GLRenderTarget readBuffer, float? deltaTime = null, bool? maskActive = null)
{
Expand Down
2 changes: 1 addition & 1 deletion THREE.OpenGL/Postprocessing/OutlinePass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public OutlinePass(Vector2 resolution, Scene scene, Camera camera, List<Object3D
this.oldClearColor = new Color();
this.oldClearAlpha = 1;

this.fullScreenQuad = new Pass.FullScreenQuad(null);
this.fullScreenQuad = new FullScreenQuad(null);

this.tempPulseColor1 = new Color();
this.tempPulseColor2 = new Color();
Expand Down
131 changes: 58 additions & 73 deletions THREE.OpenGL/Postprocessing/Pass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,102 +3,87 @@
namespace THREE
{
[Serializable]
public abstract class Pass
// Helper for passes that need to fill the viewport with a single quad.
public class FullScreenQuad : IDisposable
{
public bool Enabled = true;
public bool NeedsSwap = true;
public bool Clear = false;
public bool RenderToScreen = false;
private OrthographicCamera camera = new OrthographicCamera(-1, 1, 1, -1, 0, 1);
private BufferGeometry geometry = new BufferGeometry();

[Serializable]
// Helper for passes that need to fill the viewport with a single quad.
public class FullScreenQuad : IDisposable
private Mesh _mesh = null;
public event EventHandler<EventArgs> Disposed;

public Material material
{
private OrthographicCamera camera = new OrthographicCamera(-1, 1, 1, -1, 0, 1);
private PlaneBufferGeometry geometry = new PlaneBufferGeometry(2, 2);

private Mesh _mesh = null;
private Scene scene = new Scene();
public event EventHandler<EventArgs> Disposed;

public Material material
get
{
get
{
if (_mesh == null) return null;
else return _mesh.Material;
}
set
{
if (_mesh == null)
{
_mesh = new Mesh(geometry, value);
scene.Add(_mesh);
}
else
{
_mesh.Material = value;
}
}
return _mesh.Material;
}
public FullScreenQuad()
set
{

_mesh.Material = value;
}
}

~FullScreenQuad()
{
Dispose(false);
}
public FullScreenQuad(Material material)
{
_mesh = new Mesh(geometry, material);
~FullScreenQuad()
{
Dispose(false);
}
public FullScreenQuad(Material material = null)
{
geometry.SetAttribute("position", new BufferAttribute<float>(new float[] { -1, 3, 0, -1, -1, 0, 3, -1, 0 }, 3));
geometry.SetAttribute("uv", new BufferAttribute<float>(new float[] { 0, 2, 0, 0, 2, 0 }, 2));
_mesh = new Mesh(geometry, material);

scene.Add(_mesh);
}
}

public void Render(GLRenderer renderer)
{
renderer.Render(scene, camera);
}
public virtual void Dispose()
{
Dispose(disposed);
}
protected virtual void RaiseDisposed()
public void Render(GLRenderer renderer)
{
renderer.Render(_mesh, camera);
}
public virtual void Dispose()
{
Dispose(disposed);
}
protected virtual void RaiseDisposed()
{
var handler = this.Disposed;
if (handler != null)
handler(this, new EventArgs());
}
private bool disposed;
protected virtual void Dispose(bool disposing)
{
if (this.disposed) return;
try
{
var handler = this.Disposed;
if (handler != null)
handler(this, new EventArgs());
if (_mesh != null)
_mesh.Geometry.Dispose();
this.RaiseDisposed();
this.disposed = true;
}
private bool disposed;
protected virtual void Dispose(bool disposing)
finally
{
if (this.disposed) return;
try
{
if (_mesh != null)
_mesh.Geometry.Dispose();
this.RaiseDisposed();
this.disposed = true;
}
finally
{

}
this.disposed = true;
}

this.disposed = true;
}
public FullScreenQuad fullScreenQuad = null;

}
[Serializable]
public abstract class Pass
{
public bool Enabled = true;
public bool NeedsSwap = true;
public bool Clear = false;
public bool RenderToScreen = false;
public FullScreenQuad fullScreenQuad;
public Pass() { }

public abstract void SetSize(float width, float height);



public abstract void Render(GLRenderer renderer, GLRenderTarget writeBuffer, GLRenderTarget readBuffer, float? deltaTime = null, bool? maskActive = null);
public abstract void Render(GLRenderer renderer, GLRenderTarget writeBuffer , GLRenderTarget readBuffer=null, float? deltaTime = null, bool? maskActive = null);

}
}
33 changes: 27 additions & 6 deletions THREE.OpenGL/Postprocessing/SSAOPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using THREE.Renderers.Shaders;
using static THREE.ConvexHull;

namespace THREE
{
Expand Down Expand Up @@ -32,7 +33,7 @@ public class SSAOPass : Pass, IDisposable
ShaderMaterial depthRenderMaterial;
ShaderMaterial copyMaterial;
Color originalClearColor;

Dictionary<Object3D,bool> _visibilityCache = new Dictionary<Object3D,bool>();
public enum OUTPUT
{
Default = 0,
Expand Down Expand Up @@ -116,7 +117,7 @@ public SSAOPass(Scene scene, Camera camera, int? width = null, int? height = nul
(this.ssaoMaterial.Uniforms["cameraFar"] as GLUniform)["value"] = this.camera.Far;
((this.ssaoMaterial.Uniforms["resolution"] as GLUniform)["value"] as Vector2).Set(this.width, this.height);
((this.ssaoMaterial.Uniforms["cameraProjectionMatrix"] as GLUniform)["value"] as Matrix4).Copy(this.camera.ProjectionMatrix);
((this.ssaoMaterial.Uniforms["cameraInverseProjectionMatrix"] as GLUniform)["value"] as Matrix4).GetInverse(this.camera.ProjectionMatrix);
((this.ssaoMaterial.Uniforms["cameraInverseProjectionMatrix"] as GLUniform)["value"] as Matrix4).GetInverse(this.camera.ProjectionMatrixInverse);

// normal material

Expand Down Expand Up @@ -165,7 +166,7 @@ public SSAOPass(Scene scene, Camera camera, int? width = null, int? height = nul
{ "blendEquationAlpha", Constants.AddEquation }
});

this.fullScreenQuad = new FullScreenQuad();
this.fullScreenQuad = new FullScreenQuad(null);

this.originalClearColor = new Color();
}
Expand Down Expand Up @@ -243,9 +244,9 @@ public override void Render(GLRenderer renderer, GLRenderTarget writeBuffer, GLR
renderer.Render(this.scene, this.camera);

// render normals

this.OverrideVisibility();
this.RenderOverride(renderer, this.normalMaterial, this.normalRenderTarget, Color.Hex(0x7777ff), 1.0f);

this.RestoreVisibility();
// render SSAO

(this.ssaoMaterial.Uniforms["kernelRadius"] as GLUniform)["value"] = this.kernelRadius;
Expand Down Expand Up @@ -317,6 +318,26 @@ public override void Render(GLRenderer renderer, GLRenderTarget writeBuffer, GLR
}
}

private void RestoreVisibility()
{
scene.Traverse((object3d) => {

object3d.Visible = _visibilityCache[object3d];

});
}

private void OverrideVisibility()
{
scene.Traverse((object3d)=> {

_visibilityCache[object3d] = object3d.Visible;

if (object3d is Points || object3d is Line) object3d.Visible = false;

} );
}

public override void SetSize(float width, float height)
{
this.width = (int)width;
Expand All @@ -329,7 +350,7 @@ public override void SetSize(float width, float height)

((this.ssaoMaterial.Uniforms["resolution"] as GLUniform)["value"] as Vector2).Set(width, height);
((this.ssaoMaterial.Uniforms["cameraProjectionMatrix"] as GLUniform)["value"] as Matrix4).Copy(this.camera.ProjectionMatrix);
((this.ssaoMaterial.Uniforms["cameraInverseProjectionMatrix"] as GLUniform)["value"] as Matrix4).GetInverse(this.camera.ProjectionMatrix);
((this.ssaoMaterial.Uniforms["cameraInverseProjectionMatrix"] as GLUniform)["value"] as Matrix4).GetInverse(this.camera.ProjectionMatrixInverse);

((this.blurMaterial.Uniforms["resolution"] as GLUniform)["value"] as Vector2).Set(width, height);

Expand Down
2 changes: 1 addition & 1 deletion THREE.OpenGL/Postprocessing/ShaderPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ShaderPass(Material shader, string textureId = null)
material = shader as ShaderMaterial;
}

fullScreenQuad = new Pass.FullScreenQuad(this.material);
fullScreenQuad = new FullScreenQuad(this.material);
}
public override void Render(GLRenderer renderer, GLRenderTarget writeBuffer, GLRenderTarget readBuffer, float? deltaTime = null, bool? maskActive = null)
{
Expand Down
Loading

0 comments on commit 9b12190

Please sign in to comment.