Last active
August 16, 2017 16:04
-
-
Save yorung/d13061a8ba0d032d2c83 to your computer and use it in GitHub Desktop.
a pixel shader for drawing background using a cubemap or photosphere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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