- Home /
Refraction Shader. Strange Artifacts
Hi! I'm trying to create a lens effect in front of a texture using vertex and fragment programs. The shader program is small and quite naive; I get some artifacts that I really can't understand why they are there or how to fix them;
Demo: http://johannes.gotlen.se/blog/2010/11/refraction/
Artifact-example:
// Per pixel refraction. // Uses a normal map and vertex normals to distort the image behind Shader "FX/Glass/RefractionMap" { Properties { _BumpAmt ("Glass Thickness", Range (-5,5)) = 0.2 _DiscRadius("Disc Radius", Float) = 1 _MainTexWidth ("Width", Float) = 512 _MainTexHeight("Height", Float) = 512 _MainTex ("Background", 2D) = "white" {} _BumpMap ("NormalInfo", 2D) = "bump" {} }
Category { // We must be transparent, so other objects are drawn before this one. Tags { "Queue"="Transparent" "RenderType"="Opaque" } SubShader {
// This pass grabs the screen behind the object into a texture.
// We can access the result in the next pass as _GrabTexture
GrabPass {
Name "BASE"
Tags { "LightMode" = "Always" }
}
// Main pass: Take the texture grabbed above and use the bumpmap to perturb it
// on to the screen
Pass {
Name "BASE"
Tags { "LightMode" = "Always" }
CGPROGRAM #pragma exclude_renderers gles #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #include "UnityCG.cginc"
struct appdata_t { half4 vertex : POSITION; half2 texcoord: TEXCOORD0; half4 normal: NORMAL; };
struct v2f { half4 vertex : POSITION; half3 normal : COLOR0; half2 uvmain : TEXCOORD0; }; const half _MainTexWidth; const half _MainTexHeight; const half _BumpAmt; const half4 _BumpMap_ST; const half4 _MainTex_ST; const half _DiscRadius; static const half2 pixelSize = 1h / half2(_MainTexWidth,_MainTexHeight);
v2f vert (appdata_t v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.normal = mul(UNITY_MATRIX_IT_MV, v.normal); o.normal = o.normal * 0.5h + 0.5h; o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex ); return o; }
sampler2D _BumpMap; sampler2D _MainTex; static const half2 poisson[11] = { half2(-0.326212h, -0.40581h), half2(-0.840144h, -0.07358h), half2(-0.695914h, 0.457137h), half2(-0.203345h, 0.620716h), half2(0.96234h, -0.194983h), half2(0.473434h, -0.480026h), half2(0.519456h, 0.767022h), half2(0.185461h, -0.893124h), half2(0.507431h, 0.064425h), half2(0.89642h, 0.412458h), half2(-0.32194h, -0.932615h) };
half4 SiGrowablePoissonDisc12FilterRGB(sampler2D tSource, half2 texCoord, half2 pixelSize, half discRadius) {
// Center tap
half4 cOut = tex2D(tSource, texCoord);
for(short tap = 0; tap </pre>
Thanks!
Your answer
Follow this Question
Related Questions
Prevent ColorMask obscuring parts of an object's mesh 0 Answers
Unexpected behavior in Vertex/Fragment shader when using saturate method (CG) 0 Answers
Stop shader values from being clamped 1 Answer
Vertex/Fragment shaders - transparency ignoring Render Queue 3 Answers
Grabpass refraction masking 0 Answers