Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _vs(r) : register(vs, r)
- #define _ps(r) : register(ps, r)
- #define _cb(r)
- Texture NormalMap;
- sampler NormalMapSampler : register(s0) = sampler_state {
- texture = <NormalMap>;
- MinFilter = LINEAR;
- AddressU = MIRROR;
- AddressV = MIRROR;
- };
- static const float PI = 3.14159265f;
- float4x4 MatrixTransform _vs(c0) _cb(c0);
- void SpriteVertexShader(inout float4 color : COLOR0,
- inout float2 texCoord : TEXCOORD0,
- inout float4 position : SV_Position)
- {
- position = mul(position, MatrixTransform);
- }
- inline float4 EncodeFloatRGBA( float v ) {
- float4 enc = float4(1.0, 255.0, 65025.0, 160581375.0) * v;
- enc = frac(enc);
- enc -= enc.yzww * float4(1.0/255.0,1.0/255.0,1.0/255.0,0.0);
- return enc;
- }
- inline float DecodeFloatRGBA( float4 rgba ) {
- return dot( rgba, float4(1.0, 1/255.0, 1/65025.0, 1/160581375.0) );
- }
- float3 CartesianToSpherical( in float3 xyz )
- {
- float r = length( xyz );
- xyz *= 1.f/r;
- float theta = acos( xyz.z );
- float phi = atan2( xyz.y, xyz.x );
- phi += ( phi < 0 ) ? 2*PI : 0; // only if you want [0,2pi)
- return float3( phi, theta, r );
- }
- // Rotates the normals by the amount encoded in the color input parameter
- float4 SpritePixelShader(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : SV_Target0 {
- float4 inputColor = tex2D(NormalMapSampler, texCoord);
- if (inputColor.w > 0.1f) {
- float3 normal = (2.0f * (inputColor.xyz)) - 1.0f;
- // Color components r and g specify the angle in 0-1 range, Multiply with 2 Pi to get radian angle.
- float angle = (color.r + color.g/255.0) * 6.283185f;
- float x2 = normal.x * cos(angle) - normal.y * sin(angle);
- float y2 = normal.x * sin(angle) + normal.y * cos(angle);
- float4 rotated = float4((x2 + 1.0f) / 2.0f, (y2 + 1.0f) / 2.0f, (normal.z + 1.0f) / 2.0f, inputColor.w);
- rotated = float4(rotated.xyz * inputColor.w, inputColor.w); // Premultiply alpha
- return rotated;
- } else {
- return float4(0,0,0,0);
- }
- }
- technique NormalRotateByColor
- {
- pass
- {
- VertexShader = compile vs_2_0 SpriteVertexShader();
- PixelShader = compile ps_2_0 SpritePixelShader();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement