Skip to content

Instantly share code, notes, and snippets.

@yorung
Last active August 16, 2017 16:04
Show Gist options
  • Save yorung/d13061a8ba0d032d2c83 to your computer and use it in GitHub Desktop.
Save yorung/d13061a8ba0d032d2c83 to your computer and use it in GitHub Desktop.
a pixel shader for drawing background using a cubemap or photosphere
cbuffer perObject : register(b0)
{
row_major matrix invVP;
}
TextureCube texCube : register(t0);
SamplerState samplerState : register(s0);
struct VsToPs
{
float4 pos : SV_POSITION;
float4 pos2 : POS2;
};
VsToPs mainVS(uint id : SV_VertexID)
{
VsToPs ret;
ret.pos = float4(id & 1 ? 1 : -1, id & 2 ? -1 : 1, 1, 1);
ret.pos2 = ret.pos;
return ret;
}
float4 mainPS(VsToPs inp) : SV_Target
{
float3 dir = normalize(mul(inp.pos2, invVP).xyz);
return texCube.Sample(samplerState, dir);
}
cbuffer perObject : register(b0)
{
row_major matrix invVP;
}
Texture2D gTexture : register(t0);
SamplerState samplerState : register(s0);
struct VsToPs
{
float4 pos : SV_POSITION;
float4 pos2 : POS2;
};
VsToPs mainVS(uint id : SV_VertexID)
{
VsToPs ret;
ret.pos = float4(id & 1 ? 1 : -1, id & 2 ? -1 : 1, 1, 1);
ret.pos2 = ret.pos;
return ret;
}
float4 mainPS(VsToPs inp) : SV_Target
{
float3 dir = normalize(mul(inp.pos2, invVP).xyz);
float coordX = atan2(dir.x, dir.z) / 3.1415926;
float coordY = asin(dir.y) / (3.1415926 / 2);
return gTexture.Sample(samplerState, float2(coordX, coordY) * float2(0.5, -0.5) + 0.5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment