I’m writing my first image effect, there seems to be shortage of info on that, but I got the basics working.
Anyway, I want to access the skybox in the shader, I did this by making a cubemap of the skybox which I sample from inside the shader. This should work but the result is distorted, and doesn’t matches the actual skybox.
Because it’s the first time I write an image effect from scratch, I made it first as a normal shader, and that works perfectly:
Shader "Custom/SpaceHole"
{
Properties
{
_Cube ("Cubemap", CUBE) = "" {}
}
SubShader
{
Tags { "RenderType" = "Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 pos : POSITION;
float3 viewDir : TEXCOORD1;
};
samplerCUBE _Cube;
v2f vert( appdata_base v )
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.viewDir = WorldSpaceViewDir(v.vertex);
return o;
}
half4 frag(v2f i) : COLOR
{
float3 temp = i.viewDir;
temp.y = -temp.y;
temp.z = -temp.z;
float4 overlay = texCUBE (_Cube, temp);
return overlay;
}
ENDCG
}
}
Fallback "Diffuse"
}
I’m using the exact same code in my post effect, but I get a different result when sampling from the cubemap.
Anyone any idea why that is and how to fix it?
This is the image effect:
Shader "Custom/AtmosphereOverlayShader"
{
Properties
{
_MainTex ("Base", 2D) = "" {}
_Cube ("Cubemap", CUBE) = "" {}
}
CGINCLUDE
#include "UnityCG.cginc"
struct v2f
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
float3 viewDir : TEXCOORD1;
};
sampler2D _MainTex;
samplerCUBE _Cube;
v2f vert( appdata_img v )
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
o.viewDir = WorldSpaceViewDir(v.vertex);
return o;
}
half4 frag(v2f i) : COLOR
{
//float4 color = tex2D (_MainTex, i.uv);
float3 temp = i.viewDir;
temp.y = -temp.y;
temp.z = -temp.z;
float4 overlay = texCUBE (_Cube, temp);
return overlay;//color*overlay;
}
ENDCG
Subshader
{
Pass
{
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma fragmentoption ARB_precision_hint_fastest
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
Fallback off
}