I'm reading the following paper and I'm trying to implement it, but it seems I'm not understanding fully the paper.
https://vcg.informatik.uni-rostock.de/~malub/publications/2016_depthenhance/16_ARLS_depthenhancement.pdf
I have calculated optical flow and some history of the depth images, and took the a linear average over the history samples as shown in the shader code.
What I get is an image that has holes however the original one doesn't have any holes.
Current filtered image
Shader Implementation:
Shader "Unlit/DepthFilter"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_PrevTex("_PrevTex", 2D) = "white" {}
_CurrentDepth("Current Depth", 2D) = "white" {}
_HistoryA("History A", 2D) = "white" {}
_HistoryB("History B", 2D) = "white" {}
_HistoryC("History C", 2D) = "white" {}
_dataDelta("Data Delta", range(0, 256)) = 256
_Lambda("Lambda", Range(0.0, 0.1)) = 0.015
_Threshold("Threshold", Range(0.0, 10)) = 0.046
_Scale("_Scale", Range(0.0, 10)) = 1.33
}
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _PrevTex;
float4 _MainTex_TexelSize;
sampler2D _CurrentDepth;
sampler2D _HistoryA;
sampler2D _HistoryB;
sampler2D _HistoryC;
float _dataDelta;
float _Scale, _Lambda, _Threshold;
float4 gradient(sampler2D tex, float2 uv, float2 offset)
{
return (tex2D(tex, uv + offset)) - (tex2D(tex, uv - offset));
}
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float2 uv = i.uv;
float4 current = tex2D(_MainTex, uv);
float4 prev = tex2D(_PrevTex, uv);
float2 dx = float2(_MainTex_TexelSize.x, 0);
float2 dy = float2(0, _MainTex_TexelSize.y);
float4 diff = current - prev;
float4 gx = gradient(_PrevTex, uv, dx) + gradient(_MainTex, uv, dx);
float4 gy = gradient(_PrevTex, uv, dy) + gradient(_MainTex, uv, dy);
float4 gmag = sqrt(gx * gx + gy * gy + float4(_Lambda, _Lambda, _Lambda, _Lambda));
float4 invGmag = 1.0 / gmag;
float4 vx = diff * (gx * invGmag);
float4 vy = diff * (gy * invGmag);
float2 flow = float2(0, 0);
const float inv3 = 0.33333;
flow.x = -(vx.x + vx.y + vx.z) * inv3;
flow.y = -(vy.x + vy.y + vy.z) * inv3;
float w = length(flow);
float nw = (w - _Threshold) / (1.0 - _Threshold);
flow = lerp(float2(0, 0), normalize(flow) * nw * _Scale, step(_Threshold, w));
float2 curr = tex2D(_CurrentDepth, i.uv).xy;
float2 last_depth_frame_1 = tex2D(_HistoryA, i.uv).xy;
float2 last_depth_frame_2 = tex2D(_HistoryB, i.uv).xy;
float2 last_depth_frame_3 = tex2D(_HistoryC, i.uv).xy;
float2 last_depth_avg = (last_depth_frame_1 + last_depth_frame_2 + last_depth_frame_3) / 3.0;
float2 average_at_pixel;
average_at_pixel.x = curr.x + last_depth_avg.x - flow.x;
average_at_pixel.y = curr.y + last_depth_avg.y - flow.y;
fixed c = average_at_pixel;
//float4 velocity = float4(flow, 0,1);
//return float4(abs(velocity.xy), 0, 1);
return c;
}
ENDCG
}
}
}
Related
I would like to convert a DepthInformation into a position.
Somehow I can't do it even after several attempts.
I try to get the position of objects fragment from the DepthInformation that i get from an Depthpeeling shader.
This is the code writing the information in a RenderTexture
Shader "Hidden/OIT/Depth Peeling/Depth Peeling" {
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_MainTex ("Main Tex", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "bump" {}
}
SubShader {
Tags {"Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="Transparent"}
Pass {
Tags { "LightMode"="ForwardBase" }
ZWrite On
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
sampler2D _PrevDepthTex;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float3 lightDir: TEXCOORD0;
float3 viewDir : TEXCOORD1;
float2 uv : TEXCOORD2;
float4 screenPos: TEXCOORD3;
float depth : TEXCOORD4;
};
struct PixelOutput {
fixed4 col : COLOR0;
fixed4 depth : COLOR1;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
TANGENT_SPACE_ROTATION;
// Transform the light direction from object space to tangent space
o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
// Transform the view direction from object space to tangent space
o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
o.screenPos = ComputeScreenPos(o.pos);
o.depth = COMPUTE_DEPTH_01;
return o;
}
PixelOutput frag(v2f i) : SV_Target {
float depth = i.depth;
float prevDepth = DecodeFloatRGBA(tex2Dproj(_PrevDepthTex, UNITY_PROJ_COORD(i.screenPos)));
clip(depth - (prevDepth + 0.00001));
fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);
fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uv));
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal, tangentLightDir));
PixelOutput o;
o.col = fixed4(ambient + diffuse, _Color.a);
o.depth = EncodeFloatRGBA(i.depth);
return o;
}
ENDCG
}
}
FallBack "Diffuse/VertexLit"
}
and this is the function where i try to decode it again to a world position:
float4 DepthTextureToPos(sampler2D depthSampler,float2 uvCoord,float4 screenPos) {
float depth = DecodeFloatRGBA(tex2Dproj(depthSampler, UNITY_PROJ_COORD(screenPos)));
// H is the viewport position at this pixel in the range -1 to 1.
float4 H = float4((uvCoord.x) * 2 - 1, (uvCoord.y) * 2 - 1, depth, 1.0);
float4 D = mul(_ViewProjectInverse, H);
//return D / D.w;
float4 objPos= mul(unity_WorldToObject, (D / D.w));
return objPos;
}
the uv Coords have this as input:
float2 texc = i.screenPosition.xy / i.screenPosition.w;
the screenPos has this as Input:
ComputeScreenPos(o.vertex);
I'm working on a URP shader in unity that is supposed to make an object transparent the further away it is from the camera.
The shader I wrote does not work and I can't find a solution.
Shader "Custom/DistanceFade" {
Properties{
_Color("Color", Color) = (1,1,1,1)
_Threshold("Threshold", float) = 5
}
SubShader{
Tags { "Queue" = "Transparent" }
LOD 200
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _Color;
float _Threshold;
v2f vert(appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord;
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed4 col = tex2D(_MainTex, i.texcoord);
float _distance = distance(_WorldSpaceCameraPos, i.vertex);
col.a = saturate((_Threshold - _distance) / _Threshold);
return col * _Color;
}
ENDCG
}
}
FallBack "Diffuse"
}
Does anyone have an idea?
Basically you just need to enable blending assuming you have meaningful distance values. Here is a variant which does not depend on a depth texture for the distance and gives some control over the fading interval:
Shader "Custom/DistanceFade" {
Properties{
_Color("Color", Color) = (1,1,1,1)
_Threshold("Threshold", float) = 5
}
SubShader{
Tags { "Queue" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha // <-- enable blending
LOD 200
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
// make float3 for an extra value (can be done in other ways)
float3 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _Color;
float _Threshold;
v2f vert(appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord.xy = v.texcoord.xy;
// get the distance from the camera to the vertex
// (we do it in world space)
float dist = distance(_WorldSpaceCameraPos, mul(unity_ObjectToWorld, v.vertex));
// map the distance to an fade interval
float beginfade = 500;
float endfade = 600;
float alpha = min(max(dist, beginfade), endfade) - beginfade;
alpha = 1 - alpha / (endfade - beginfade);
// put alpha somewhere unused to deliver it to the fragment shader
o.texcoord.z = alpha ;
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed4 col = tex2D(_MainTex, i.texcoord);
// use our fade value
col.a = i.texcoord.z;
return col * _Color;
}
ENDCG
}
}
FallBack "Diffuse"
}
I'm making a volumetric lightning from the tutorial on https://www.kodeco.com/22027819-volumetric-light-scattering-as-a-custom-renderer-feature-in-urp.
and I'm getting an error from unity: [Worker0] Shader error in 'Hidden/RW/RadialBlur': unrecognized identifier 'Blit' at line 26 (on d3d11)
I tried changing the variables sorting but didn't worked.
Full code :
Shader "Hidden/RW/RadialBlur"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_BlurWidth("Blur Width", Range(0,1)) = 0.85
_Intensity("Intensity", Range(0,1)) = 1
_Center("Center", Vector) = (0.5,0.5,0,0)
}
SubShader
{
// No culling or depth
Blend One One
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define NUM_SAMPLES 100
float _BlurWidth;
float _Intensity;
float4 _Center;
// HERER HERER HERER
Blit(cmd, occluders.Identifier(), cameraColorTargetIdent,
radialBlurMaterial);
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
radialBlurMaterial.SetVector("_Center", new Vector4(
sunPositionViewportSpace.x, sunPositionViewportSpace.y, 0, 0));
radialBlurMaterial.SetFloat("_Intensity", intensity);
radialBlurMaterial.SetFloat("_BlurWidth", blurWidth);
fixed4 frag(v2f i) : SV_Target
{
//1
fixed4 color = fixed4(0.0f, 0.0f, 0.0f, 1.0f);
//2
float2 ray = i.uv - _Center.xy;
//3
for (int i = 0; i < NUM_SAMPLES; i++)
{
float scale = 1.0f - _BlurWidth * (float(i) /
float(NUM_SAMPLES - 1));
color.xyz += tex2D(_MainTex, (ray * scale) +
_Center.xy).xyz / float(NUM_SAMPLES);
}
//4
return color * _Intensity;
}
ENDCG
}
}
}
I'm a beginner to shader programming please help.
I found this BLUR Shader and it works beautifully which I apply to a material.
Shader "Custom/BLUR" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_BumpAmt ("Distortion", Range (0,128)) = 10
_MainTex ("Tint Color (RGB)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_Size ("Size", Range(0, 20)) = 1
}
Category {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" }
SubShader {
GrabPass {
Tags { "LightMode" = "Always" }
}
Pass {
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float4 uvgrab : TEXCOORD0;
};
v2f vert (appdata_t v) {
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
return o;
}
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float _Size;
half4 frag( v2f i ) : COLOR {
half4 sum = half4(0,0,0,0);
#define GRABPIXEL(weight,kernelx) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
sum += GRABPIXEL(0.05, -4.0);
sum += GRABPIXEL(0.09, -3.0);
sum += GRABPIXEL(0.12, -2.0);
sum += GRABPIXEL(0.15, -1.0);
sum += GRABPIXEL(0.18, 0.0);
sum += GRABPIXEL(0.15, +1.0);
sum += GRABPIXEL(0.12, +2.0);
sum += GRABPIXEL(0.09, +3.0);
sum += GRABPIXEL(0.05, +4.0);
return sum;
}
ENDCG
}
GrabPass {
Tags { "LightMode" = "Always" }
}
Pass {
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float4 uvgrab : TEXCOORD0;
};
v2f vert (appdata_t v) {
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
return o;
}
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float _Size;
half4 frag( v2f i ) : COLOR {
half4 sum = half4(0,0,0,0);
#define GRABPIXEL(weight,kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely*_Size, i.uvgrab.z, i.uvgrab.w))) * weight
sum += GRABPIXEL(0.05, -4.0);
sum += GRABPIXEL(0.09, -3.0);
sum += GRABPIXEL(0.12, -2.0);
sum += GRABPIXEL(0.15, -1.0);
sum += GRABPIXEL(0.18, 0.0);
sum += GRABPIXEL(0.15, +1.0);
sum += GRABPIXEL(0.12, +2.0);
sum += GRABPIXEL(0.09, +3.0);
sum += GRABPIXEL(0.05, +4.0);
return sum;
}
ENDCG
}
GrabPass {
Tags { "LightMode" = "Always" }
}
Pass {
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float4 uvgrab : TEXCOORD0;
float2 uvbump : TEXCOORD1;
float2 uvmain : TEXCOORD2;
};
float _BumpAmt;
float4 _BumpMap_ST;
float4 _MainTex_ST;
v2f vert (appdata_t v) {
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
return o;
}
fixed4 _Color;
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
sampler2D _BumpMap;
sampler2D _MainTex;
half4 frag( v2f i ) : COLOR {
half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg;
float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
half4 tint = tex2D( _MainTex, i.uvmain ) * _Color;
return col * tint;
}
ENDCG
}
}
}
}
But when using with URP Asset (Universal Render Pipeline), the BLUR looks just like a solid color. It works without using URP. Is it possible to use Custom Shader while still using URP?
I tried everything to fix and make it work together but no progress so far.
I highly value any reply.
I have blur shader to Unity and this shader doesn't support SinglePass rendering mode (after build on Oculus GO with SinglePass graphic artifacts appear). I tried to implement support for SinglePass rendering using Unity guides, but with no effects. I do not have any ideas what could be wrong.
Unity SinglePass guidies I tried applied:
https://docs.unity3d.com/Manual/SinglePassStereoRendering.html
https://docs.unity3d.com/Manual/SinglePassInstancing.html
OLD VERSION NOT SUPPORTED SINGLE PASS RENDERING:
Shader "Custom/BLUR" {
Properties
{
_Radius("Radius", Range(1, 255)) = 1
}
Category
{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Opaque" }
SubShader
{
GrabPass
{
Tags{ "LightMode" = "Always" }
}
Pass
{
Tags{ "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};
struct v2f
{
float4 vertex : POSITION;
float4 uvgrab : TEXCOORD0;
};
v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
return o;
}
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float _Radius;
half4 frag(v2f i) : COLOR
{
half4 sum = half4(0,0,0,0);
#define GRABXYPIXEL(kernelx, kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely, i.uvgrab.z, i.uvgrab.w)))
sum += GRABXYPIXEL(0.0, 0.0);
int measurments = 1;
for (float range = 0.1f; range <= _Radius; range += 0.1f)
{
sum += GRABXYPIXEL(range, range);
sum += GRABXYPIXEL(range, -range);
sum += GRABXYPIXEL(-range, range);
sum += GRABXYPIXEL(-range, -range);
measurments += 4;
}
return sum / measurments;
}
ENDCG
}
GrabPass
{
Tags{ "LightMode" = "Always" }
}
Pass
{
Tags{ "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};
struct v2f
{
float4 vertex : POSITION;
float4 uvgrab : TEXCOORD0;
};
v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
return o;
}
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float _Radius;
half4 frag(v2f i) : COLOR
{
half4 sum = half4(0,0,0,0);
float radius = 1.41421356237 * _Radius;
#define GRABXYPIXEL(kernelx, kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely, i.uvgrab.z, i.uvgrab.w)))
sum += GRABXYPIXEL(0.0, 0.0);
int measurments = 1;
for (float range = 1.41421356237f; range <= radius * 1.41; range += 1.41421356237f)
{
sum += GRABXYPIXEL(range, 0);
sum += GRABXYPIXEL(-range, 0);
sum += GRABXYPIXEL(0, range);
sum += GRABXYPIXEL(0, -range);
measurments += 4;
}
return sum / measurments;
}
ENDCG
}
}
}
}
MY NON-WORKING CHANGES:
Shader "Custom/BLUR" {
Properties
{
_Radius("Radius", Range(1, 255)) = 1
}
Category
{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Opaque" }
SubShader
{
GrabPass
{
Tags{ "LightMode" = "Always" }
}
Pass
{
Tags{ "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};
struct v2f
{
float4 vertex : POSITION;
float4 uvgrab : TEXCOORD0;
};
v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
return o;
}
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float _Radius;
half4 _MainTex_ST;
half4 frag(v2f i) : SV_Target
{
half4 sum = half4(0,0,0,0);
#if UNITY_SINGLE_PASS
#define GRABXYPIXEL(kernelx, kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(UnityStereoScreenSpaceUVAdjust(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely, i.uvgrab.z, i.uvgrab.w),_MainTex_ST)))
#else
#define GRABXYPIXEL(kernelx, kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely, i.uvgrab.z, i.uvgrab.w)))
#endif
sum += GRABXYPIXEL(0.0, 0.0);
int measurments = 1;
for (float range = 0.1f; range <= _Radius; range += 0.1f)
{
sum += GRABXYPIXEL(range, range);
sum += GRABXYPIXEL(range, -range);
sum += GRABXYPIXEL(-range, range);
sum += GRABXYPIXEL(-range, -range);
measurments += 4;
}
return sum / measurments;
}
ENDCG
}
GrabPass
{
Tags{ "LightMode" = "Always" }
}
Pass
{
Tags{ "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};
struct v2f
{
float4 vertex : POSITION;
float4 uvgrab : TEXCOORD0;
};
v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
return o;
}
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float _Radius;
half4 _MainTex_ST;
half4 frag(v2f i) : SV_Target
{
half4 sum = half4(0,0,0,0);
float radius = 1.41421356237 * _Radius;
#if UNITY_SINGLE_PASS
#define GRABXYPIXEL(kernelx, kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(UnityStereoScreenSpaceUVAdjust(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely, i.uvgrab.z, i.uvgrab.w),_MainTex_ST)))
#else
#define GRABXYPIXEL(kernelx, kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely, i.uvgrab.z, i.uvgrab.w)))
#endif
sum += GRABXYPIXEL(0.0, 0.0);
int measurments = 1;
for (float range = 1.41421356237f; range <= radius * 1.41; range += 1.41421356237f)
{
sum += GRABXYPIXEL(range, 0);
sum += GRABXYPIXEL(-range, 0);
sum += GRABXYPIXEL(0, range);
sum += GRABXYPIXEL(0, -range);
measurments += 4;
}
return sum / measurments;
}
ENDCG
}
}
}
}
I expect this shader will support SinglePass VR rendering mode.