# CSharpCoder

Member

9

108 Neutral

• Rank
Newbie

• Role
Programmer
• Interests
Design
Programming

## Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

1. ## 2D 2D Specular Lighting

I have written the following pixel shader for point lighting in a 2D platformer. A normal map and a specularity map is generated using Sprite DLight. colorMap is the original sprite, normalMap.rgb is the normal and normalMap.a is the specularity from the specularity map. lightPosition has a z coordinate to position it further away from the scene. Is this correct for 2D specular lighting? float4 PointLightShader(VertexToPixel PSIn) : COLOR0 { float4 colorMap = tex2D(ColorMapSampler, PSIn.TexCoord); clip(colorMap.a - 0.001); float3 pixelPosition = float3(PSIn.WorldPos, 0); float3 lightDirection = lightPosition - pixelPosition; float coneAttenuation = saturate(1.0f - length(lightDirection) / lightDecay); float4 normalColor = tex2D(NormalMapSampler, PSIn.TexCoord); float3 normal = normalColor.rgb; float3 normalTangent = (2.0f * normal) - 1.0f; float3 lightDirNorm = normalize(lightDirection); float3 halfVec = float3(0, 0, 1); float amount = max(dot(normalTangent, lightDirNorm), 0); float3 reflect = normalize(2 * amount * normalTangent - lightDirNorm); float specular = min(pow(saturate(dot(reflect, halfVec)), 10), amount); return (colorMap * coneAttenuation * lightColor * lightStrength) + (colorMap * specular * coneAttenuation * normalColor.a); }
2. ## HLSL is it possible to output integers from pixel shader?

The first one. 1 / 65535 becomes 0.98 for example. I'm using it as a bitfield and I'm using additive blending so it really adds up.
3. ## HLSL is it possible to output integers from pixel shader?

I'm experiencing large floating point precision errors during the conversion between 0-1 floats and 0-65535 integers. I'm using RGBA64 format for a texture. In Shader Model 3.0 is it possible to output int4 or something instead of float4 from pixel shader? This question is a bit similar to this: https://gamedev.stackexchange.com/questions/132123/is-it-possible-with-directx11-to-have-pixel-shader-output-an-integer-rather-than
4. ## HLSL - Sample the backbuffer I'm rendering to

InterlockedOr requires shader model 4.0, don't know what you mean by texture UAV. Although I found a perfect solution for my specific case. I can just use additive blending among the shadows being drawn in the same batch because they are all created by different lights, which will create the same result as using bitwise OR 1 + 2 = 1 | 2 = 3, however if the bit is already set 2 + 2 ≠ 2 | 2 = 2 It will still need to sample the shadows from other characters and check if the bit is already set, if that's the case it should return 0 (add 0), otherwise, return light bit for the shadow.
5. ## HLSL - Additive blending

You need to set a BlendState for this, the following code adds the two colors together and calculates alpha as source * 1 + destination * 0, the result will of course be source. BlendState additiveColorBlendState = new BlendState { ColorBlendFunction = BlendFunction.Add, ColorSourceBlend = Blend.One, ColorDestinationBlend = Blend.One, AlphaSourceBlend = Blend.One, AlphaDestinationBlend = Blend.Zero, AlphaBlendFunction = BlendFunction.Add };
6. ## HLSL - Additive blending

I have the following HLSL technique. I want to do additive blending on RGB (color) only, not alpha. The alpha should just override the underlying alpha. This also does additive blending on alpha. How do I modify it to only do additive blending on RGB? technique AdditiveTest { pass Pass1 { AlphaBlendEnable = FALSE; VertexShader = compile vs_3_0 AdditiveVertexShader(); PixelShader = compile ps_3_0 AdditivePixelShader(); SrcBlend = ONE; DestBlend = ONE; } }
7. ## HLSL - Sample the backbuffer I'm rendering to

I could use additive blending. How do I set blending to additive blending color only (no alpha blending) for a technique in HLSL?